diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-07-05 09:32:45 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-07-05 09:32:45 +1200 |
commit | 9b4abd1410adbad7456bc0360069ee72be2e8099 (patch) | |
tree | f0397527289e18b06b2a4de86c2e907138114d8f | |
parent | f99bc0bd33290a62576408a11222f44527d0145d (diff) | |
download | python-ttystatus-9b4abd1410adbad7456bc0360069ee72be2e8099.tar.gz |
Fix Messager to report actual width it will use as .width.
This way, when TerminalStatus computes the amount of
remaining space, it is not off by one: Messager refuses to
print out the last character lest the terminal scroll or
line wrap.
-rw-r--r-- | ttystatus/messager.py | 9 | ||||
-rw-r--r-- | ttystatus/messager_tests.py | 4 |
2 files changed, 8 insertions, 5 deletions
diff --git a/ttystatus/messager.py b/ttystatus/messager.py index ce74b02..43fb0e1 100644 --- a/ttystatus/messager.py +++ b/ttystatus/messager.py @@ -32,9 +32,12 @@ class Messager(object): self._last_msg = '' # What did we write last? self._last_time = 0 # When did we write last? self._cached_msg = '' # Last message from user, to write() method. - self.width = self._get_terminal_width() # Width of terminal + self.set_width(self._get_terminal_width()) # Width of terminal signal.signal(signal.SIGWINCH, self._sigwinch_handler) + def set_width(self, actual_width): + self.width = actual_width - 1 + def _now(self): '''Return current time.''' # This is a wrapper around time.time(), for testing. @@ -65,7 +68,7 @@ class Messager(object): # Clear the terminal from old stuff, using the old width. self.clear() # Get new width. - self.width = self._get_terminal_width() + self.set_width(self._get_terminal_width()) def _raw_write(self, string): '''Write raw data if output is terminal.''' @@ -85,7 +88,7 @@ class Messager(object): def write(self, string): '''Write raw data, but only once per period.''' - string = string[:self.width - 1] + string = string[:self.width] now = self._now() if now - self._last_time >= self._period: self._overwrite(string) diff --git a/ttystatus/messager_tests.py b/ttystatus/messager_tests.py index 1c51a47..4830727 100644 --- a/ttystatus/messager_tests.py +++ b/ttystatus/messager_tests.py @@ -86,9 +86,9 @@ class MessagerTests(unittest.TestCase): self.assertEqual(self.output.getvalue(), 'foo\n') def test_has_width(self): - self.assertEqual(self.messager.width, 80) + self.assertEqual(self.messager.width, 79) def test_write_truncates_at_one_less_than_width(self): - self.messager.width = 4 + self.messager.set_width(4) self.messager.write('foobar') self.assertEqual(self.output.getvalue(), 'foo') |