summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-04-15 19:19:53 +0100
committerLars Wirzenius <liw@liw.fi>2012-04-15 19:19:53 +0100
commit8620de40bf5d5a223acc37d5cc9ed55aa022314c (patch)
tree014b076948c03243121d1a81b72ae5b5c5223b6c
parentb3cd2a40dc74bb33753c5c04bab06b5e93086d64 (diff)
downloadpython-ttystatus-8620de40bf5d5a223acc37d5cc9ed55aa022314c.tar.gz
Fix rendering to adjust to available space
-rw-r--r--ttystatus/status.py29
-rw-r--r--ttystatus/status_tests.py8
2 files changed, 32 insertions, 5 deletions
diff --git a/ttystatus/status.py b/ttystatus/status.py
index 8365017..b0b93dd 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -72,12 +72,31 @@ class TerminalStatus(object):
for w in self._widgets:
w.update(self)
if self._m.time_to_write():
- self._render()
+ self._write()
def _render(self):
- '''Format and output all widgets.'''
- self._m.write(''.join(w.render(1) for w in self._widgets))
-
+ '''Render current state of all widgets.'''
+
+ remaining = self._m.width
+
+ texts = [None] * len(self._widgets)
+
+ for i, w in enumerate(self._widgets):
+ if w.static_width:
+ texts[i] = w.render(0)
+ remaining -= len(texts[i])
+
+ for i, w in enumerate(self._widgets):
+ if not w.static_width:
+ texts[i] = w.render(remaining)
+ remaining -= len(texts[i])
+
+ return ''.join(texts)
+
+ def _write(self):
+ '''Render and output current state of all widgets.'''
+ self._m.write(self._render())
+
def increase(self, key, delta):
'''Increase value for a key by a given amount.'''
self[key] = (self[key] or 0) + delta
@@ -92,7 +111,7 @@ class TerminalStatus(object):
def finish(self):
'''Finish status display.'''
- self._render()
+ self._write()
self._m.finish()
def disable(self):
diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py
index 500cde2..aa8c021 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -130,3 +130,11 @@ class TerminalStatusTests(unittest.TestCase):
self.ts['value'] = i
self.assertEqual(w.render(0), str(n))
+ def test_renders_everything_when_there_is_space(self):
+ w1 = ttystatus.Literal('foo')
+ w2 = ttystatus.ProgressBar('done', 'total')
+ self.ts.add(w1)
+ self.ts.add(w2)
+ text = self.ts._render()
+ self.assertEqual(len(text), self.ts._m.width)
+