# HG changeset patch # User catherine@Drou # Date 1265502104 18000 # Node ID 466eaec2c0766a6053700a9082f4c54c44207017 # Parent 54e2dd53ba3864447befd8bafa993920c76871bf colorize taken from sqlpython diff -r 54e2dd53ba38 -r 466eaec2c076 cmd2.py --- a/cmd2.py Sun Jan 31 08:18:15 2010 -0500 +++ b/cmd2.py Sat Feb 06 19:21:44 2010 -0500 @@ -36,6 +36,7 @@ import urllib import glob import traceback +import platform from code import InteractiveConsole, InteractiveInterpreter, softspace from optparse import make_option @@ -44,7 +45,7 @@ else: import pyparsing -__version__ = '0.5.6' +__version__ = '0.6.0' class OptionParser(optparse.OptionParser): def exit(self, status=0, msg=None): @@ -337,6 +338,7 @@ debug = True settable = ''' prompt + colors Colorized output (*nix only) continuation_prompt debug default_file_name for `save`, `load`, etc. @@ -375,7 +377,22 @@ for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: if not os.system('which %s' % (editor)): break - + + colorcodes = {'bold':{True:'\x1b[1m',False:'\x1b[22m'}, + 'cyan':{True:'\x1b[36m',False:'\x1b[39m'}, + 'blue':{True:'\x1b[34m',False:'\x1b[39m'}, + 'red':{True:'\x1b[31m',False:'\x1b[39m'}, + 'magenta':{True:'\x1b[35m',False:'\x1b[39m'}, + 'green':{True:'\x1b[32m',False:'\x1b[39m'}, + 'underline':{True:'\x1b[4m',False:'\x1b[24m'}} + colors = (platform.system() != 'Windows') + def colorize(self, val, color): + '''Wraps `val` in `color` markers (one of: red/blue/green/cyan/magenta, bold, underline), + but only if `colors` environment variable is True''' + if self.colors and (self.stdout == self.initial_stdout): + return self.colorcodes[color][True] + val + self.colorcodes[color][False] + return val + def do_cmdenvironment(self, args): '''Summary report of interactive parameters.''' self.stdout.write(""" diff -r 54e2dd53ba38 -r 466eaec2c076 setup.py --- a/setup.py Sun Jan 31 08:18:15 2010 -0500 +++ b/setup.py Sat Feb 06 19:21:44 2010 -0500 @@ -10,7 +10,7 @@ setup( name="cmd2", - version="0.5.6", + version="0.6.0", py_modules=["cmd2",], use_2to3=True,