Commit 3f2ac73

Richard Luby <richluby@gmail.com>
2016-11-09 15:13:24
client displays results of test
client can display results of individual test given a test number. categories stored with the test may not be accurate, especially if categories have changed indices since the test was taken
1 parent d5aed80
Changed files (1)
command.go
@@ -24,7 +24,8 @@ var commandArray = []Command{Command{Command: "exit",
 		return nil
 	}},
 	Command{Command: "score",
-		Description: "Display the score for the user.",
+		Usage:       "score [n]",
+		Description: "Display the score for the user. If supplied, the number displays the selected test.",
 		Run:         getScoreFromServer},
 	Command{Command: "test",
 		Usage: "test [flags] [n]",
@@ -116,6 +117,23 @@ func displayTests(clientTests *[]ClientTest) {
 		numCorrect/numQuestions*100, hardRule, numQuestions, numCorrect, hardRule)
 }
 
+// displayTest displays a single test to the user
+func displayTest(clientTest ClientTest) {
+	fmt.Printf("Questions: %3d\nScore: %5.2f%%\n",
+		len(clientTest.Records), clientTest.Score)
+	//	width, _, _ := terminal.GetSize(int(os.Stdin.Fd()))
+	for i, record := range clientTest.Records {
+		fmt.Printf("%2d) %s", i, record.Question)
+		answerLine := fmt.Sprintf("\nCorrect: %-25s Answer: %-25s",
+			COLOR_GREEN+record.Answer+COLOR_RESET, COLOR_RED+record.ClientAnswer+COLOR_RESET)
+		if record.AnsweredCorrectly {
+			fmt.Printf("%-40s%15s\n", answerLine, COLOR_GREEN+"[✓]"+COLOR_RESET)
+		} else {
+			fmt.Printf("%-40s%15s\n", answerLine, COLOR_RED+"[X]"+COLOR_RESET)
+		}
+	}
+}
+
 // getScoreFromServer retrieves the previous tests for this user
 // from the server
 func getScoreFromServer(command Command) error {
@@ -131,8 +149,15 @@ func getScoreFromServer(command Command) error {
 	if err != nil {
 		return fmt.Errorf("Error while parsing request: %+v", err)
 	}
-	fmt.Printf("Pulled %d tests from server.", len(clientTests))
-	displayTests(&clientTests)
+	if len(command.PositionalParameters) < 1 {
+		displayTests(&clientTests)
+	} else {
+		testNum, err := strconv.Atoi(command.PositionalParameters[0])
+		if err != nil || len(clientTests) < testNum {
+			return fmt.Errorf("Error while selecting test number %+v", err)
+		}
+		displayTest(clientTests[testNum])
+	}
 	return nil
 }
 
@@ -168,7 +193,6 @@ func runTest(clientTest *ClientTest) {
 		}
 	}
 	clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
-	return
 }
 
 // postRecordsToServer sends the client responses back to the server