]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/ansi.py
- Check rate limit using the command line tool. Patch by @stalkr_
[z_archive/twitter.git] / twitter / ansi.py
index ae99ddb786daa09470fc05675bb62ead7c6a9f5d..1537000ec8c4208a57fee9cbadd6cb1f981d07da 100644 (file)
@@ -15,16 +15,17 @@ Support for ANSI colours in command-line client.
 """
 
 import itertools
+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')
 ]
 
@@ -46,20 +47,26 @@ 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]
 
+def cmdReset():
+    ''' Returns the ansi cmd colour for a RESET '''
+    if sys.stdout.isatty():
+        return ESC + "[0m"
+    else:
+        return ""
+
 def cmdColour(colour):
     '''
     Return the ansi cmd colour (i.e. escape sequence)
     for the ansi `colour` value
     '''
-    return ESC + "[" + colour + "m"
-
-def cmdReset():
-    ''' Returns the ansi cmd colour for a RESET '''
-    return cmdColour(RESET)
+    if sys.stdout.isatty():
+        return ESC + "[" + colour + "m"
+    else:
+        return ""
 
 def cmdColourNamed(colour):
     ''' Return the ansi cmdColour for a given named `colour` '''