integrated readline
This commit is contained in:
parent
eb85a10415
commit
3c2dde3665
4 changed files with 31 additions and 29 deletions
42
cmd/repl.go
42
cmd/repl.go
|
|
@ -20,9 +20,8 @@ package main
|
|||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
"github.com/chzyer/readline"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
"git.callpipe.com/aidan/shs/stdlib"
|
||||
|
|
@ -59,44 +58,31 @@ func main() {
|
|||
ast.InitVarTable(vars)
|
||||
ast.SyncTablesWithOSEnviron = true
|
||||
|
||||
useHist := false
|
||||
var histFile *os.File
|
||||
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 == "" {
|
||||
prompt = def_prompt
|
||||
}
|
||||
|
||||
// TODO: is bufio right for this?
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
rl, err := readline.NewEx(&readline.Config{
|
||||
Prompt: prompt,
|
||||
HistoryFile: hist,
|
||||
InterruptPrompt: "^C",
|
||||
})
|
||||
|
||||
defer rl.Close()
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
setLogLvl()
|
||||
fmt.Print(prompt + " ")
|
||||
text, err := reader.ReadString('\n')
|
||||
text, err := rl.Readline()
|
||||
if err != nil {
|
||||
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)
|
||||
if userInput == nil {
|
||||
// errors handled in Lex
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
|||
module git.callpipe.com/aidan/shs
|
||||
|
||||
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,
|
||||
Args: 1,
|
||||
},
|
||||
|
||||
"exit": &ast.Function{
|
||||
Function: exit,
|
||||
Name: "exit",
|
||||
TimesCalled 0,
|
||||
Args: 0,
|
||||
},
|
||||
}
|
||||
|
||||
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