mirror of https://github.com/Hilbis/Hilbish
Compare commits
4 Commits
af14dc20a4
...
70c2b91fca
Author | SHA1 | Date |
---|---|---|
sammyette | 70c2b91fca | |
sammyette | 38d036d96f | |
sammyette | 4c61c551aa | |
sammyette | eded38c7b5 |
|
@ -1,3 +1,23 @@
|
||||||
|
local fs = require 'fs'
|
||||||
|
|
||||||
|
-- explanation: this specific function gives to us info about
|
||||||
|
-- the currently running source. this includes a path to the
|
||||||
|
-- source file (info.source)
|
||||||
|
-- we will use that to automatically load all commands by reading
|
||||||
|
-- all the files in this dir and just requiring it.
|
||||||
|
local info = debug.getinfo(1)
|
||||||
|
local commandDir = fs.dir(info.source)
|
||||||
|
if commandDir == '.' then return end
|
||||||
|
|
||||||
|
local commands = fs.readdir(commandDir)
|
||||||
|
for _, command in ipairs(commands) do
|
||||||
|
local name = command:gsub('%.lua', '') -- chop off extension
|
||||||
|
if name ~= 'init' then
|
||||||
|
-- skip this file (for obvious reasons)
|
||||||
|
require('nature.completions.' .. name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function hilbish.completion.handler(line, pos)
|
function hilbish.completion.handler(line, pos)
|
||||||
if type(line) ~= 'string' then error '#1 must be a string' end
|
if type(line) ~= 'string' then error '#1 must be a string' end
|
||||||
if type(pos) ~= 'number' then error '#2 must be a number' end
|
if type(pos) ~= 'number' then error '#2 must be a number' end
|
|
@ -0,0 +1,53 @@
|
||||||
|
local function curry(f)
|
||||||
|
return function (x) return function (y) return f(x,y) end end
|
||||||
|
end
|
||||||
|
|
||||||
|
local flags = {}
|
||||||
|
local function flag(f, description)
|
||||||
|
flags[f] = {description}
|
||||||
|
end
|
||||||
|
|
||||||
|
local addflag = curry(flag)
|
||||||
|
|
||||||
|
addflag '-A' 'Ask for password via askpass or $SUDO_ASKPASS'
|
||||||
|
addflag '-B' 'Ring the bell as part of the password prompt.'
|
||||||
|
|
||||||
|
hilbish.complete('command.sudo', function(query, ctx, fields)
|
||||||
|
table.remove(fields, 1)
|
||||||
|
local nonflags = table.filter(fields, function(v)
|
||||||
|
if v == '' then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return v:match '^%-' == nil
|
||||||
|
end)
|
||||||
|
|
||||||
|
if #fields == 1 or #nonflags == 0 then
|
||||||
|
-- complete commands or sudo flags
|
||||||
|
if query:match ('^%-') then
|
||||||
|
local compFlags = {}
|
||||||
|
for flg, flgstuff in pairs(flags) do
|
||||||
|
if flg:match('^' .. query) then
|
||||||
|
compFlags[flg] = flgstuff
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local compGroup = {
|
||||||
|
items = compFlags,
|
||||||
|
type = 'list'
|
||||||
|
}
|
||||||
|
|
||||||
|
return {compGroup}, query
|
||||||
|
end
|
||||||
|
|
||||||
|
local comps, pfx = hilbish.completion.bins(query, ctx, fields)
|
||||||
|
local compGroup = {
|
||||||
|
items = comps,
|
||||||
|
type = 'grid'
|
||||||
|
}
|
||||||
|
|
||||||
|
return {compGroup}, pfx
|
||||||
|
end
|
||||||
|
|
||||||
|
-- otherwise, get command flags
|
||||||
|
return hilbish.completion.call('command.' .. fields[2], query, ctx, fields)
|
||||||
|
end)
|
|
@ -128,15 +128,19 @@ func (rl *Instance) walkHistory(i int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.histOffset += i
|
rl.histOffset += i
|
||||||
|
historyLen := history.Len()
|
||||||
if rl.histOffset == 0 {
|
if rl.histOffset == 0 {
|
||||||
rl.line = []rune(rl.lineBuf)
|
rl.line = []rune(rl.lineBuf)
|
||||||
rl.pos = len(rl.lineBuf)
|
rl.pos = len(rl.lineBuf)
|
||||||
} else if rl.histOffset <= -1 {
|
} else if rl.histOffset <= -1 {
|
||||||
rl.histOffset = 0
|
rl.histOffset = 0
|
||||||
|
} else if rl.histOffset > historyLen {
|
||||||
|
// TODO: should this wrap around?s
|
||||||
|
rl.histOffset = 0
|
||||||
} else {
|
} else {
|
||||||
dedup = true
|
dedup = true
|
||||||
old = string(rl.line)
|
old = string(rl.line)
|
||||||
new, err = history.GetLine(history.Len() - rl.histOffset)
|
new, err = history.GetLine(historyLen - rl.histOffset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rl.resetHelpers()
|
rl.resetHelpers()
|
||||||
print("\r\n" + err.Error() + "\r\n")
|
print("\r\n" + err.Error() + "\r\n")
|
||||||
|
|
|
@ -29,7 +29,7 @@ func (rl *Instance) updateTabFind(r []rune) {
|
||||||
rl.search = string(rl.tfLine)
|
rl.search = string(rl.tfLine)
|
||||||
|
|
||||||
// We update and print
|
// We update and print
|
||||||
rl.clearHelpers()
|
//rl.clearHelpers()
|
||||||
rl.getTabCompletion()
|
rl.getTabCompletion()
|
||||||
rl.renderHelpers()
|
rl.renderHelpers()
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,7 @@ func (rl *Instance) clearHelpers() {
|
||||||
moveCursorForwards(rl.fullX)
|
moveCursorForwards(rl.fullX)
|
||||||
|
|
||||||
// Clear everything below
|
// Clear everything below
|
||||||
//print(seqClearScreenBelow)
|
print(seqClearScreenBelow)
|
||||||
|
|
||||||
// Go back to current cursor position
|
// Go back to current cursor position
|
||||||
moveCursorBackwards(GetTermWidth())
|
moveCursorBackwards(GetTermWidth())
|
||||||
|
|
Loading…
Reference in New Issue