Skip to content

Commit

Permalink
interp: use io.Reader and io.Writer for REPL parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak authored and traefiker committed Oct 8, 2019
1 parent 4f95c27 commit 398b0e0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/build"
"go/scanner"
"go/token"
"io"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -332,7 +333,7 @@ func (interp *Interpreter) Use(values Exports) {

// Repl performs a Read-Eval-Print-Loop on input file descriptor.
// Results are printed on output.
func (interp *Interpreter) Repl(in, out *os.File) {
func (interp *Interpreter) Repl(in io.Reader, out io.Writer) {
s := bufio.NewScanner(in)
prompt := getPrompt(in, out)
prompt()
Expand All @@ -357,9 +358,14 @@ func (interp *Interpreter) Repl(in, out *os.File) {
}
}

// getPrompt returns a function which prints a prompt only if input is a terminal
func getPrompt(in, out *os.File) func() {
if stat, err := in.Stat(); err == nil && stat.Mode()&os.ModeCharDevice != 0 {
// getPrompt returns a function which prints a prompt only if input is a terminal.
func getPrompt(in io.Reader, out io.Writer) func() {
s, ok := in.(interface{ Stat() (os.FileInfo, error) })
if !ok {
return func() {}
}
stat, err := s.Stat()
if err == nil && stat.Mode()&os.ModeCharDevice != 0 {
return func() { fmt.Fprint(out, "> ") }
}
return func() {}
Expand Down

0 comments on commit 398b0e0

Please sign in to comment.