]> jfr.im git - z_archive/twitter.git/commitdiff
Bugfix for ansi mode and non-tty output. (Patch by Rainer M. Schmid)
authorMike Verdone <redacted>
Thu, 2 Apr 2009 01:54:12 +0000 (19:54 -0600)
committerMike Verdone <redacted>
Thu, 2 Apr 2009 01:54:12 +0000 (19:54 -0600)
Bugfix: when redirecting to a file (not a tty) neuter the ansi formatter
functionality to prevent garbage in file. Also, use utf-8 encoding when
redirecting to file.

twitter/ansi.py
twitter/cmdline.py

index 870dec2b5720fe3e98dd20a3adacb328eec7fc54..7b8f6dbd465c4762e5526c845aec68a44752049e 100644 (file)
@@ -4,6 +4,7 @@ Support for ANSI colours in command-line client.
 """
 
 import itertools
+import sys
 
 ESC = chr(0x1B)
 RESET = "0"
@@ -21,7 +22,13 @@ class ColourMap(object):
         return self._cmap[string]
 
 def cmdReset():
-    return ESC + "[0m"
+    if sys.stdout.isatty():
+        return ESC + "[0m"
+    else:
+        return ""
 
 def cmdColour(colour):
-    return ESC + "[" + colour + "m"
+    if sys.stdout.isatty():
+        return ESC + "[" + colour + "m"
+    else:
+        return ""
index 03234d1c0717d593c73aa3732c44e8d14a61b5a6..dc36375a24217c721d83d490d5179c8a3cb567d2 100644 (file)
@@ -182,7 +182,10 @@ class StatusAction(Action):
         for status in statuses:
             statusStr = sf(status)
             if statusStr.strip():
-                print statusStr.encode(sys.stdout.encoding, 'replace')
+                if sys.stdout.encoding:
+                    print statusStr.encode(sys.stdout.encoding, 'replace')
+                else:
+                    print statusStr.encode('utf-8')
 
 class AdminAction(Action):
     def __call__(self, twitter, options):