diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-06-05 13:52:06 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-06-05 13:52:06 +1200 |
commit | 8c1491860ffcf6870e87a1a1276524fe3c8c0ffc (patch) | |
tree | 5296f3593f114d8735ea0f536bf638735ad54826 | |
parent | 04b32543ee3c5d5f401aa1353b357bc688964cf4 (diff) | |
download | python-ttystatus-8c1491860ffcf6870e87a1a1276524fe3c8c0ffc.tar.gz |
Add Messager.notify.
-rw-r--r-- | ttystatus/messager.py | 18 | ||||
-rw-r--r-- | ttystatus/messager_tests.py | 5 |
2 files changed, 23 insertions, 0 deletions
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') + |