]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/ansi.py
Complete tildes fix for python 2
[z_archive/twitter.git] / twitter / ansi.py
index 0a8456444987783b817e99efa44974f3ad0a541e..0ae2071cef4c843bb4d15ddf1d43decb06ac40f9 100644 (file)
@@ -20,12 +20,12 @@ import sys
 ESC = chr(0x1B)
 RESET = "0"
 
-COLOURS_NAMED = dict(zip(
+COLOURS_NAMED = dict(list(zip(
     ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'],
     [str(x) for x in range(30, 38)]
-))
+)))
 COLOURS_MIDS = [
-    colour for name, colour in COLOURS_NAMED.items()
+    colour for name, colour in list(COLOURS_NAMED.items())
     if name not in ('black', 'white')
 ]
 
@@ -47,30 +47,56 @@ class ColourMap(object):
         Returns an ansi colour value given a `string`.
         The same ansi colour value is always returned for the same string
         '''
-        if not self._cmap.has_key(string):
-            self._cmap[string] = self._colourIter.next()
+        if string not in self._cmap:
+            self._cmap[string] = next(self._colourIter)
         return self._cmap[string]
 
+class AnsiCmd(object):
+    def __init__(self, forceAnsi):
+        self.forceAnsi = forceAnsi
+
+    def cmdReset(self):
+        ''' Returns the ansi cmd colour for a RESET '''
+        if sys.stdout.isatty() or self.forceAnsi:
+            return ESC + "[0m"
+        else:
+            return ""
+
+    def cmdColour(self, colour):
+        '''
+        Return the ansi cmd colour (i.e. escape sequence)
+        for the ansi `colour` value
+        '''
+        if sys.stdout.isatty() or self.forceAnsi:
+            return ESC + "[" + colour + "m"
+        else:
+            return ""
+
+    def cmdColourNamed(self, colour):
+        ''' Return the ansi cmdColour for a given named `colour` '''
+        try:
+            return self.cmdColour(COLOURS_NAMED[colour])
+        except KeyError:
+            raise AnsiColourException('Unknown Colour %s' % (colour))
+
+    def cmdBold(self):
+        if sys.stdout.isatty() or self.forceAnsi:
+            return ESC + "[1m"
+        else:
+            return ""
+
+    def cmdUnderline(self):
+        if sys.stdout.isatty() or self.forceAnsi:
+            return ESC + "[4m"
+        else:
+            return ""
+
+"""These exist to maintain compatibility with users of version<=1.9.0"""
 def cmdReset():
-    ''' Returns the ansi cmd colour for a RESET '''
-    if sys.stdout.isatty():
-        return ESC + "[0m"
-    else:
-        return ""
+    return AnsiCmd(False).cmdReset()
 
 def cmdColour(colour):
-    '''
-    Return the ansi cmd colour (i.e. escape sequence)
-    for the ansi `colour` value
-    '''
-    if sys.stdout.isatty():
-        return ESC + "[" + colour + "m"
-    else:
-        return ""
+    return AnsiCmd(False).cmdColour(colour)
 
 def cmdColourNamed(colour):
-    ''' Return the ansi cmdColour for a given named `colour` '''
-    try:
-        return cmdColour(COLOURS_NAMED[colour])
-    except KeyError:
-        raise AnsiColourException('Unknown Colour %s' %(colour))
+    return AnsiCmd(False).cmdColourNamed(colour)