Skip to content

Commit

Permalink
Merge pull request #385 from refaktor/allin
Browse files Browse the repository at this point in the history
xterm.js number of columns ... os/env?  function new
  • Loading branch information
refaktor authored Oct 27, 2024
2 parents b17a961 + f2cf572 commit 4736ecc
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 15 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
49 changes: 43 additions & 6 deletions evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,57 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here
// #TODO don't display more than N words
// #TODO make current word bold

// fmt.Println("****")
// # TRICK: we don't have the cursor position, but the caller code handles that already so we can suggest in the middle
suggestions := make([]string, 0)
var wordpart string
spacePos := strings.LastIndex(line, " ")
var prefix string
if spacePos < 0 {
fmt.Println("*")
wordpart = line
prefix = ""
} else {
wordpart = strings.TrimSpace(line[spacePos:])
fmt.Print("=(")
fmt.Print(wordpart)
fmt.Print(")=")
prefix = line[0:spacePos] + " "
if wordpart == "" { // we are probably 1 space after last word
fmt.Println("+")
return
}
}

fmt.Print("=[")
fmt.Print(wordpart)
fmt.Print("]=")
switch mode {
case 0:
for i := 0; i < es.Idx.GetWordCount(); i++ {
// fmt.Print(es.Idx.GetWord(i))
if strings.HasPrefix(es.Idx.GetWord(i), strings.ToLower(line)) {
c = append(c, es.Idx.GetWord(i))
if strings.HasPrefix(es.Idx.GetWord(i), strings.ToLower(wordpart)) {
c = append(c, prefix+es.Idx.GetWord(i))
suggestions = append(suggestions, es.Idx.GetWord(i))
} else if strings.HasPrefix("."+es.Idx.GetWord(i), strings.ToLower(wordpart)) {
c = append(c, prefix+"."+es.Idx.GetWord(i))
suggestions = append(suggestions, es.Idx.GetWord(i))
} else if strings.HasPrefix("|"+es.Idx.GetWord(i), strings.ToLower(wordpart)) {
c = append(c, prefix+"|"+es.Idx.GetWord(i))
suggestions = append(suggestions, es.Idx.GetWord(i))
}
}
case 1:
for key := range es.Ctx.GetState() {
// fmt.Print(es.Idx.GetWord(i))
if strings.HasPrefix(es.Idx.GetWord(key), strings.ToLower(line)) {
c = append(c, es.Idx.GetWord(key))
if strings.HasPrefix(es.Idx.GetWord(key), strings.ToLower(wordpart)) {
c = append(c, prefix+es.Idx.GetWord(key))
suggestions = append(suggestions, es.Idx.GetWord(key))
} else if strings.HasPrefix("."+es.Idx.GetWord(key), strings.ToLower(wordpart)) {
c = append(c, prefix+"."+es.Idx.GetWord(key))
suggestions = append(suggestions, es.Idx.GetWord(key))
} else if strings.HasPrefix("|"+es.Idx.GetWord(key), strings.ToLower(wordpart)) {
c = append(c, prefix+"|"+es.Idx.GetWord(key))
suggestions = append(suggestions, es.Idx.GetWord(key))
}
}
}
Expand All @@ -403,7 +440,7 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here

// Print something
term.ColorMagenta()
fmt.Print(c)
fmt.Print(suggestions)
term.CloseProps() // fmt.Print("This is the new line.")

// Move the cursor back to the previous line
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 4736ecc

Please sign in to comment.