2021-10-16 15:31:01 +00:00
|
|
|
package main
|
|
|
|
|
2021-05-17 23:03:56 +00:00
|
|
|
// Here we define a generic interface for readline and hilbiline,
|
|
|
|
// making them interchangable during build time
|
2022-02-27 23:21:21 +00:00
|
|
|
// this is hilbiline's, as is obvious by the filename
|
2021-05-17 23:03:56 +00:00
|
|
|
|
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
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-02-27 23:21:21 +00:00
|
|
|
"os"
|
2021-11-22 16:24:31 +00:00
|
|
|
|
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-02-27 23:21:21 +00:00
|
|
|
// other gophers might hate this naming but this is local, shut up
|
2021-12-06 21:21:31 +00:00
|
|
|
func newLineReader(prompt string) *lineReader {
|
2022-02-27 23:21:21 +00:00
|
|
|
rl := readline.NewInstance()
|
|
|
|
rl.Multiline = true
|
|
|
|
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-02-27 23:21:21 +00:00
|
|
|
|
|
|
|
compGroup := []*readline.CompletionGroup{
|
|
|
|
&readline.CompletionGroup{
|
|
|
|
TrimSlash: false,
|
|
|
|
NoSpace: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-02-27 23:21:21 +00:00
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
if len(fields) == 1 {
|
2022-02-27 23:21:21 +00:00
|
|
|
fileCompletions := fileComplete(query, ctx, fields)
|
|
|
|
if len(fileCompletions) != 0 {
|
|
|
|
for _, f := range fileCompletions {
|
|
|
|
name := strings.Replace(query + f, "~", curuser.HomeDir, 1)
|
|
|
|
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
|
|
|
|
continue
|
2021-11-24 00:09:07 +00:00
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
completions = append(completions, f)
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
compGroup[0].Suggestions = completions
|
|
|
|
return "", compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
|
2021-11-23 03:52:14 +00:00
|
|
|
// filter out executables, but in path
|
2021-11-22 16:24:31 +00:00
|
|
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
|
|
|
// print dir to stderr for debugging
|
|
|
|
// search for an executable which matches our query string
|
|
|
|
if matches, err := filepath.Glob(filepath.Join(dir, query + "*")); err == nil {
|
|
|
|
// get basename from matches
|
|
|
|
for _, match := range matches {
|
|
|
|
// check if we have execute permissions for our match
|
|
|
|
if info, err := os.Stat(match); err == nil && info.Mode().Perm() & 0100 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// get basename from match
|
|
|
|
name := filepath.Base(match)
|
|
|
|
// print name to stderr for debugging
|
|
|
|
// add basename to completions
|
|
|
|
completions = append(completions, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
// add lua registered commands to completions
|
|
|
|
for cmdName := range commands {
|
|
|
|
if strings.HasPrefix(cmdName, query) {
|
|
|
|
completions = append(completions, cmdName)
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
|
|
|
|
compGroup[0].Suggestions = completions
|
|
|
|
return query, compGroup
|
2021-11-22 16:24:31 +00:00
|
|
|
} else {
|
|
|
|
if completecb, ok := luaCompletions["command." + fields[0]]; ok {
|
|
|
|
err := l.CallByParam(lua.P{
|
|
|
|
Fn: completecb,
|
|
|
|
NRet: 1,
|
|
|
|
Protect: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok {
|
|
|
|
cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) {
|
|
|
|
// if key is a number (index), we just check and complete that
|
|
|
|
if key.Type() == lua.LTNumber {
|
|
|
|
// if we have only 2 fields then this is fine
|
|
|
|
if len(fields) == 2 {
|
|
|
|
if strings.HasPrefix(value.String(), fields[1]) {
|
|
|
|
completions = append(completions, value.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if key.Type() == lua.LTString {
|
|
|
|
if len(fields) == 2 {
|
|
|
|
if strings.HasPrefix(key.String(), fields[1]) {
|
|
|
|
completions = append(completions, key.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if we have more than 2 fields, we need to check if the key matches
|
|
|
|
// the current field and if it does, we need to check if the value is a string
|
|
|
|
// or table (nested sub completions)
|
|
|
|
if key.String() == fields[1] {
|
|
|
|
// if value is a table, we need to iterate over it
|
|
|
|
// and add each value to completions
|
2021-12-07 21:58:56 +00:00
|
|
|
// check if value is either a table or function
|
|
|
|
if value.Type() == lua.LTTable {
|
|
|
|
valueTbl := value.(*lua.LTable)
|
|
|
|
valueTbl.ForEach(func(key lua.LValue, value lua.LValue) {
|
|
|
|
val := value.String()
|
|
|
|
if val == "<file>" {
|
|
|
|
// complete files
|
2022-02-27 23:21:21 +00:00
|
|
|
completions = append(completions, fileComplete(query, ctx, fields)...)
|
2021-12-07 21:58:56 +00:00
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(val, query) {
|
|
|
|
completions = append(completions, val)
|
|
|
|
}
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2021-12-07 21:58:56 +00:00
|
|
|
})
|
|
|
|
} else if value.Type() == lua.LTFunction {
|
|
|
|
// if value is a function, we need to call it
|
|
|
|
// and add each value to completions
|
|
|
|
// completionsCtx is the context we pass to the function,
|
|
|
|
// removing 2 fields from the fields array
|
|
|
|
completionsCtx := strings.Join(fields[2:], " ")
|
|
|
|
err := l.CallByParam(lua.P{
|
|
|
|
Fn: value,
|
|
|
|
NRet: 1,
|
|
|
|
Protect: true,
|
|
|
|
}, lua.LString(query), lua.LString(completionsCtx))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
2021-12-07 21:58:56 +00:00
|
|
|
|
|
|
|
luacompleteTable := l.Get(-1)
|
|
|
|
l.Pop(1)
|
|
|
|
|
|
|
|
// just check if its actually a table and add it to the completions
|
|
|
|
if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok {
|
|
|
|
cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) {
|
|
|
|
val := value.String()
|
|
|
|
if strings.HasPrefix(val, query) {
|
|
|
|
completions = append(completions, val)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// throw lua error
|
|
|
|
// complete.cmdname: error message...
|
|
|
|
l.RaiseError("complete." + fields[0] + ": completion value is not a table or function")
|
|
|
|
}
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(completions) == 0 {
|
2022-02-27 23:21:21 +00:00
|
|
|
completions = fileComplete(query, ctx, fields)
|
2021-11-22 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 23:21:21 +00:00
|
|
|
|
|
|
|
compGroup[0].Suggestions = completions
|
|
|
|
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 {
|
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, err // might get another error
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) SetPrompt(prompt string) {
|
2022-02-27 23:21:21 +00:00
|
|
|
halfPrompt := strings.Split(prompt, "\n")
|
|
|
|
if len(halfPrompt) > 1 {
|
|
|
|
lr.rl.SetPrompt(strings.Join(halfPrompt[:len(halfPrompt) - 1], "\n"))
|
|
|
|
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
|
|
|
} else {
|
|
|
|
// print cursor up ansi code
|
|
|
|
fmt.Printf("\033[1A")
|
|
|
|
lr.rl.SetPrompt("")
|
|
|
|
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
|
|
|
}
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) AddHistory(cmd string) {
|
2022-02-27 23:21:21 +00:00
|
|
|
return
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *lineReader) luaSize(l *lua.LState) int {
|
2022-02-27 23:21:21 +00:00
|
|
|
return 0
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *lineReader) luaGetHistory(l *lua.LState) int {
|
2022-02-27 23:21:21 +00:00
|
|
|
return 0
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *lineReader) luaAllHistory(l *lua.LState) int {
|
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
|
|
|
|
}
|