integrated readline
This commit is contained in:
parent
eb85a10415
commit
3c2dde3665
4 changed files with 31 additions and 29 deletions
44
cmd/repl.go
44
cmd/repl.go
|
|
@ -20,16 +20,15 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
"bufio"
|
|
||||||
"strings"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"github.com/chzyer/readline"
|
||||||
"git.callpipe.com/aidan/shs/ast"
|
"git.callpipe.com/aidan/shs/ast"
|
||||||
"git.callpipe.com/aidan/shs/log"
|
"git.callpipe.com/aidan/shs/log"
|
||||||
"git.callpipe.com/aidan/shs/stdlib"
|
"git.callpipe.com/aidan/shs/stdlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
def_prompt string = "λ"
|
def_prompt string = "λ "
|
||||||
)
|
)
|
||||||
|
|
||||||
func setLogLvl() {
|
func setLogLvl() {
|
||||||
|
|
@ -59,44 +58,31 @@ func main() {
|
||||||
ast.InitVarTable(vars)
|
ast.InitVarTable(vars)
|
||||||
ast.SyncTablesWithOSEnviron = true
|
ast.SyncTablesWithOSEnviron = true
|
||||||
|
|
||||||
useHist := false
|
|
||||||
var histFile *os.File
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if hist != "" {
|
|
||||||
useHist = true
|
|
||||||
histFile, err = os.OpenFile(hist, os.O_RDWR|os.O_CREATE, 0755)
|
|
||||||
if err != nil {
|
|
||||||
useHist = false
|
|
||||||
log.Log(log.ERR, "coudlnt open histfile: " + err.Error(), "init")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if prompt == "" {
|
if prompt == "" {
|
||||||
prompt = def_prompt
|
prompt = def_prompt
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: is bufio right for this?
|
rl, err := readline.NewEx(&readline.Config{
|
||||||
reader := bufio.NewReader(os.Stdin)
|
Prompt: prompt,
|
||||||
|
HistoryFile: hist,
|
||||||
|
InterruptPrompt: "^C",
|
||||||
|
})
|
||||||
|
|
||||||
|
defer rl.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
setLogLvl()
|
setLogLvl()
|
||||||
fmt.Print(prompt + " ")
|
text, err := rl.Readline()
|
||||||
text, err := reader.ReadString('\n')
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: replace with a regex
|
|
||||||
text = strings.Replace(text, "\r\n", "", -1)
|
|
||||||
text = strings.Replace(text, "\n", "", -1)
|
|
||||||
|
|
||||||
if useHist {
|
|
||||||
_, err = histFile.Write([]byte(text + "\n"))
|
|
||||||
if err != nil {
|
|
||||||
log.Log(log.DEBUG, "couldnt write to histfile: " + err.Error(), "repl")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
userInput := ast.Lex(text)
|
userInput := ast.Lex(text)
|
||||||
if userInput == nil {
|
if userInput == nil {
|
||||||
// errors handled in Lex
|
// errors handled in Lex
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module git.callpipe.com/aidan/shs
|
module git.callpipe.com/aidan/shs
|
||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
|
require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
|
||||||
|
|
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
|
||||||
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
|
|
@ -114,7 +114,19 @@ func GenFuncTable() ast.FuncTable {
|
||||||
TimesCalled: 0,
|
TimesCalled: 0,
|
||||||
Args: 1,
|
Args: 1,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"exit": &ast.Function{
|
||||||
|
Function: exit,
|
||||||
|
Name: "exit",
|
||||||
|
TimesCalled 0,
|
||||||
|
Args: 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdlib
|
return stdlib
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func exit_shell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||||
|
os.Exit(0)
|
||||||
|
return nil // I hope execution doesnt get here
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue