diff options
author | Daniel, Dao Quang Minh <dqminh89@gmail.com> | 2017-11-10 16:10:34 +0000 |
---|---|---|
committer | alandonovan <adonovan@google.com> | 2017-11-10 11:10:34 -0500 |
commit | aa0bc8ec376caaff75fa2c8663023ac3c1f52ec3 (patch) | |
tree | 22515382997d09bd1630da5a4ecf107ccc97f4d7 | |
parent | 7b65d16c7a9abf7fdc27e64f929332878143fc69 (diff) | |
download | starlark-go-aa0bc8ec376caaff75fa2c8663023ac3c1f52ec3.tar.gz |
implement readline in repl (#39)
The current skylark repl doesnt have readline capability that is familiar when
using other repl such as python. This leverages github.com/chzyer/readline to
support that
-rw-r--r-- | cmd/skylark/skylark.go | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/cmd/skylark/skylark.go b/cmd/skylark/skylark.go index 8fac681..d236269 100644 --- a/cmd/skylark/skylark.go +++ b/cmd/skylark/skylark.go @@ -31,17 +31,16 @@ package main // This is not necessarily a bug. import ( - "bufio" "bytes" "flag" "fmt" - "io" "log" "os" "runtime/pprof" "sort" "strings" + "github.com/chzyer/readline" "github.com/google/skylark" "github.com/google/skylark/resolve" "github.com/google/skylark/syntax" @@ -115,14 +114,20 @@ func repl() { thread := &skylark.Thread{Load: load} globals := make(skylark.StringDict) - sc := bufio.NewScanner(os.Stdin) + rl, err := readline.New(">>> ") + if err != nil { + printError(err) + return + } + defer rl.Close() outer: for { - io.WriteString(os.Stderr, ">>> ") - if !sc.Scan() { + rl.SetPrompt(">>> ") + line, err := rl.Readline() + if err != nil { break } - line := sc.Text() + if l := strings.TrimSpace(line); l == "" || l[0] == '#' { continue // blank or comment } @@ -155,11 +160,11 @@ outer: var buf bytes.Buffer fmt.Fprintln(&buf, line) for { - io.WriteString(os.Stderr, "... ") - if !sc.Scan() { + rl.SetPrompt("... ") + line, err := rl.Readline() + if err != nil { break outer } - line := sc.Text() if l := strings.TrimSpace(line); l == "" { break // blank } |