From f2cf57273bdfdf142616b02bf9eed5437bbf32a2 Mon Sep 17 00:00:00 2001 From: refaktor Date: Mon, 28 Oct 2024 00:53:49 +0100 Subject: [PATCH] solwed reporting terminal width in browser to Go runtime, review one day this code. added os/env? builtin --- evaldo/builtins_os.go | 20 +++++++++++++++++++- evaldo/builtins_term.go | 15 +++++++++++++++ main_wasm.go | 8 ++++++++ term/getcolumns_wasm.go | 12 +++++++++++- term/microliner.go | 11 +++++++++-- wasm/ryeshell/index.html | 17 ++++++++++++----- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/evaldo/builtins_os.go b/evaldo/builtins_os.go index 1ffcfac0..2734805a 100644 --- a/evaldo/builtins_os.go +++ b/evaldo/builtins_os.go @@ -37,7 +37,7 @@ func FileExists(filePath string) int { var Builtins_os = map[string]*env.Builtin{ - "cwd": { + "cwd?": { Argsn: 0, Doc: "Returns current working directory.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { @@ -67,6 +67,24 @@ var Builtins_os = map[string]*env.Builtin{ }, }, + "env?": { + Argsn: 1, + Doc: "Gets the environment variable.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch variable_name := arg0.(type) { + case env.String: + + val, ok := os.LookupEnv(variable_name.Value) + if !ok { + return MakeBuiltinError(ps, "Variable couldn't be read", "env?") + } + return env.NewString(val) + default: + return MakeArgError(ps, 1, []env.Type{env.StringType}, "env?") + } + }, + }, + /* "cd_": { Argsn: 1, Doc: "Changes current directory.", diff --git a/evaldo/builtins_term.go b/evaldo/builtins_term.go index dfc549a8..b8c491fe 100644 --- a/evaldo/builtins_term.go +++ b/evaldo/builtins_term.go @@ -4,12 +4,27 @@ package evaldo import ( + "os" + "github.com/refaktor/rye/env" "github.com/refaktor/rye/term" + goterm "golang.org/x/term" ) var Builtins_term = map[string]*env.Builtin{ + "width?": { + Argsn: 0, + Doc: "Get the terminal width", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + fd := int(os.Stdout.Fd()) + width, _, err := goterm.GetSize(fd) + if err != nil { + return MakeBuiltinError(ps, err.Error(), "width?") + } + return env.NewInteger(int64(width)) + }, + }, "red": { Argsn: 0, Doc: "Take input from a user.", diff --git a/main_wasm.go b/main_wasm.go index e8032818..1dc03ce3 100755 --- a/main_wasm.go +++ b/main_wasm.go @@ -87,6 +87,8 @@ func main() { js.Global().Set("InitRyeShell", js.FuncOf(InitRyeShell)) + js.Global().Set("SetTerminalSize", js.FuncOf(SetTerminalSize)) + js.Global().Set("SendKeypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} { if len(args) > 0 { cc := term.NewKeyEvent(args[0].String(), args[1].Int(), args[2].Bool(), args[3].Bool(), args[4].Bool()) @@ -115,6 +117,12 @@ func main() { } +func SetTerminalSize(this js.Value, args []js.Value) any { + term.SetTerminalColumns(args[0].Int()) + ml.SetColumns(term.GetTerminalColumns()) + return "Ok" +} + func InitRyeShell(this js.Value, args []js.Value) any { // subc := false // fmt.Println("INITIALIZATION") diff --git a/term/getcolumns_wasm.go b/term/getcolumns_wasm.go index 60fdb6da..c67dfcfd 100644 --- a/term/getcolumns_wasm.go +++ b/term/getcolumns_wasm.go @@ -3,6 +3,16 @@ package term +var ColumnNum int + func GetTerminalColumns() int { - return 50 + if ColumnNum == 0 { + return 60 + } else { + return ColumnNum + } +} + +func SetTerminalColumns(c int) { + ColumnNum = c } diff --git a/term/microliner.go b/term/microliner.go index f28ccadd..673ba64a 100644 --- a/term/microliner.go +++ b/term/microliner.go @@ -287,8 +287,15 @@ func NewMicroLiner(ch chan KeyEvent, sb func(msg string), el func(line string) s func (s *MLState) getColumns() bool { s.columns = GetTerminalColumns() - // fmt.Print("*getColumns* : ") - // fmt.Println(s.columns) + fmt.Print("*getColumns* : ") + fmt.Println(s.columns) + return true +} + +func (s *MLState) SetColumns(cols int) bool { + s.columns = cols + fmt.Print("*setColumns* : ") + fmt.Println(s.columns) return true } diff --git a/wasm/ryeshell/index.html b/wasm/ryeshell/index.html index 2f84c3b1..9673bc03 100644 --- a/wasm/ryeshell/index.html +++ b/wasm/ryeshell/index.html @@ -162,6 +162,7 @@ go.run(dat.instance); InitRyeShell(); console.log("INIT RYE SHELL") + window.SetTerminalSize(term.cols, term.rows); }); @@ -254,14 +255,20 @@

{ + // const { cols, rows } = size; + // console.log('Terminal resized to:', cols, 'columns and', rows, 'rows'); + // }); + // term.setOption('theme', theme) term.options.theme = theme; // term.setOption('theme', theme) @@ -277,14 +284,14 @@