Commit 7c48e78

Richard Luby <richluby@gmail.com>
2016-12-01 09:34:02
program displays user answer in buffer
client now displays user answer next to the correct answer to allow the user to see what went wrong
1 parent 20c1966
Changed files (1)
command.go
@@ -196,6 +196,34 @@ func setStatusBarForTest(clientTest *ClientTest, currentIndex int) {
 		clientTest.Score, currentIndex+1, len(clientTest.Records)))
 }
 
+// parseUserTestAnswer determines if the user wrote the correct answer,
+// and then updates the view to inform the user
+// returns the number of questions correctly answered, or an error
+func parseUserTestAnswer(input string, record *ClientRecord, clientTest *ClientTest, index int) (int, error) {
+	input = strings.TrimSpace(input)
+	maxX, _ := ApplicationView.MainGui.Size()
+	if strings.Compare(input, record.Answer) == 0 {
+		// dynamically determine width of screen for right-alignment
+		fieldWidth := strconv.Itoa(maxX - ApplicationView.COMMAND_PALLET_WIDTH - len(record.Answer) - 10)
+		displayStringToMainScreen(fmt.Sprintf("%s%"+fieldWidth+"s\n",
+			COLOR_GREEN+record.Answer, "[✓]"+COLOR_RESET))
+		clientTest.Records[index].AnsweredCorrectly = true
+		return 1, nil
+	} else if strings.Compare(input, "exit") == 0 {
+		return 0, fmt.Errorf("Client exited test.")
+	} else {
+		leftString := fmt.Sprintf(COLOR_RED+"%-15s"+COLOR_GREEN+"    Correct Answer: %s",
+			input,
+			record.Answer)
+		rightString := fmt.Sprintf(COLOR_RED+"%"+
+			strconv.Itoa(maxX-ApplicationView.COMMAND_PALLET_WIDTH-len(leftString)-4)+
+			"s\n"+COLOR_RESET, "[X]")
+		displayStringToMainScreen(leftString + rightString)
+		clientTest.Records[index].ClientAnswer = input
+		return 0, nil
+	}
+}
+
 // walks the user through the test questions
 // this function updates the records with the user responses
 func runTest(clientTest *ClientTest) {
@@ -211,24 +239,11 @@ func runTest(clientTest *ClientTest) {
 		if err != nil {
 			log.Printf("Error reading input: %+v", err)
 		}
-		input = strings.TrimSpace(input)
-		maxX, _ := ApplicationView.MainGui.Size()
-		if strings.Compare(input, record.Answer) == 0 {
-			// dynamically determine width of screen for right-alignment
-			fieldWidth := strconv.Itoa(maxX - ApplicationView.COMMAND_PALLET_WIDTH - len(record.Answer) - 10)
-			displayStringToMainScreen(fmt.Sprintf("%s%"+fieldWidth+"s\n",
-				COLOR_GREEN+record.Answer, "[✓]"+COLOR_RESET))
-			clientTest.Records[i].AnsweredCorrectly = true
-			numCorrect++
-		} else if strings.Compare(input, "exit") == 0 {
+		if correct, err := parseUserTestAnswer(input, &record, clientTest, i); err != nil {
 			clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
 			return
 		} else {
-			fieldWidth := strconv.Itoa(maxX - ApplicationView.COMMAND_PALLET_WIDTH - len(record.Answer) - 10)
-			displayStringToMainScreen(fmt.Sprintf("%s%"+fieldWidth+"s\n",
-				COLOR_RED+record.Answer,
-				"[X]"+COLOR_RESET))
-			clientTest.Records[i].ClientAnswer = input
+			numCorrect += correct
 		}
 		clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
 		updateMainView()