ui.cli: Use pygments to colorize command line output
authorW. Trevor King <wking@tremily.us>
Thu, 7 Feb 2013 21:10:05 +0000 (16:10 -0500)
committerW. Trevor King <wking@tremily.us>
Thu, 7 Feb 2013 21:10:05 +0000 (16:10 -0500)
README
quizzer/ui/cli.py

diff --git a/README b/README
index ae4f8cfae781a2aeb94ee0fae6122e0d06ac5ce0..61cdc6e5f27e4108848f5ba0a1b1e7df22e34f30 100644 (file)
--- a/README
+++ b/README
@@ -71,4 +71,5 @@ The unanswered question (“What is your name?”) wasn't asked because
 the user successfully answered the question that depended on it (“What
 is your quest?”).
 
-Quizzer requires Python ≥ 3.3.
+Quizzer requires Python ≥ 3.3.  If Pygments is installed, the command
+line prompt will be colored.
index b799e1e209ecc74879f323e31521bfeddf8ce36f..4663077a29fbec2c6be81532707e2a4dc969aafd 100644 (file)
@@ -20,6 +20,13 @@ try:
 except ImportError as _readline_import_error:
     _readline = None
 
+try:
+    from pygments.console import colorize as _colorize
+except ImportError as e:
+    def _colorize(color_key=None, text=None):
+        return text
+    print(e)
+
 from . import UserInterface
 
 
@@ -48,13 +55,15 @@ class QuestionCommandLine (_cmd.Cmd):
         "Pose a question and prompt"
         if self.question:
             self.prompt = '\n{}\n{}'.format(
-                self.question.format_prompt(), self._prompt)
+                _colorize(
+                    self.ui.colors['question'], self.question.format_prompt()),
+                _colorize(self.ui.colors['prompt'], self._prompt))
         else:
-            self.prompt = self._prompt
+            self.prompt = _colorize(self.ui.colors['prompt'], self._prompt)
 
     def _set_ps2(self):
         "Just prompt (without the question, e.g. for multi-line answers)"
-        self.prompt = self._prompt
+        self.prompt = _colorize(self.ui.colors['prompt'], self._prompt)
 
     def default(self, line):
         self.answers.append(line)
@@ -75,9 +84,9 @@ class QuestionCommandLine (_cmd.Cmd):
             answer = ''
         correct = self.ui.process_answer(question=self.question, answer=answer)
         if correct:
-            print('correct\n')
+            print(_colorize(self.ui.colors['correct'], 'correct\n'))
         else:
-            print('incorrect\n')
+            print(_colorize(self.ui.colors['incorrect'], 'incorrect\n'))
         self.question = self.ui.get_question()
         if not self.question:
             return True  # out of questions
@@ -111,6 +120,14 @@ class QuestionCommandLine (_cmd.Cmd):
 
 
 class CommandLineInterface (UserInterface):
+    colors = {  # listed in pygments.console.light_colors
+        'question': 'turquoise',
+        'prompt': 'blue',
+        'correct': 'green',
+        'incorrect': 'red',
+        'result': 'fuchsia',
+        }
+
     def run(self):
         if not self.stack:
             return
@@ -119,7 +136,7 @@ class CommandLineInterface (UserInterface):
         print()
 
     def display_results(self):
-        print('results:')
+        print(_colorize(self.colors['result'], 'results:'))
         for question in self.quiz:
             if question.id in self.answers:
                 self.display_result(question=question)
@@ -129,7 +146,10 @@ class CommandLineInterface (UserInterface):
     def display_result(self, question):
         answers = self.answers.get(question.id, [])
         print('question:')
-        print('  {}'.format(question.format_prompt(newline='\n  ')))
+        print('  {}'.format(
+            _colorize(
+                self.colors['question'],
+                question.format_prompt(newline='\n  '))))
         la = len(answers)
         lc = len([a for a in answers if a['correct']])
         print('answers: {}/{} ({:.2f})'.format(lc, la, float(lc)/la))
@@ -138,6 +158,7 @@ class CommandLineInterface (UserInterface):
                 correct = 'correct'
             else:
                 correct = 'incorrect'
+            correct = _colorize(self.colors[correct], correct)
             print('  you answered: {}'.format(answer['answer']))
             print('     which was: {}'.format(correct))