Commit 7f9e9a7

Richard Luby <richluby@gmail.com>
2016-11-02 13:33:00
client now has COLORS
errors and incorrect answers now printed in RED. questions printed in green. categories printed in blue.
1 parent 1a544c3
client.go
@@ -65,12 +65,12 @@ func initUserSession() {
 		}
 		// select a command from the list of available commands
 		if command, err = selectCommand(strings.TrimSpace(args[0])); err != nil {
-			log.Printf("Error while parsing command: %+v", err)
+			log.Printf(COLOR_RED+"Error while parsing command: %+v"+COLOR_RESET, err)
 			continue
 		}
 		// execute the command and check for any errors that may have occurred
 		if err = command.Run(args[1:]); err != nil {
-			log.Printf("Error while executing command: %+v", err)
+			log.Printf(COLOR_RED+"Error while executing command: %+v"+COLOR_RESET, err)
 		}
 	}
 }
command.go
@@ -97,7 +97,8 @@ func runTest(clientTest *ClientTest) {
 	reader := bufio.NewReader(os.Stdin)
 	numCorrect := 0
 	for i, record := range clientTest.Records {
-		fmt.Printf("%d) [%s] %s\n", i+1, categoryKeys[record.Category], record.Question)
+		fmt.Printf("%d) [%s] %s\n", i+1, COLOR_BLUE+categoryKeys[record.Category]+COLOR_RESET,
+			COLOR_GREEN+record.Question+COLOR_RESET)
 		input, _ := reader.ReadString('\n')
 		input = strings.TrimSpace(input)
 		if strings.Compare(input, record.Answer) == 0 {
@@ -108,7 +109,7 @@ func runTest(clientTest *ClientTest) {
 			clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
 			return
 		} else {
-			fmt.Printf("Incorrect. Correct answer was: %s\n", record.Answer)
+			fmt.Printf("Incorrect. Correct answer was: %s\n", COLOR_RED+record.Answer+COLOR_RESET)
 			clientTest.Records[i].ClientAnswer = input
 		}
 	}
@@ -126,7 +127,7 @@ func postRecordsToServer(client *http.Client, recordArray *ClientTest) error {
 	defer resp.Body.Close()
 	response, err := ioutil.ReadAll(resp.Body)
 	if resp.StatusCode != http.StatusCreated {
-		log.Printf("Error sending the results to the server: %s", response)
+		return fmt.Errorf("Error sending the results to the server: %s", response)
 	} else {
 		fmt.Print("\nTest submitted successfully.")
 	}
structures.go
@@ -15,6 +15,14 @@ var recordMap map[string][]Record
 // on category
 var categoryKeys []string
 
+// define colors for printing to terminals
+const (
+	COLOR_GREEN = "\x1b[32m"
+	COLOR_BLUE  = "\x1b[34m"
+	COLOR_RED   = "\x1b[31m"
+	COLOR_RESET = "\x1b[0m"
+)
+
 // EXIT_CODES define exit error codes
 type EXIT_CODES struct {
 	BAD_CONFIG,