From c69e9f604c47469429a6a8a27ba7ef873134e0a1 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 2 May 2017 23:05:09 +0200 Subject: initial implementation of "rtree get" command --- pkg/util/util.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'pkg/util/util.go') diff --git a/pkg/util/util.go b/pkg/util/util.go index 3c5e724..8d2b0bf 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -21,6 +21,7 @@ package util import ( "bufio" "fmt" + "io" "os" "sort" "strings" @@ -57,12 +58,12 @@ var stdin = bufio.NewReader(os.Stdin) // // choice := Prompt("(y)es or (n)o", []string{"y","n"}) // //choice is either "y" or "n" -func Prompt(question string, answers []string) string { +func Prompt(out io.Writer, question string, answers []string) string { for idx, answer := range answers { answers[idx] = strings.ToLower(answer) } - os.Stdout.Write([]byte(">> " + strings.TrimSpace(question) + " ")) + out.Write([]byte(">> " + strings.TrimSpace(question) + " ")) for { input, err := stdin.ReadString('\n') FatalIfError(err) @@ -74,17 +75,24 @@ func Prompt(question string, answers []string) string { } //user typed gibberish - ask again - os.Stdout.Write([]byte("Please type ")) + out.Write([]byte("Please type ")) for idx, answer := range answers { if idx > 0 { if idx == len(answers)-1 { - os.Stdout.Write([]byte(" or ")) + out.Write([]byte(" or ")) } else { - os.Stdout.Write([]byte(", ")) + out.Write([]byte(", ")) } } - os.Stdout.Write([]byte("'" + answer + "'")) + out.Write([]byte("'" + answer + "'")) } - os.Stdout.Write([]byte(": ")) + out.Write([]byte(": ")) } } + +//ReadLine reads a line from stdin, with whitespace already trimmed. +func ReadLine() string { + input, err := stdin.ReadString('\n') + FatalIfError(err) + return strings.TrimSpace(input) +} -- cgit v1.3.1