mirror of
https://github.com/Hilbis/Hilbish
synced 2025-04-16 02:23:22 +00:00
feat: add abbreviations (#340)
This commit is contained in:
parent
02c89b99dd
commit
6cd294373c
67
docs/api/hilbish/hilbish.abbr.md
Normal file
67
docs/api/hilbish/hilbish.abbr.md
Normal file
@ -0,0 +1,67 @@
|
||||
---
|
||||
title: Module hilbish.abbr
|
||||
description: command line abbreviations
|
||||
layout: doc
|
||||
menu:
|
||||
docs:
|
||||
parent: "API"
|
||||
---
|
||||
|
||||
|
||||
## Introduction
|
||||
The abbr module manages Hilbish abbreviations. These are words that can be replaced
|
||||
with longer command line strings when entered.
|
||||
As an example, `git push` can be abbreviated to `gp`. When the user types
|
||||
`gp` into the command line, after hitting space or enter, it will expand to `git push`.
|
||||
Abbreviations can be used as an alternative to aliases. They are saved entirely in the history
|
||||
Instead of the aliased form of the same command.
|
||||
|
||||
## Functions
|
||||
|||
|
||||
|----|----|
|
||||
|<a href="#remove">remove(abbr)</a>|Removes the named `abbr`.|
|
||||
|<a href="#add">add(abbr, expanded|function, opts)</a>|Adds an abbreviation. The `abbr` is the abbreviation itself,|
|
||||
<hr>
|
||||
<div id='add'>
|
||||
<h4 class='heading'>
|
||||
hilbish.abbr.add(abbr, expanded|function, opts)
|
||||
<a href="#add" class='heading-link'>
|
||||
<i class="fas fa-paperclip"></i>
|
||||
</a>
|
||||
</h4>
|
||||
|
||||
Adds an abbreviation. The `abbr` is the abbreviation itself,
|
||||
while `expanded` is what the abbreviation should expand to.
|
||||
It can be either a function or a string. If it is a function, it will expand to what
|
||||
the function returns.
|
||||
`opts` is a table that accepts 1 key: `anywhere`.
|
||||
`opts.anywhere` defines whether the abbr expands anywhere in the command line or not,
|
||||
whereas the default behavior is only at the beginning of the line
|
||||
#### Parameters
|
||||
`abbr` **`string`**
|
||||
|
||||
|
||||
`expanded|function` **`string`**
|
||||
|
||||
|
||||
`opts` **`table`**
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
<div id='remove'>
|
||||
<h4 class='heading'>
|
||||
hilbish.abbr.remove(abbr)
|
||||
<a href="#remove" class='heading-link'>
|
||||
<i class="fas fa-paperclip"></i>
|
||||
</a>
|
||||
</h4>
|
||||
|
||||
Removes the named `abbr`.
|
||||
#### Parameters
|
||||
`abbr` **`string`**
|
||||
|
||||
|
||||
</div>
|
||||
|
@ -14,12 +14,30 @@ directly interact with the line editor in use.
|
||||
## Functions
|
||||
|||
|
||||
|----|----|
|
||||
|<a href="#editor.deleteByAmount">deleteByAmount(amount)</a>|Deletes characters in the line by the given amount.|
|
||||
|<a href="#editor.getLine">getLine() -> string</a>|Returns the current input line.|
|
||||
|<a href="#editor.getVimRegister">getVimRegister(register) -> string</a>|Returns the text that is at the register.|
|
||||
|<a href="#editor.insert">insert(text)</a>|Inserts text into the Hilbish command line.|
|
||||
|<a href="#editor.getChar">getChar() -> string</a>|Reads a keystroke from the user. This is in a format of something like Ctrl-L.|
|
||||
|<a href="#editor.setVimRegister">setVimRegister(register, text)</a>|Sets the vim register at `register` to hold the passed text.|
|
||||
|
||||
<hr>
|
||||
<div id='editor.deleteByAmount'>
|
||||
<h4 class='heading'>
|
||||
hilbish.editor.deleteByAmount(amount)
|
||||
<a href="#editor.deleteByAmount" class='heading-link'>
|
||||
<i class="fas fa-paperclip"></i>
|
||||
</a>
|
||||
</h4>
|
||||
|
||||
Deletes characters in the line by the given amount.
|
||||
|
||||
#### Parameters
|
||||
`number` **`amount`**
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
<div id='editor.getLine'>
|
||||
<h4 class='heading'>
|
||||
@ -96,6 +114,9 @@ hilbish.editor.setVimRegister(register, text)
|
||||
Sets the vim register at `register` to hold the passed text.
|
||||
|
||||
#### Parameters
|
||||
`string` **`register`**
|
||||
|
||||
|
||||
`string` **`text`**
|
||||
|
||||
|
||||
|
22
editor.go
22
editor.go
@ -17,6 +17,7 @@ func editorLoader(rtm *rt.Runtime) *rt.Table {
|
||||
"getVimRegister": {editorGetRegister, 2, false},
|
||||
"getLine": {editorGetLine, 0, false},
|
||||
"readChar": {editorReadChar, 0, false},
|
||||
"deleteByAmount": {editorDeleteByAmount, 1, false},
|
||||
}
|
||||
|
||||
mod := rt.NewTable()
|
||||
@ -47,7 +48,7 @@ func editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
// #interface editor
|
||||
// setVimRegister(register, text)
|
||||
// Sets the vim register at `register` to hold the passed text.
|
||||
// #aram register string
|
||||
// #param register string
|
||||
// #param text string
|
||||
func editorSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
@ -106,3 +107,22 @@ func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
||||
}
|
||||
|
||||
// #interface editor
|
||||
// deleteByAmount(amount)
|
||||
// Deletes characters in the line by the given amount.
|
||||
// #param amount number
|
||||
func editorDeleteByAmount(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
amount, err := c.IntArg(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lr.rl.DeleteByAmount(int(amount))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
@ -7,6 +7,9 @@ local hilbish = {}
|
||||
--- @param cmd string
|
||||
function hilbish.aliases.add(alias, cmd) end
|
||||
|
||||
--- Deletes characters in the line by the given amount.
|
||||
function hilbish.editor.deleteByAmount(amount) end
|
||||
|
||||
--- Returns the current input line.
|
||||
function hilbish.editor.getLine() end
|
||||
|
||||
|
61
nature/abbr.lua
Normal file
61
nature/abbr.lua
Normal file
@ -0,0 +1,61 @@
|
||||
-- @module hilbish.abbr
|
||||
-- command line abbreviations
|
||||
-- The abbr module manages Hilbish abbreviations. These are words that can be replaced
|
||||
-- with longer command line strings when entered.
|
||||
-- As an example, `git push` can be abbreviated to `gp`. When the user types
|
||||
-- `gp` into the command line, after hitting space or enter, it will expand to `git push`.
|
||||
-- Abbreviations can be used as an alternative to aliases. They are saved entirely in the history
|
||||
-- Instead of the aliased form of the same command.
|
||||
local bait = require 'bait'
|
||||
local hilbish = require 'hilbish'
|
||||
hilbish.abbr = {
|
||||
all = {}
|
||||
}
|
||||
|
||||
--- Adds an abbreviation. The `abbr` is the abbreviation itself,
|
||||
--- while `expanded` is what the abbreviation should expand to.
|
||||
--- It can be either a function or a string. If it is a function, it will expand to what
|
||||
--- the function returns.
|
||||
--- `opts` is a table that accepts 1 key: `anywhere`.
|
||||
--- `opts.anywhere` defines whether the abbr expands anywhere in the command line or not,
|
||||
--- whereas the default behavior is only at the beginning of the line
|
||||
-- @param abbr string
|
||||
-- @param expanded|function string
|
||||
-- @param opts table
|
||||
function hilbish.abbr.add(abbr, expanded, opts)
|
||||
print(abbr, expanded, opts)
|
||||
opts = opts or {}
|
||||
opts.abbr = abbr
|
||||
opts.expand = expanded
|
||||
hilbish.abbr.all[abbr] = opts
|
||||
end
|
||||
|
||||
--- Removes the named `abbr`.
|
||||
-- @param abbr string
|
||||
function hilbish.abbr.remove(abbr)
|
||||
hilbish.abbr.all[abbr] = nil
|
||||
end
|
||||
|
||||
bait.catch('hilbish.rawInput', function(c)
|
||||
-- 0x0d == enter
|
||||
if c == ' ' or c == string.char(0x0d) then
|
||||
-- check if the last "word" was a valid abbreviation
|
||||
local line = hilbish.editor.getLine()
|
||||
local lineSplits = string.split(line, ' ')
|
||||
local thisAbbr = hilbish.abbr.all[lineSplits[#lineSplits]]
|
||||
|
||||
if thisAbbr and (#lineSplits == 1 or thisAbbr.anywhere == true) then
|
||||
hilbish.editor.deleteByAmount(-lineSplits[#lineSplits]:len())
|
||||
if type(thisAbbr.expand) == 'string' then
|
||||
hilbish.editor.insert(thisAbbr.expand)
|
||||
elseif type(thisAbbr.expand) == 'function' then
|
||||
local expandRet = thisAbbr.expand()
|
||||
if type(expandRet) ~= 'string' then
|
||||
print(string.format('abbr %s has an expand function that did not return a string. instead it returned: %s', thisAbbr.abbr, expandRet))
|
||||
return
|
||||
end
|
||||
hilbish.editor.insert(expandRet)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
@ -26,6 +26,7 @@ require 'nature.opts'
|
||||
require 'nature.vim'
|
||||
require 'nature.runner'
|
||||
require 'nature.hummingbird'
|
||||
require 'nature.abbr'
|
||||
|
||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||
if shlvl ~= nil then
|
||||
|
@ -142,6 +142,10 @@ func (rl *Instance) viDeleteByAdjust(adjust int) {
|
||||
rl.updateHelpers()
|
||||
}
|
||||
|
||||
func (rl *Instance) DeleteByAmount(adjust int) {
|
||||
rl.viDeleteByAdjust(adjust)
|
||||
}
|
||||
|
||||
func (rl *Instance) vimDeleteToken(r rune) bool {
|
||||
tokens, _, _ := tokeniseSplitSpaces(rl.line, 0)
|
||||
pos := int(r) - 48 // convert ASCII to integer
|
||||
|
Loading…
x
Reference in New Issue
Block a user