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

hpk at codespeak.net hpk at codespeak.net
Fri Aug 15 15:40:19 CEST 2008


Author: hpk
Date: Fri Aug 15 15:40:19 2008
New Revision: 57283

Modified:
   py/branch/event/py/io/terminalwriter.py
   py/branch/event/py/io/testing/test_terminalwriter.py
Log:
extend terminalwriter a bit, test attributes


Modified: py/branch/event/py/io/terminalwriter.py
==============================================================================
--- py/branch/event/py/io/terminalwriter.py	(original)
+++ py/branch/event/py/io/terminalwriter.py	Fri Aug 15 15:40:19 2008
@@ -60,12 +60,13 @@
             file = WriteFile(file)
         self._file = file
         self.fullwidth = get_terminal_width()
+        self.hasmarkup = sys.platform != "win32" and \
+                        hasattr(file, 'isatty') and file.isatty() 
 
     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')
+        if esc and self.hasmarkup:
+            text = (''.join(['\x1b[%sm' % cod for cod in esc])  +  
+                text +'\x1b[0m')
         return text
 
     def markup(self, text, **kw):
@@ -78,7 +79,7 @@
         return self._escaped(text, tuple(esc))
 
     def sep(self, sepchar, title=None, fullwidth=None, **kw):
-        if not fullwidth:
+        if fullwidth is None:
             fullwidth = self.fullwidth
         # the goal is to have the line be as long as possible
         # under the condition that len(line) <= fullwidth
@@ -102,8 +103,12 @@
 
         self.line(line, **kw)
 
-    def write(self, s):
-        self._file.write(str(s))
+    def write(self, s, **kw):
+        if s:
+            s = str(s)
+            if self.hasmarkup and kw:
+                s = self.markup(s, **kw)
+            self._file.write(s)
         self._file.flush()
 
     def line(self, s='', **kw):

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	Fri Aug 15 15:40:19 2008
@@ -59,6 +59,23 @@
         py.test.raises(ValueError, "tw.markup('x', wronkw=3)")
         py.test.raises(ValueError, "tw.markup('x', wronkw=0)")
 
+    def test_line_write_markup(self):
+        tw = self.getwriter()
+        tw.hasmarkup = True
+        tw.line("x", bold=True)
+        tw.write("x\n", red=True)
+        l = self.getlines()
+        assert len(l[0]) > 2, l
+        assert len(l[1]) > 2, l
+
+    def test_attr_fullwidth(self):
+        tw = self.getwriter()
+        tw.sep("-", "hello", fullwidth=40)
+        tw.fullwidth = 40
+        tw.sep("-", "hello")
+        l = self.getlines()
+        assert len(l[0]) == len(l[1])
+
 class TestStringIO(BaseTests):
     def getwriter(self):
         self.tw = py.io.TerminalWriter(stringio=True)
@@ -78,3 +95,14 @@
         io.write("".join(self.writes))
         io.seek(0)
         return io.readlines()
+
+def test_attr_hasmarkup():
+    tw = py.io.TerminalWriter(stringio=True)
+    assert not tw.hasmarkup
+    tw.hasmarkup = True
+    tw.line("hello", bold=True)
+    s = tw.stringio.getvalue()
+    assert len(s) > len("hello")
+
+    
+



More information about the pytest-commit mailing list