changeset 337:8fbf49d3abe8

colors
author Catherine Devlin <catherine.devlin@gmail.com>
date Thu, 09 Apr 2009 00:14:22 -0400
parents 1cde0ec62e61
children a8835fe129f6
files sqlpython/mysqlpy.py sqlpython/sqlpyPlus.py sqlpython/sqlpython.py
diffstat 3 files changed, 79 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/mysqlpy.py	Tue Apr 07 22:23:49 2009 -0400
+++ b/sqlpython/mysqlpy.py	Thu Apr 09 00:14:22 2009 -0400
@@ -154,7 +154,7 @@
             desc = self.curs.description
             self.rc = self.curs.rowcount
             if self.rc > 0:
-                self.poutput('\n' + sqlpython.pmatrix(rows,desc,200))
+                self.poutput('\n' + self.pmatrix(rows,desc,200))
         except Exception, e:
             self.perror(e)
 
--- a/sqlpython/sqlpyPlus.py	Tue Apr 07 22:23:49 2009 -0400
+++ b/sqlpython/sqlpyPlus.py	Thu Apr 09 00:14:22 2009 -0400
@@ -316,7 +316,7 @@
     def __init__(self):
         sqlpython.sqlpython.__init__(self)
         self.binds = CaselessDict()
-        self.settable += 'autobind commit_on_exit maxfetch maxtselctrows rows_remembered scan serveroutput sql_echo timeout heading wildsql'.split()
+        self.settable += 'autobind colors commit_on_exit maxfetch maxtselctrows rows_remembered scan serveroutput sql_echo timeout heading wildsql'.split()
         self.settable.remove('case_insensitive')
         self.settable.sort()
         self.stdoutBeforeSpool = sys.stdout
@@ -331,7 +331,6 @@
         self.substvars = {}
         self.result_history = []
         self.rows_remembered = 10000
-        
         self.pystate = {'r': [], 'binds': self.binds, 'substs': self.substvars}
         
     # overrides cmd's parseline
@@ -463,7 +462,7 @@
                     transpr[x] = map(binascii.b2a_hex, transpr[x])
                     transpr[x][0] = rname
             newdesc[0][0] = 'COLUMN NAME'
-            result = '\n' + sqlpython.pmatrix(transpr,newdesc)            
+            result = '\n' + self.pmatrix(transpr,newdesc)            
         elif outformat in ('\\l', '\\L', '\\p', '\\b'):
             plot = Plot()
             plot.build(self, outformat)
@@ -471,9 +470,9 @@
             plot.draw()
             return ''
         else:
-            result = sqlpython.pmatrix(self.rows, self.curs.description, 
-                                       self.maxfetch, heading=self.heading, 
-                                       restructuredtext = (outformat == '\\r'))
+            result = self.pmatrix(self.rows, self.curs.description, 
+                                  self.maxfetch, heading=self.heading, 
+                                  restructuredtext = (outformat == '\\r'))
         return result
         
     legalOracle = re.compile('[a-zA-Z_$#]')
--- a/sqlpython/sqlpython.py	Tue Apr 07 22:23:49 2009 -0400
+++ b/sqlpython/sqlpython.py	Thu Apr 09 00:14:22 2009 -0400
@@ -269,58 +269,78 @@
         return cmd2.Cmd.do_quit(self, None)
     do_exit = do_quit
     do_q = do_quit
+    colorcodes = {'bold':{True:'\x1b[1m',False:'\x1b[22m'},
+                  'red':{True:'\x1b[36m',False:'\x1b[39m'},
+                  'cyan':{True:'\x1b[31m',False:'\x1b[39m'},
+                  'underline':{True:'\x1b[4m',False:'\x1b[24m'}}
+    colors = True
+    def colorize(self, val, color):
+        if self.colors and (self.stdout == self.initial_stdout):
+            if color not in self.colorcodes:
+                if (color % 2):
+                    color = 'red'
+                else:
+                    color = 'cyan'
+            return self.colorcodes[color][True] + val + self.colorcodes[color][False]        
+        return val
+    def pmatrix(self,rows,desc,maxlen=30,heading=True,restructuredtext=False):
+        '''prints a matrix, used by sqlpython to print queries' result sets'''
+        names = []
+        maxen = []
+        toprint = []
+        for d in desc:
+            n = d[0]
+            names.append(n)      # list col names
+            maxen.append(len(n)) # col length
+        rcols = range(len(desc))
+        rrows = range(len(rows))
+        for i in rrows:          # loops for all rows
+            rowsi = map(str, rows[i]) # current row to process
+            split = []                # service var is row split is needed
+            mustsplit = 0             # flag 
+            for j in rcols:
+                if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>":  # handles RAW columns
+                    rowsi[j] = binascii.b2a_hex(rowsi[j])
+                maxen[j] = max(maxen[j], len(rowsi[j]))    # computes max field length
+                if maxen[j] <= maxlen:
+                    split.append('')
+                else:                    # split the line is 2 because field is too long
+                    mustsplit = 1   
+                    maxen[j] = maxlen
+                    split.append(rowsi[j][maxlen-1:2*maxlen-1])
+                    rowsi[j] = rowsi[j][0:maxlen-1] # this implem. truncates after maxlen*2
+            toprint.append(rowsi)        # 'toprint' is a printable copy of rows
+            if mustsplit != 0:
+                toprint.append(split)
+        sepcols = []
+        for i in rcols:
+            maxcol = maxen[i]
+            name = names[i]
+            sepcols.append("-" * maxcol)  # formats column names (header)
+            names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
+            rrows2 = range(len(toprint))
+            for j in rrows2:
+                val = toprint[j][i]
+                if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>":  # right align numbers
+                    toprint[j][i] = (" " * (maxcol-len(val))) + val
+                else:
+                    toprint[j][i] = val + (" " * (maxcol-len(val)))
+                toprint[j][i] = self.colorize(toprint[j][i], i)
+        for j in rrows2:
+            toprint[j] = ' '.join(toprint[j])
+        names = [self.colorize(name, n) for (n, name) in enumerate(names)]
+        names = ' '.join(names)
+        names = self.colorize(names, 'bold')
+        sepcols = ' '.join(sepcols)
+        if heading or restructuredtext:
+            toprint.insert(0, sepcols)
+            toprint.insert(0, names)
+        if restructuredtext:
+            toprint.insert(0, sepcols)
+            toprint.append(sepcols)
+        return '\n'.join(toprint)
     
-def pmatrix(rows,desc,maxlen=30,heading=True,restructuredtext=False):
-    '''prints a matrix, used by sqlpython to print queries' result sets'''
-    names = []
-    maxen = []
-    toprint = []
-    for d in desc:
-        n = d[0]
-        names.append(n)      # list col names
-        maxen.append(len(n)) # col length
-    rcols = range(len(desc))
-    rrows = range(len(rows))
-    for i in rrows:          # loops for all rows
-        rowsi = map(str, rows[i]) # current row to process
-        split = []                # service var is row split is needed
-        mustsplit = 0             # flag 
-        for j in rcols:
-            if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>":  # handles RAW columns
-                rowsi[j] = binascii.b2a_hex(rowsi[j])
-            maxen[j] = max(maxen[j], len(rowsi[j]))    # computes max field length
-            if maxen[j] <= maxlen:
-                split.append('')
-            else:                    # split the line is 2 because field is too long
-                mustsplit = 1   
-                maxen[j] = maxlen
-                split.append(rowsi[j][maxlen-1:2*maxlen-1])
-                rowsi[j] = rowsi[j][0:maxlen-1] # this implem. truncates after maxlen*2
-        toprint.append(rowsi)        # 'toprint' is a printable copy of rows
-        if mustsplit != 0:
-            toprint.append(split)
-    sepcols = []
-    for i in rcols:
-        maxcol = maxen[i]
-        name = names[i]
-        sepcols.append("-" * maxcol)  # formats column names (header)
-        names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
-        rrows2 = range(len(toprint))
-        for j in rrows2:
-            val = toprint[j][i]
-            if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>":  # right align numbers
-                toprint[j][i] = (" " * (maxcol-len(val))) + val
-            else:
-                toprint[j][i] = val + (" " * (maxcol-len(val)))
-    for j in rrows2:
-        toprint[j] = ' '.join(toprint[j])
-    names = ' '.join(names)
-    sepcols = ' '.join(sepcols)
-    if heading or restructuredtext:
-        toprint.insert(0, sepcols)
-        toprint.insert(0, names)
-    if restructuredtext:
-        toprint.insert(0, sepcols)
-        toprint.append(sepcols)
-    return '\n'.join(toprint)
+    
+    
+