Skip to content

Commit

Permalink
solwed reporting terminal width in browser to Go runtime, review one …
Browse files Browse the repository at this point in the history
…day this code. added os/env? builtin
  • Loading branch information
refaktor committed Oct 27, 2024
1 parent 2719b12 commit f2cf572
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
20 changes: 19 additions & 1 deletion evaldo/builtins_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.",
Expand Down
15 changes: 15 additions & 0 deletions evaldo/builtins_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 8 additions & 0 deletions main_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 11 additions & 1 deletion term/getcolumns_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 9 additions & 2 deletions term/microliner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 12 additions & 5 deletions wasm/ryeshell/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
go.run(dat.instance);
InitRyeShell();
console.log("INIT RYE SHELL")
window.SetTerminalSize(term.cols, term.rows);
});


Expand Down Expand Up @@ -254,14 +255,20 @@ <h3 style="font-family: mono; font-weight: normal; font-size: 14px; color: #999;
brightWhite: '#FFFFFF'
}

term = new Terminal({
var term = new Terminal({
windowsMode: ['Windows', 'Win16', 'Win32', 'WinCE'].indexOf(navigator.platform) >= 0,
fontFamily: '"Cascadia Code", Menlo, monospace',
fontSize: 13,
convertEol: true,
rows: 30,
cols: 80,
})


// term.on('resize', (size) => {
// 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)
Expand All @@ -277,14 +284,14 @@ <h3 style="font-family: mono; font-weight: normal; font-size: 14px; color: #999;
})
};
return true;
});
});

// term.open(document.getElementById('terminal-container'))

// var term = new Terminal();
term.open(document.getElementById('terminal'));
term.focus()
term.writeln("Welcome to Rye web console. It's a work in progress. Visit \033[38;5;14mryelang.org\33[0m for more.")
term.writeln("- \033[38;5;246mtype in lsp (list parent context) too see some functions, or ls to see yours \033[0m-")
term.writeln("- \033[38;5;246mtype in lcp (list context parent) too see some functions, or lc to see yours \033[0m-")
term.writeln("--------------------------------------------------------------------------------")

var currLine = "";
Expand Down

0 comments on commit f2cf572

Please sign in to comment.