From 3cdd21a4fa40655fc0da483b6ba09e71f03efd96 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 27 Feb 2022 19:27:18 -0400 Subject: [PATCH] refactor: remove gnu readline --- Makefile | 3 - go.mod | 1 - go.sum | 2 - rl.go | 1 + rl_gnu.go | 252 ------------------------------------------------------ 5 files changed, 1 insertion(+), 258 deletions(-) delete mode 100644 rl_gnu.go diff --git a/Makefile b/Makefile index aa19a40..658595c 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,6 @@ build: dev: @go build -ldflags "-s -w -X main.version=$(shell git describe --tags)" -gnurl: - @go build -ldflags "-s -w -X main.version=$(shell git describe --tags)+grnurl" -tags gnurl - install: @install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish" @mkdir -p "$(DESTDIR)$(LIBDIR)" diff --git a/go.mod b/go.mod index 2d499d4..ef8261b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module hilbish go 1.16 require ( - github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503 github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 github.com/maxlandon/readline v0.1.0-beta.0.20211027085530-2b76cabb8036 github.com/pborman/getopt v1.1.0 diff --git a/go.sum b/go.sum index 4e19fe4..ecba767 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503 h1:hA2kXBwY8SFH3+PT67CkoUjlMuRN08RSEtsKjwQ+of4= -github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503/go.mod h1:OH+WJSCks0t2ISvaCFUT4ZxNGr4Etq4ju9JE/UxH/5o= github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d h1:KBttN41h/tPahmpaZavviwQ8q4rCkt5CD0HdVmfgPVA= github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs= github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20211022004519-f67a49cb50f5 h1:ygwVRX8gf5MHA0VzSgOdscCEoAJLjM8joEotfQPgAd0= diff --git a/rl.go b/rl.go index 210084f..8752c78 100644 --- a/rl.go +++ b/rl.go @@ -198,6 +198,7 @@ func newLineReader(prompt string) *lineReader { } func (lr *lineReader) Read() (string, error) { + hooks.Em.Emit("command.precmd", nil) s, err := lr.rl.Readline() // this is so dumb if err == readline.EOF { diff --git a/rl_gnu.go b/rl_gnu.go deleted file mode 100644 index 4e09bf0..0000000 --- a/rl_gnu.go +++ /dev/null @@ -1,252 +0,0 @@ -// +build gnurl - -package main - -// Here we define a generic interface for readline and hilbiline, -// making them interchangable during build time -// this is normal readline - -import ( - "os" - "path/filepath" - "strings" - - "github.com/Rosettea/readline" - "github.com/yuin/gopher-lua" -) - -type lineReader struct { - Prompt string -} - -func newLineReader(prompt string) *lineReader { - readline.Init() - - readline.Completer = func(query string, ctx string) []string { - var completions []string - // trim whitespace from ctx - ctx = strings.TrimLeft(ctx, " ") - if len(ctx) == 0 { - return []string{} - } - fields := strings.Split(ctx, " ") - if len(fields) == 0 { - return []string{} - } - - ctx = aliases.Resolve(ctx) - - if len(fields) == 1 { - fileCompletions := fileComplete(query, ctx, fields) - if len(fileCompletions) != 0 { - return fileCompletions - } - - // filter out executables, but in path - 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) - } - } - } - // add lua registered commands to completions - for cmdName := range commands { - if strings.HasPrefix(cmdName, query) { - completions = append(completions, cmdName) - } - } - } else { - if completecb, ok := luaCompletions["command." + fields[0]]; ok { - err := l.CallByParam(lua.P{ - Fn: completecb, - NRet: 1, - Protect: true, - }) - - if err != nil { - return []string{} - } - - 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 - // 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 == "" { - // complete files - completions = append(completions, readline.FilenameCompleter(query, ctx)...) - } else { - if strings.HasPrefix(val, query) { - completions = append(completions, val) - } - } - }) - } 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 - } - - 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") - } - } - } - } - }) - } - } - - if len(completions) == 0 { - completions = readline.FilenameCompleter(query, ctx) - } - } - return completions - } - readline.LoadHistory(defaultHistPath) - - return &lineReader{ - Prompt: prompt, - } -} - -func (lr *lineReader) Read() (string, error) { - hooks.Em.Emit("command.precmd", nil) - return readline.String(lr.Prompt) -} - -func (lr *lineReader) SetPrompt(prompt string) { - lr.Prompt = prompt -} - -func (lr *lineReader) AddHistory(cmd string) { - readline.AddHistory(cmd) - readline.SaveHistory(defaultHistPath) -} - -func (lr *lineReader) ClearInput() { - readline.ReplaceLine("", 0) - readline.RefreshLine() -} - -func (lr *lineReader) Resize() { - readline.Resize() -} - -// 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, - } - - mod := L.SetFuncs(L.NewTable(), lrLua) - - 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 { - l.Push(lua.LNumber(readline.HistorySize())) - - return 1 -} - -func (lr *lineReader) luaGetHistory(l *lua.LState) int { - idx := l.CheckInt(1) - cmd := readline.GetHistory(idx) - l.Push(lua.LString(cmd)) - - return 1 -} - -func (lr *lineReader) luaAllHistory(l *lua.LState) int { - tbl := l.NewTable() - size := readline.HistorySize() - - for i := 0; i < size; i++ { - cmd := readline.GetHistory(i) - tbl.Append(lua.LString(cmd)) - } - - l.Push(tbl) - - return 1 -} - -func (lr *lineReader) luaClearHistory(l *lua.LState) int { - readline.ClearHistory() - readline.SaveHistory(defaultHistPath) - - return 0 -}