summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-07-07 09:27:00 +1200
committerLars Wirzenius <liw@liw.fi>2010-07-07 09:27:00 +1200
commit129e223f07e7d090655da2bfdaa6262cfa399f2e (patch)
tree46d0b1f315e225eebf52a49649d50aa7bf90723d
parentdc830c6e3bdaa9f2caf766a541a33767c14eacc3 (diff)
downloadpython-ttystatus-129e223f07e7d090655da2bfdaa6262cfa399f2e.tar.gz
Don't attempt to write until it is time.
-rw-r--r--ttystatus/messager.py9
-rw-r--r--ttystatus/messager_tests.py12
-rw-r--r--ttystatus/status.py3
-rw-r--r--ttystatus/status_tests.py3
4 files changed, 23 insertions, 4 deletions
diff --git a/ttystatus/messager.py b/ttystatus/messager.py
index 43fb0e1..c322590 100644
--- a/ttystatus/messager.py
+++ b/ttystatus/messager.py
@@ -85,14 +85,17 @@ class Messager(object):
self._raw_write('\r' + (' ' * len(self._last_msg)) + '\r')
self._raw_write(string)
self._last_msg = string
+
+ def time_to_write(self):
+ '''Is it time to write now?'''
+ return self._now() - self._last_time >= self._period
def write(self, string):
'''Write raw data, but only once per period.'''
string = string[:self.width]
- now = self._now()
- if now - self._last_time >= self._period:
+ if self.time_to_write():
self._overwrite(string)
- self._last_time = now
+ self._last_time = self._now()
self._cached_msg = string
def clear(self):
diff --git a/ttystatus/messager_tests.py b/ttystatus/messager_tests.py
index 4830727..b57fbaf 100644
--- a/ttystatus/messager_tests.py
+++ b/ttystatus/messager_tests.py
@@ -46,6 +46,18 @@ class MessagerTests(unittest.TestCase):
def test_raw_writes_something_if_output_is_not_a_terminal(self):
self.messager._raw_write('foo')
self.assertEqual(self.output.getvalue(), 'foo')
+
+ def test_knows_it_is_time_to_write_at_start(self):
+ self.assert_(self.messager.time_to_write())
+
+ def test_knows_it_is_not_time_to_write_right_after_previous_one(self):
+ self.messager._last_time = self.messager._now()
+ self.assertFalse(self.messager.time_to_write())
+
+ def test_knows_it_is_time_to_write_after_a_period(self):
+ self.messager._last_time = (self.messager._now() -
+ self.messager._period*2)
+ self.assert_(self.messager.time_to_write())
def test_cached_write_writes_first_thing(self):
self.messager.write('foo')
diff --git a/ttystatus/status.py b/ttystatus/status.py
index d7d8ef4..67443fc 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -62,7 +62,8 @@ class TerminalStatus(object):
for w in self._interests.get(key, []):
w.update(self, width)
width -= len(str(w))
- self._m.write(''.join(str(w) for w in self._widgets))
+ if self._m.time_to_write():
+ self._m.write(''.join(str(w) for w in self._widgets))
def increase(self, key, delta):
'''Increase value for a key by a given amount.'''
diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py
index 36bc4bd..d22db53 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -25,6 +25,9 @@ class DummyMessager(object):
def clear(self):
pass
+
+ def time_to_write(self):
+ return True
def write(self, string):
pass