Commit 57a69fd

Richard Luby <richluby@gmail.com>
2016-11-15 10:22:46
moved reading user input
reading user input now occurs in one location in preparation for more in-depth ui
1 parent 86ae3b0
clientVisualization.go
@@ -14,6 +14,13 @@ func displayStringToMainScreen(str string) {
 	fmt.Printf("%s", str)
 }
 
+// reads input from the user in the main screen
+func readInputFromMainScreen() (string, error) {
+	reader := bufio.NewReader(os.Stdin)
+	input, err := reader.ReadString('\n')
+	return input, err
+}
+
 // initUserSession starts the interactive prompt for the user
 func initUserSession() {
 	displayStringToMainScreen("Welcome to the question interface. Use 'help' for more information.")
@@ -24,11 +31,9 @@ func initUserSession() {
 	var input string
 	var command Command
 	var err error
-	reader := bufio.NewReader(os.Stdin)
-
 	for {
-		fmt.Printf("\n:> ")
-		input, _ = reader.ReadString('\n')
+		displayStringToMainScreen("\n:> ")
+		input, _ = readInputFromMainScreen()
 		args := strings.Fields(input)
 		if len(args) < 1 { // skip the empty strings
 			continue
command.go
@@ -1,7 +1,6 @@
 package main
 
 import (
-	"bufio"
 	"bytes"
 	"encoding/json"
 	"fmt"
@@ -182,13 +181,12 @@ func calculateScore(numCorrect int, numQuestions int) float32 {
 // walks the user through the test questions
 // this function updates the records with the user responses
 func runTest(clientTest *ClientTest) {
-	reader := bufio.NewReader(os.Stdin)
 	numCorrect := 0
 	for i, record := range clientTest.Records {
 		displayStringToMainScreen(fmt.Sprintf("%d) [%s] %s\n", i+1,
 			COLOR_BLUE+categoryKeys[record.Category]+COLOR_RESET,
 			record.Question))
-		input, _ := reader.ReadString('\n')
+		input, _ := readInputFromMainScreen()
 		input = strings.TrimSpace(input)
 		width, _, _ := terminal.GetSize(int(os.Stdin.Fd()))
 		if strings.Compare(input, record.Answer) == 0 {
@@ -301,8 +299,7 @@ func buildBluePrint(useBlueprint bool) (string, error) {
 			i+1, categoryKeys[i+1]))
 	}
 	displayStringToMainScreen("Enter the category numbers, separated by a comma: ")
-	reader := bufio.NewReader(os.Stdin)
-	input, _ := reader.ReadString('\n')
+	input, _ := readInputFromMainScreen()
 	splits := strings.Split(input, ",")
 	for _, split := range splits {
 		split = strings.TrimSpace(split)