From 796d1951e15f7ac8a0fa8ce996ec4daf66d47df1 Mon Sep 17 00:00:00 2001 From: refaktor Date: Wed, 11 Sep 2024 11:46:42 +0200 Subject: [PATCH] improvements to console, multiline, bug fixes around dialects, os / pipes contexts --- evaldo/builtins_console.go | 2 +- evaldo/builtins_os_not.go | 6 ++--- evaldo/builtins_pipes_not.go | 6 ++--- evaldo/repl.go | 40 +++++++++++++++++++++------- examples/os_pipes/main.db | Bin 0 -> 8192 bytes examples/os_pipes/make_num_dirs.rye | 13 +++++++++ runner/runner.go | 4 +-- util/microliner.go | 20 ++++++++++++++ 8 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 examples/os_pipes/main.db create mode 100644 examples/os_pipes/make_num_dirs.rye diff --git a/evaldo/builtins_console.go b/evaldo/builtins_console.go index 14da89bf..5e6c559a 100644 --- a/evaldo/builtins_console.go +++ b/evaldo/builtins_console.go @@ -46,7 +46,7 @@ var Builtins_console = map[string]*env.Builtin{ } }*/ - DoRyeRepl(ps, "do", ShowResults) + DoRyeRepl(ps, "rye", ShowResults) fmt.Println("-------------------------------------------------------------") ps.Ser = ser return ps.Res diff --git a/evaldo/builtins_os_not.go b/evaldo/builtins_os_not.go index 451bc0e2..30391403 100644 --- a/evaldo/builtins_os_not.go +++ b/evaldo/builtins_os_not.go @@ -1,5 +1,5 @@ -//go:build no_devops -// +build no_devops +//go:build no_os +// +build no_os package evaldo @@ -7,4 +7,4 @@ import ( "github.com/refaktor/rye/env" ) -var Builtins_devops = map[string]*env.Builtin{} +var Builtins_os = map[string]*env.Builtin{} diff --git a/evaldo/builtins_pipes_not.go b/evaldo/builtins_pipes_not.go index 451bc0e2..ad118251 100644 --- a/evaldo/builtins_pipes_not.go +++ b/evaldo/builtins_pipes_not.go @@ -1,5 +1,5 @@ -//go:build no_devops -// +build no_devops +//go:build no_pipes +// +build no_pipes package evaldo @@ -7,4 +7,4 @@ import ( "github.com/refaktor/rye/env" ) -var Builtins_devops = map[string]*env.Builtin{} +var Builtins_os = map[string]*env.Builtin{} diff --git a/evaldo/repl.go b/evaldo/repl.go index 1e2ef38d..a826add4 100644 --- a/evaldo/repl.go +++ b/evaldo/repl.go @@ -5,7 +5,6 @@ package evaldo import ( "context" "fmt" - "log" "os" "path/filepath" "regexp" @@ -16,8 +15,6 @@ import ( "github.com/refaktor/rye/env" "github.com/refaktor/rye/loader" "github.com/refaktor/rye/util" - - "github.com/refaktor/liner" ) var ( @@ -33,7 +30,7 @@ type ShellEd struct { Return env.Object } -func genPrompt(shellEd *ShellEd, line string) (string, string) { +func genPrompt(shellEd *ShellEd, line string, multiline bool) (string, string) { if shellEd != nil && shellEd.Mode != "" { a := shellEd.Askfor if len(a) > 0 { @@ -49,7 +46,11 @@ func genPrompt(shellEd *ShellEd, line string) (string, string) { if len(line) > 0 { return " ", "" } else { - return "×> ", "" + if multiline { + return " > ", "" + } else { + return "×> ", "" + } } } } @@ -210,6 +211,7 @@ func (r *Repl) evalLine(es *env.ProgramState, code string) string { es.Dialect = env.EyrDialect Eyr_EvalBlock(es, true) } else if r.dialect == "math" { + idxx, _ := es.Idx.GetIndex("math") s1, ok := es.Ctx.Get(idxx) if ok { @@ -229,6 +231,8 @@ func (r *Repl) evalLine(es *env.ProgramState, code string) string { Eyr_EvalBlock(es, false) es.Ser = ser } + } else { + fmt.Println("Unknown dialect: " + r.dialect) } MaybeDisplayFailureOrError(es, genv) @@ -294,6 +298,9 @@ func constructKeyEvent(r rune, k keyboard.Key) util.KeyEvent { case keyboard.KeyCtrlU: ch = "u" ctrl = true + case keyboard.KeyCtrlX: + ch = "x" + ctrl = true case keyboard.KeyEnter: code = 13 @@ -383,6 +390,16 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here fmt.Println(err) } // fmt.Println("END") + + // TODO -- make it save history + /* if f, err := os.Create(history_fn); err != nil { + log.Print("Error writing history file: ", err) + } else { + if _, err := line.WriteHistory(f); err != nil { + log.Print("Error writing history file: ", err) + } + f.Close() + }*/ } /* THIS WAS DISABLED TEMP FOR WASM MODE .. 20250116 func DoGeneralInput(es *env.ProgramState, prompt string) { @@ -406,7 +423,7 @@ func DoGeneralInputField(es *env.ProgramState, prompt string) { } */ -func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of some odd options we were experimentally adding +/* func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of some odd options we were experimentally adding codestr := "a: 100\nb: \"jim\"\nprint 10 + 20 + b" codelines := strings.Split(codestr, ",\n") @@ -444,8 +461,10 @@ func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of var prevResult env.Object + multiline := false + for { - prompt, arg := genPrompt(&shellEd, line2) + prompt, arg := genPrompt(&shellEd, line2, multiline) if code, err := line.Prompt(prompt); err == nil { // strip comment @@ -459,7 +478,7 @@ func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of es.LiveObj.ClearUpdates() es.LiveObj.PsMutex.Unlock() - multiline := len(code) > 1 && code[len(code)-1:] == " " + multiline = len(code) > 1 && code[len(code)-1:] == " " comment := regexp.MustCompile(`\s*;`) line1 := comment.Split(code, 2) //--- just very temporary solution for some comments in repl. Later should probably be part of loader ... maybe? @@ -550,14 +569,14 @@ func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of for _, c := range codelines { fmt.Println(c) } - MoveCursorUp(len(codelines))*/ + MoveCursorUp(len(codelines)) * / } else { log.Print("Error reading line: ", err) break } } - if f, err := os.Create(history_fn); err != nil { + if f, err := os.Create(history_fn); err != nil { log.Print("Error writing history file: ", err) } else { if _, err := line.WriteHistory(f); err != nil { @@ -566,3 +585,4 @@ func DoRyeRepl_OLD(es *env.ProgramState, showResults bool) { // here because of f.Close() } } +*/ diff --git a/examples/os_pipes/main.db b/examples/os_pipes/main.db new file mode 100644 index 0000000000000000000000000000000000000000..bceb748638718e0b4aaed2a00eac0cf919a6b730 GIT binary patch literal 8192 zcmeI#y$ZrG5Ww+E6*s9%alKI&7jd^rw}PmB0mUchV@xc21`<5MyMnDk4#nzI?*?x!bHm3Hx6W$>j0u`%@-+IcyjFy`$Eix_AMV@Ks=t#?hmRDy_wo)B` k|K_#Cb(_bzivR)$Ab