From 0232433a2ddd64c270a4d049f5ae9895245ee058 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Thu, 26 Jun 2025 00:54:29 -0400 Subject: Add improved line editing to debug shell Improved line editing include moveable cursor and command history --- pkg/shell/shell.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 pkg/shell/shell.go (limited to 'pkg/shell/shell.go') diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go new file mode 100644 index 0000000..19a4223 --- /dev/null +++ b/pkg/shell/shell.go @@ -0,0 +1,50 @@ +package shell + +import ( + "fmt" + "os" + + "golang.org/x/term" +) + +var commands = []string{ + "help", + "clear", + "let", + "del", + "slice", + "rematch", + "repattern", + "tokenize", + "opt_simplify", "opt_tighten", "opt_flatten", "opt_sort", "opt_tidy", "opt_contradictions", "opt_compact", "opt_strictEq", + "parse", + "compile", +} + +func (inter *Interpreter) Run() error { + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer term.Restore(int(os.Stdin.Fd()), oldState) + inter.term = term.NewTerminal(os.Stdin, "atlasi> ") + inter.term.SetPrompt( + string(inter.term.Escape.Yellow) + "atlasi> " + + string(inter.term.Escape.Reset), + ) + + for { + line, err := inter.term.ReadLine() + if err != nil { + return err + } + tokens := inter.Tokenize(line) + fatal, err := inter.Eval(inter.term, tokens) + if fatal { + return err + } else if err != nil { + fmt.Fprintln(inter.term, string(inter.term.Escape.Red), "Error:", + string(inter.term.Escape.Reset), err) + } + } +} -- cgit v1.2.3