Commits

Steve Losh committed 0fd010a

Add colorization with tput (off by default).

Comments (0)

Files changed (2)

         elif answer and answer in answers.lower():
             return answer
 
-def log(msg=None, verbosemsg=None, verbose=False):
+RED = '1'
+GREEN = '2'
+YELLOW = '3'
+
+def set_color(code):
+    subprocess.call(['tput', 'setaf', code])
+
+def set_bold():
+    subprocess.call(['tput', 'bold'])
+
+def reset_colors():
+    subprocess.call(['tput', 'sgr0'])
+
+def log(msg=None, verbosemsg=None, verbose=False, color=False):
     """Write msg to standard out and flush.
 
     If verbose is True, write verbosemsg instead.
     if verbose:
         msg = verbosemsg
     if msg:
+        if color:
+            if msg.startswith('+'):
+                set_bold()
+                set_color(GREEN)
+            elif msg.startswith('-'):
+                set_bold()
+                set_color(RED)
+            elif msg.startswith('@@'):
+                set_bold()
+                set_color(YELLOW)
+            elif msg.startswith('# Ran'):
+                set_bold()
         sys.stdout.write(msg)
         sys.stdout.flush()
+        if color:
+            reset_colors()
 
 def patch(cmd, diff):
     """Run echo [lines from diff] | cmd -p0"""
     return p.returncode == 0
 
 def run(paths, tmpdir, quiet=False, verbose=False, patchcmd=None, answer=None,
-        indent=2):
+        indent=2, color=False):
     """Run tests in paths in tmpdir.
 
     If quiet is True, diffs aren't printed. If verbose is True,
                     if patchcmd:
                         diff = list(diff)
                     for line in diff:
-                        log(line)
+                        log(line, color=color)
                     if (patchcmd and
                         prompt('Accept this change?', 'yN', answer) == 'y'):
                         if patch(patchcmd, diff):
                             log('%s: merge failed\n' % path)
     log('\n', None, verbose)
     log('# Ran %s tests, %s skipped, %s failed.\n'
-        % (len(seen), skipped, failed))
+        % (len(seen), skipped, failed),
+        color=color)
     return bool(failed)
 
 def which(cmd):
                  type='int', help='number of spaces to use for indentation')
     p.add_option('-E', action='store_false', dest='sterilize', default=True,
                  help="don't reset common environment variables")
+    p.add_option('--color', dest='color', action='store_true', default=False,
+                 help="colorize output (requires tput)")
+    p.add_option('--no-color', dest='color', action='store_false',
+                 help="don't colorize output")
     opts, paths = p.parse_args(args)
 
     if opts.version:
 
     try:
         return run(paths, tmpdir, opts.quiet, opts.verbose, patchcmd, answer,
-                   opts.indent)
+                   opts.indent, opts.color)
     finally:
         if opts.keep_tmpdir:
             log('# Kept temporary directory: %s\n' % tmpdir)
     --keep-tmpdir      keep temporary directories
     --indent=NUM       number of spaces to use for indentation
     -E                 don't reset common environment variables
+    --color            colorize output (requires tput)
+    --no-color         don't colorize output
   $ cram -V
   Cram CLI testing framework (version 0.5)