[py-svn] r57039 - in py/branch/event/py/io: . testing

hpk at codespeak.net hpk at codespeak.net
Wed Aug 6 20:33:09 CEST 2008


Author: hpk
Date: Wed Aug  6 20:33:08 2008
New Revision: 57039

Modified:
   py/branch/event/py/io/terminalwriter.py
   py/branch/event/py/io/testing/test_terminalwriter.py
Log:
add markup and ansi-color codes for terminal printing


Modified: py/branch/event/py/io/terminalwriter.py
==============================================================================
--- py/branch/event/py/io/terminalwriter.py	(original)
+++ py/branch/event/py/io/terminalwriter.py	Wed Aug  6 20:33:08 2008
@@ -42,7 +42,29 @@
     if flush:
         file.flush()
 
+def ansi_print(text, esc, file=None, newline=True, flush=False):
+    if file is None:
+        file = sys.stderr
+    text = text.rstrip()
+    if esc and sys.platform != "win32" and file.isatty():
+        if not isinstance(esc, tuple):
+            esc = (esc,)
+        text = (''.join(['\x1b[%sm' % cod for cod in esc])  +  
+                text +
+                '\x1b[0m')     # ANSI color code "reset"
+    if newline:
+        text += '\n'
+    file.write(text)
+    if flush:
+        file.flush()
+
 class TerminalWriter(object):
+    _esctable = dict(black=30, red=31, green=32, yellow=33, 
+                     blue=34, purple=35, cyan=36, white=37,
+                     Black=40, Red=41, Green=42, Yellow=43, 
+                     Blue=44, Purple=45, Cyan=46, White=47,
+                     bold=1, light=2, blink=5, invert=7)
+
     def __init__(self, file=None, stringio=False):
         if file is None:
             if stringio:
@@ -54,6 +76,20 @@
         self._file = file
         self.fullwidth = get_terminal_width()
 
+    def _escaped(self, text, esc):
+        if esc and sys.platform != "win32":
+            if hasattr(self._file, 'isatty') and self._file.isatty():
+                text = (''.join(['\x1b[%sm' % cod for cod in esc])  +  
+                    text +'\x1b[0m')
+        return text
+
+    def markup(self, text, **kw):
+        esc = []
+        for name in kw:
+            if name in self._esctable:
+                esc.append(self._esctable[name])
+        return self._escaped(text, tuple(esc))
+
     def sep(self, sepchar, title=None, fullwidth=None):
         if not fullwidth:
             fullwidth = self.fullwidth

Modified: py/branch/event/py/io/testing/test_terminalwriter.py
==============================================================================
--- py/branch/event/py/io/testing/test_terminalwriter.py	(original)
+++ py/branch/event/py/io/testing/test_terminalwriter.py	Wed Aug  6 20:33:08 2008
@@ -45,6 +45,18 @@
         assert len(l) == 1
         assert l[0] == "-" * 26 + " hello " + "-" * 27 + "\n"
 
+    def test__escaped(self):
+        tw = self.getwriter()
+        text2 = tw._escaped("hello", (31))
+        assert text2.find("hello") != -1
+
+    def test_markup(self):
+        tw = self.getwriter()
+        for bold in (True, False):
+            for color in ("red", "green"):
+                text2 = tw.markup("hello", color=color, bold=bold)
+                assert text2.find("hello") != -1
+
 class TestStringIO(BaseTests):
     def getwriter(self):
         self.tw = py.io.TerminalWriter(stringio=True)



More information about the pytest-commit mailing list