From e0851809ab42197389646f497f75c93c2d7e911b Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 17 May 2021 19:03:56 -0400 Subject: [PATCH] feat: make readline agnostic, add hilbiline to test --- go.mod | 1 + go.sum | 7 +++++++ main.go | 18 +++++++++--------- rl.go | 36 ++++++++++++++++++++++++++++++++++++ rl_hilbiline.go | 34 ++++++++++++++++++++++++++++++++++ shell.go | 4 ++-- 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 rl.go create mode 100644 rl_hilbiline.go diff --git a/go.mod b/go.mod index 3cc8f3d..2ab24fd 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module hilbish go 1.16 require ( + github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4 // indirect github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 github.com/pborman/getopt v1.1.0 diff --git a/go.sum b/go.sum index f682465..cdcec8b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4 h1:S9ulYwm5MUp02mEKnbDhVGjVBkdYV7/tmgEO1g3Q+Z8= +github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4/go.mod h1:mp9I7lvFoZ3ldAeydCzISakl6VdQYyrR8vVjmnKGqDo= github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e h1:4G8AYOOwZdDWOiJR6D6JXaFmj5BDS7c5D5PyqsG/+Hg= github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e/go.mod h1:fmqtV+Wqx0uFYLN1F4VhjZdtT56Dr8c3yA7nALFsw/Q= github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 h1:xz6Nv3zcwO2Lila35hcb0QloCQsc38Al13RNEzWRpX4= @@ -15,10 +17,14 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0= github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk= github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -33,6 +39,7 @@ golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201029080932-201ba4db2418/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4= golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20191110171634-ad39bd3f0407/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= diff --git a/main.go b/main.go index 34ccd29..74b6983 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,6 @@ import ( "hilbish/golibs/bait" "github.com/pborman/getopt" - "github.com/bobappleyard/readline" "github.com/yuin/gopher-lua" "layeh.com/gopher-luar" "golang.org/x/term" @@ -22,6 +21,7 @@ import ( var ( version = "v0.4.0" l *lua.LState + lr *LineReader prompt string // User's prompt, this will get set when lua side is initialized multilinePrompt = "> " @@ -113,8 +113,7 @@ func main() { RunLogin() RunConfig(*configflag) - readline.Completer = readline.FilenameCompleter - readline.LoadHistory(homedir + "/.hilbish-history") + lr = NewLineReader("") if *cmdflag != "" { RunInput(*cmdflag) @@ -133,7 +132,8 @@ func main() { for interactive { running = false - input, err := readline.String(fmtPrompt()) + lr.SetPrompt(fmtPrompt()) + input, err := lr.Read() if err == io.EOF { // Exit if user presses ^D (ctrl + d) @@ -171,7 +171,8 @@ func main() { func ContinuePrompt(prev string) (string, error) { hooks.Em.Emit("multiline", nil) - cont, err := readline.String(multilinePrompt) + lr.SetPrompt(multilinePrompt) + cont, err := lr.Read() if err != nil { fmt.Println("") return "", err @@ -214,15 +215,14 @@ func HandleSignals() { for range c { if !running { - readline.ReplaceLine("", 0) - readline.RefreshLine() + //readline.ReplaceLine("", 0) + //readline.RefreshLine() } } } func HandleHistory(cmd string) { - readline.AddHistory(cmd) - readline.SaveHistory(homedir + "/.hilbish-history") + lr.AddHistory(cmd) // TODO: load history again (history shared between sessions like this ye) } diff --git a/rl.go b/rl.go new file mode 100644 index 0000000..d909957 --- /dev/null +++ b/rl.go @@ -0,0 +1,36 @@ +// +build !hilbiline + +// Here we define a generic interface for readline and hilbiline, +// making them interchangable during build time +// this is normal readline +package main + +import "github.com/bobappleyard/readline" + +type LineReader struct { + Prompt string +} + +// other gophers might hate this naming but this is local, shut up +func NewLineReader(prompt string) *LineReader { + readline.Completer = readline.FilenameCompleter + readline.LoadHistory(homedir + "/.hilbish-history") + + return &LineReader{ + Prompt: prompt, + } +} + +func (lr *LineReader) Read() (string, error) { + 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(homedir + "/.hilbish-history") +} + diff --git a/rl_hilbiline.go b/rl_hilbiline.go new file mode 100644 index 0000000..6c6fff2 --- /dev/null +++ b/rl_hilbiline.go @@ -0,0 +1,34 @@ +// +build hilbiline + +// Here we define a generic interface for readline and hilbiline, +// making them interchangable during build time +// this is hilbiline's, as is obvious by the filename +package main + +import "github.com/Hilbis/Hilbiline" + +type LineReader struct { + hl *hilbiline.HilbilineState +} + +// other gophers might hate this naming but this is local, shut up +func NewLineReader(prompt string) *LineReader { + hl := hilbiline.New(prompt) + + return &LineReader{ + &hl, + } +} + +func (lr *LineReader) Read() (string, error) { + return lr.hl.Read() +} + +func (lr *LineReader) SetPrompt(prompt string) { + lr.hl.SetPrompt(prompt) +} + +func (lr *LineReader) AddHistory(cmd string) { + return +} + diff --git a/shell.go b/shell.go index d60f5b2..c16781d 100644 --- a/shell.go +++ b/shell.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "github.com/bobappleyard/readline" +// "github.com/bobappleyard/readline" "github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua/parse" "layeh.com/gopher-luar" @@ -132,7 +132,7 @@ func splitInput(input string) ([]string, string) { cmdArgs := []string{} sb := &strings.Builder{} cmdstr := &strings.Builder{} - lastcmd := readline.GetHistory(readline.HistorySize() - 1) + lastcmd := "" //readline.GetHistory(readline.HistorySize() - 1) for _, r := range input { if r == '"' {