changeset 312:466eaec2c076

colorize taken from sqlpython
author catherine@Drou
date Sat, 06 Feb 2010 19:21:44 -0500
parents 54e2dd53ba38
children e9f9dc712651
files cmd2.py setup.py
diffstat 2 files changed, 20 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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("""
--- 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,