From 8c1491860ffcf6870e87a1a1276524fe3c8c0ffc Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 5 Jun 2010 13:52:06 +1200 Subject: Add Messager.notify. --- ttystatus/messager.py | 18 ++++++++++++++++++ ttystatus/messager_tests.py | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/ttystatus/messager.py b/ttystatus/messager.py index 20f8fc9..42a202d 100644 --- a/ttystatus/messager.py +++ b/ttystatus/messager.py @@ -51,3 +51,21 @@ class Messager(object): def clear(self): '''Remove current message from terminal.''' self._raw_write('') + + def notify(self, string): + '''Show a notification message string to the user. + + Notifications are meant for error messages and other things + that do not belong in, say, progress bars. Whatever is currently + on the terminal is wiped, then the notification message is shown, + a new line is started, and the old message is restored. + + Notifications are written even when the output is not going + to a terminal. + + ''' + + old = self._last_msg + self.clear() + self.output.write('%s\n' % string) + self._raw_write(old) diff --git a/ttystatus/messager_tests.py b/ttystatus/messager_tests.py index 4fc616c..95448ea 100644 --- a/ttystatus/messager_tests.py +++ b/ttystatus/messager_tests.py @@ -69,3 +69,8 @@ class MessagerTests(unittest.TestCase): self.messager.clear() self.assertEqual(self.output.getvalue(), 'foo\r \r') + def test_notify_removes_message_and_puts_it_back_afterwards(self): + self.messager.write('foo') + self.messager.notify('bar') + self.assertEqual(self.output.getvalue(), 'foo\r \rbar\nfoo') + -- cgit v1.2.1