2021-10-16 15:31:01 +00:00
|
|
|
package main
|
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
import (
|
2022-02-27 23:21:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-22 16:24:31 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-02-27 23:21:21 +00:00
|
|
|
"github.com/maxlandon/readline"
|
2021-11-22 16:24:31 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
type lineReader struct {
|
2022-02-27 23:21:21 +00:00
|
|
|
rl *readline.Instance
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
2022-03-05 19:57:46 +00:00
|
|
|
var fileHist *fileHistory
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2022-02-27 23:21:21 +00:00
|
|
|
// other gophers might hate this naming but this is local, shut up
|
2022-03-06 18:38:27 +00:00
|
|
|
func newLineReader(prompt string, noHist bool) *lineReader {
|
2022-02-27 23:21:21 +00:00
|
|
|
rl := readline.NewInstance()
|
2022-03-06 18:38:27 +00:00
|
|
|
// we don't mind hilbish.read rl instances having completion,
|
|
|
|
// but it cant have shared history
|
|
|
|
if !noHist {
|
2022-03-13 01:43:02 +00:00
|
|
|
fileHist = newFileHistory()
|
2022-03-19 17:24:12 +00:00
|
|
|
rl.SetHistoryCtrlR("History", fileHist)
|
2022-03-06 18:38:27 +00:00
|
|
|
rl.HistoryAutoWrite = false
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
2022-03-02 02:00:46 +00:00
|
|
|
rl.ShowVimMode = false
|
|
|
|
rl.ViModeCallback = func(mode readline.ViMode) {
|
|
|
|
modeStr := ""
|
|
|
|
switch mode {
|
|
|
|
case readline.VimKeys: modeStr = "normal"
|
|
|
|
case readline.VimInsert: modeStr = "insert"
|
|
|
|
case readline.VimDelete: modeStr = "delete"
|
|
|
|
case readline.VimReplaceOnce:
|
|
|
|
case readline.VimReplaceMany: modeStr = "replace"
|
|
|
|
}
|
|
|
|
setVimMode(modeStr)
|
|
|
|
}
|
2022-03-13 20:44:11 +00:00
|
|
|
rl.ViActionCallback = func(action readline.ViAction, args []string) {
|
|
|
|
actionStr := ""
|
|
|
|
switch action {
|
|
|
|
case readline.VimActionPaste: actionStr = "paste"
|
|
|
|
case readline.VimActionYank: actionStr = "yank"
|
|
|
|
}
|
|
|
|
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
|
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
|
|
|
|
ctx := string(line)
|
2021-11-22 16:24:31 +00:00
|
|
|
var completions []string
|
2022-03-05 19:59:00 +00:00
|
|
|
|
|
|
|
var compGroup []*readline.CompletionGroup
|
2022-02-27 23:21:21 +00:00
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
ctx = strings.TrimLeft(ctx, " ")
|
2021-11-24 00:09:07 +00:00
|
|
|
if len(ctx) == 0 {
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", compGroup
|
2021-11-24 00:09:07 +00:00
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
|
2021-11-23 02:52:58 +00:00
|
|
|
fields := strings.Split(ctx, " ")
|
2021-11-22 16:24:31 +00:00
|
|
|
if len(fields) == 0 {
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
query := fields[len(fields) - 1]
|
2021-11-22 16:24:31 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
ctx = aliases.Resolve(ctx)
|
2022-03-05 19:59:00 +00:00
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
if len(fields) == 1 {
|
2022-03-05 19:59:00 +00:00
|
|
|
completions, prefix := binaryComplete(query, ctx, fields)
|
2022-03-05 12:58:12 +00:00
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
compGroup = append(compGroup, &readline.CompletionGroup{
|
|
|
|
TrimSlash: false,
|
|
|
|
NoSpace: true,
|
|
|
|
Suggestions: completions,
|
|
|
|
})
|
|
|
|
|
|
|
|
return prefix, compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
} else {
|
|
|
|
if completecb, ok := luaCompletions["command." + fields[0]]; ok {
|
2022-03-05 19:59:00 +00:00
|
|
|
luaFields := l.NewTable()
|
|
|
|
for _, f := range fields {
|
|
|
|
luaFields.Append(lua.LString(f))
|
|
|
|
}
|
2021-11-22 16:24:31 +00:00
|
|
|
err := l.CallByParam(lua.P{
|
|
|
|
Fn: completecb,
|
|
|
|
NRet: 1,
|
|
|
|
Protect: true,
|
2022-03-05 19:59:00 +00:00
|
|
|
}, lua.LString(query), lua.LString(ctx), luaFields)
|
2021-11-22 16:24:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
luacompleteTable := l.Get(-1)
|
|
|
|
l.Pop(1)
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
/*
|
|
|
|
as an example with git,
|
|
|
|
completion table should be structured like:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
items = {
|
|
|
|
'add',
|
|
|
|
'clone',
|
|
|
|
'init'
|
|
|
|
},
|
|
|
|
type = 'grid'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items = {
|
|
|
|
'-c',
|
|
|
|
'--git-dir'
|
|
|
|
},
|
|
|
|
type = 'list'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
^ a table of completion groups.
|
|
|
|
it is the responsibility of the completer
|
|
|
|
to work on subcommands and subcompletions
|
|
|
|
*/
|
2021-11-22 16:24:31 +00:00
|
|
|
if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok {
|
|
|
|
cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) {
|
|
|
|
if key.Type() == lua.LTNumber {
|
2022-03-05 19:59:00 +00:00
|
|
|
// completion group
|
|
|
|
if value.Type() == lua.LTTable {
|
|
|
|
luaCmpGroup := value.(*lua.LTable)
|
|
|
|
compType := luaCmpGroup.RawGet(lua.LString("type"))
|
|
|
|
compItems := luaCmpGroup.RawGet(lua.LString("items"))
|
|
|
|
if compType.Type() != lua.LTString {
|
|
|
|
l.RaiseError("bad type name for completion (expected string, got %v)", compType.Type().String())
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
if compItems.Type() != lua.LTTable {
|
|
|
|
l.RaiseError("bad items for completion (expected table, got %v)", compItems.Type().String())
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
var items []string
|
|
|
|
itemDescriptions := make(map[string]string)
|
|
|
|
compItems.(*lua.LTable).ForEach(func(k lua.LValue, v lua.LValue) {
|
|
|
|
if k.Type() == lua.LTString {
|
|
|
|
// ['--flag'] = {'description', '--flag-alias'}
|
|
|
|
itm := v.(*lua.LTable)
|
|
|
|
items = append(items, k.String())
|
|
|
|
itemDescriptions[k.String()] = itm.RawGet(lua.LNumber(1)).String()
|
2021-12-07 21:58:56 +00:00
|
|
|
} else {
|
2022-03-05 19:59:00 +00:00
|
|
|
items = append(items, v.String())
|
2021-12-07 21:58:56 +00:00
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var dispType readline.TabDisplayType
|
|
|
|
switch compType.String() {
|
|
|
|
case "grid": dispType = readline.TabDisplayGrid
|
|
|
|
case "list": dispType = readline.TabDisplayList
|
|
|
|
// need special cases, will implement later
|
|
|
|
//case "map": dispType = readline.TabDisplayMap
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
compGroup = append(compGroup, &readline.CompletionGroup{
|
|
|
|
DisplayType: dispType,
|
|
|
|
Descriptions: itemDescriptions,
|
|
|
|
Suggestions: items,
|
|
|
|
TrimSlash: false,
|
|
|
|
NoSpace: true,
|
|
|
|
})
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
if len(compGroup) == 0 {
|
2022-02-27 23:21:21 +00:00
|
|
|
completions = fileComplete(query, ctx, fields)
|
2022-03-05 19:59:00 +00:00
|
|
|
compGroup = append(compGroup, &readline.CompletionGroup{
|
|
|
|
TrimSlash: false,
|
|
|
|
NoSpace: true,
|
|
|
|
Suggestions: completions,
|
|
|
|
})
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
return &lineReader{
|
2022-02-27 23:21:21 +00:00
|
|
|
rl,
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) Read() (string, error) {
|
2022-02-27 23:27:18 +00:00
|
|
|
hooks.Em.Emit("command.precmd", nil)
|
2022-02-27 23:21:21 +00:00
|
|
|
s, err := lr.rl.Readline()
|
|
|
|
// this is so dumb
|
|
|
|
if err == readline.EOF {
|
2022-03-02 02:00:46 +00:00
|
|
|
fmt.Println("")
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, err // might get another error
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 21:20:41 +00:00
|
|
|
func (lr *lineReader) SetPrompt(p string) {
|
|
|
|
halfPrompt := strings.Split(p, "\n")
|
2022-02-27 23:21:21 +00:00
|
|
|
if len(halfPrompt) > 1 {
|
2022-03-05 01:16:45 +00:00
|
|
|
lr.rl.Multiline = true
|
2022-02-27 23:21:21 +00:00
|
|
|
lr.rl.SetPrompt(strings.Join(halfPrompt[:len(halfPrompt) - 1], "\n"))
|
|
|
|
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
|
|
|
} else {
|
2022-03-05 01:16:45 +00:00
|
|
|
lr.rl.Multiline = false
|
2022-03-13 20:42:14 +00:00
|
|
|
lr.rl.MultilinePrompt = ""
|
2022-03-06 21:20:41 +00:00
|
|
|
lr.rl.SetPrompt(p)
|
2022-02-27 23:21:21 +00:00
|
|
|
}
|
2022-03-06 21:20:41 +00:00
|
|
|
if initialized && !running {
|
2022-03-02 02:00:46 +00:00
|
|
|
lr.rl.RefreshPromptInPlace("")
|
|
|
|
}
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) AddHistory(cmd string) {
|
2022-03-05 19:57:46 +00:00
|
|
|
fileHist.Write(cmd)
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) ClearInput() {
|
2022-02-27 23:21:21 +00:00
|
|
|
return
|
2021-06-09 01:00:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) Resize() {
|
2022-02-27 23:21:21 +00:00
|
|
|
return
|
2021-11-22 15:41:27 +00:00
|
|
|
}
|
2022-01-27 21:02:21 +00:00
|
|
|
|
|
|
|
// lua module
|
|
|
|
func (lr *lineReader) Loader(L *lua.LState) *lua.LTable {
|
|
|
|
lrLua := map[string]lua.LGFunction{
|
|
|
|
"add": lr.luaAddHistory,
|
|
|
|
"all": lr.luaAllHistory,
|
|
|
|
"clear": lr.luaClearHistory,
|
|
|
|
"get": lr.luaGetHistory,
|
|
|
|
"size": lr.luaSize,
|
|
|
|
}
|
|
|
|
|
2022-02-27 23:21:21 +00:00
|
|
|
mod := l.SetFuncs(l.NewTable(), lrLua)
|
2022-01-27 21:02:21 +00:00
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *lineReader) luaAddHistory(l *lua.LState) int {
|
|
|
|
cmd := l.CheckString(1)
|
|
|
|
lr.AddHistory(cmd)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:57:46 +00:00
|
|
|
func (lr *lineReader) luaSize(L *lua.LState) int {
|
|
|
|
L.Push(lua.LNumber(fileHist.Len()))
|
|
|
|
|
|
|
|
return 1
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:57:46 +00:00
|
|
|
func (lr *lineReader) luaGetHistory(L *lua.LState) int {
|
|
|
|
idx := L.CheckInt(1)
|
|
|
|
cmd, _ := fileHist.GetLine(idx)
|
|
|
|
L.Push(lua.LString(cmd))
|
|
|
|
|
2022-02-27 23:21:21 +00:00
|
|
|
return 0
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:57:46 +00:00
|
|
|
func (lr *lineReader) luaAllHistory(L *lua.LState) int {
|
|
|
|
tbl := L.NewTable()
|
|
|
|
size := fileHist.Len()
|
|
|
|
|
|
|
|
for i := 1; i < size; i++ {
|
|
|
|
cmd, _ := fileHist.GetLine(i)
|
|
|
|
tbl.Append(lua.LString(cmd))
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(tbl)
|
|
|
|
|
2022-02-27 23:21:21 +00:00
|
|
|
return 0
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *lineReader) luaClearHistory(l *lua.LState) int {
|
|
|
|
return 0
|
2022-03-05 19:57:46 +00:00
|
|
|
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|