summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-29 18:42:21 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-29 18:42:21 +0100
commit84e81bdaaab761107f280c0d48dd4d47928c43f4 (patch)
treef246c095b8b4b014a9730eb4677badbb895a8b26
parent7926271cfb87478f8884c14563e48e94b128d291 (diff)
parent26d564c10f8c62c955f049db03ca26f8cadb943e (diff)
downloadpython-ttystatus-84e81bdaaab761107f280c0d48dd4d47928c43f4.tar.gz
Write notifications to stdout, and way to write errors to stderr.
-rw-r--r--NEWS6
-rw-r--r--example.py2
-rw-r--r--ttystatus/messager.py9
-rw-r--r--ttystatus/messager_tests.py13
-rw-r--r--ttystatus/status.py8
-rw-r--r--ttystatus/status_tests.py5
6 files changed, 35 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index 9a1cb40..5832380 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,12 @@
NEWS file for ttystatus
=======================
+Version 0.14, released XXXXXXXXXX
+---------------------------------
+
+* Notifications are written to the standard output, not the terminal.
+* A method for printing error messages is provided. They are printed to
+ the standard error.
Version 0.13, released 2011-08-18
---------------------------------
diff --git a/example.py b/example.py
index ddb5500..909ce46 100644
--- a/example.py
+++ b/example.py
@@ -60,6 +60,8 @@ def main():
if os.path.islink(pathname):
ts['symlink'] = pathname
ts.notify('Symlink! %s' % pathname)
+ elif 'error' in pathname:
+ ts.error('Error in pathname: %s' % pathname)
ts['done'] += 1
ts.finish()
diff --git a/ttystatus/messager.py b/ttystatus/messager.py
index bcbceca..43a04cf 100644
--- a/ttystatus/messager.py
+++ b/ttystatus/messager.py
@@ -116,7 +116,7 @@ class Messager(object):
'''Remove current message from terminal.'''
self._overwrite('')
- def notify(self, string):
+ def notify(self, string, f):
'''Show a notification message string to the user.
Notifications are meant for error messages and other things
@@ -133,10 +133,11 @@ class Messager(object):
old = self._last_msg
self.clear()
try:
- self.output.write('%s\n' % string)
- except IOError: # pragma: no cover
+ f.write('%s\n' % string)
+ f.flush()
+ except IOError:
# We ignore these. No point in crashing if terminal is bad.
- self.output.flush()
+ pass
self._overwrite(old)
def finish(self):
diff --git a/ttystatus/messager_tests.py b/ttystatus/messager_tests.py
index 6f9bc8a..3ce889e 100644
--- a/ttystatus/messager_tests.py
+++ b/ttystatus/messager_tests.py
@@ -93,9 +93,18 @@ class MessagerTests(unittest.TestCase):
self.assertEqual(self.output.getvalue(), 'foo\r \r')
def test_notify_removes_message_and_puts_it_back_afterwards(self):
+ f = StringIO.StringIO()
self.messager.write('foo')
- self.messager.notify('bar')
- self.assertEqual(self.output.getvalue(), 'foo\r \rbar\nfoo')
+ self.messager.notify('bar', f)
+ self.assertEqual(self.output.getvalue(), 'foo\r \rfoo')
+ self.assertEqual(f.getvalue(), 'bar\n')
+
+ def test_notify_does_not_mind_ioerror(self):
+ f = open('/dev/full', 'w')
+ self.messager.write('foo')
+ self.messager.notify('bar', f)
+ self.assertEqual(self.output.getvalue(), 'foo\r \rfoo')
+ f.close()
def test_finish_flushes_unwritten_message(self):
self.messager._now = lambda: 0
diff --git a/ttystatus/status.py b/ttystatus/status.py
index 63bf438..b5d743a 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -14,6 +14,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import sys
+
import ttystatus
@@ -80,7 +82,11 @@ class TerminalStatus(object):
def notify(self, msg):
'''Show a message.'''
- self._m.notify(msg)
+ self._m.notify(msg, sys.stdout)
+
+ def error(self, msg):
+ '''Write an error message.'''
+ self._m.notify(msg, sys.stderr)
def finish(self):
'''Finish status display.'''
diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py
index 068212a..9e6d0ac 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -37,7 +37,7 @@ class DummyMessager(object):
def write(self, string):
self.written.write(string)
- def notify(self, string):
+ def notify(self, string, f):
pass
def finish(self):
@@ -117,6 +117,9 @@ class TerminalStatusTests(unittest.TestCase):
def test_has_notify_method(self):
self.assertEqual(self.ts.notify('foo'), None)
+ def test_has_error_method(self):
+ self.assertEqual(self.ts.error('foo'), None)
+
def test_has_finish_method(self):
self.assertEqual(self.ts.finish(), None)