integrated readline

This commit is contained in:
Aidan 2020-06-27 21:27:33 -07:00
parent eb85a10415
commit 3c2dde3665
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 31 additions and 29 deletions

View file

@ -20,16 +20,15 @@ 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"
)
const (
def_prompt string = "
def_prompt string = "
)
func setLogLvl() {
@ -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