summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-04-15 19:08:23 +0100
committerLars Wirzenius <liw@liw.fi>2012-04-15 19:08:23 +0100
commitb3cd2a40dc74bb33753c5c04bab06b5e93086d64 (patch)
tree8b8577972fb765b9186385a498be84d4ca44f01c
parent9cb92e835f1b37a5e5a30c0fdbc76ab3bb220f89 (diff)
downloadpython-ttystatus-b3cd2a40dc74bb33753c5c04bab06b5e93086d64.tar.gz
Add width argument to render() method
-rw-r--r--ttystatus/bytesize.py4
-rw-r--r--ttystatus/bytesize_tests.py19
-rw-r--r--ttystatus/bytespeed.py4
-rw-r--r--ttystatus/bytespeed_tests.py13
-rw-r--r--ttystatus/counter.py4
-rw-r--r--ttystatus/counter_tests.py11
-rw-r--r--ttystatus/elapsed.py2
-rw-r--r--ttystatus/elapsed_tests.py9
-rw-r--r--ttystatus/fmt_tests.py8
-rw-r--r--ttystatus/index.py4
-rw-r--r--ttystatus/index_tests.py9
-rw-r--r--ttystatus/integer.py4
-rw-r--r--ttystatus/integer_tests.py9
-rw-r--r--ttystatus/literal.py2
-rw-r--r--ttystatus/literal_tests.py9
-rw-r--r--ttystatus/pathname.py4
-rw-r--r--ttystatus/pathname_tests.py9
-rw-r--r--ttystatus/percent.py4
-rw-r--r--ttystatus/percent_tests.py11
-rw-r--r--ttystatus/progressbar.py11
-rw-r--r--ttystatus/progressbar_tests.py24
-rw-r--r--ttystatus/remtime.py2
-rw-r--r--ttystatus/remtime_tests.py17
-rw-r--r--ttystatus/status.py2
-rw-r--r--ttystatus/status_tests.py6
-rw-r--r--ttystatus/string.py4
-rw-r--r--ttystatus/string_tests.py9
-rw-r--r--ttystatus/widget.py17
28 files changed, 151 insertions, 80 deletions
diff --git a/ttystatus/bytesize.py b/ttystatus/bytesize.py
index b5a8059..13ee68b 100644
--- a/ttystatus/bytesize.py
+++ b/ttystatus/bytesize.py
@@ -20,6 +20,8 @@ import ttystatus
class ByteSize(ttystatus.Widget):
'''Display data size in bytes, KiB, etc.'''
+
+ static_width = False
def __init__(self, name):
self.name = name
@@ -28,7 +30,7 @@ class ByteSize(ttystatus.Widget):
def update(self, ts):
self._bytes = ts[self.name]
- def render(self):
+ def render(self, width):
units = (
(1024**4, 2, 'TiB'),
(1024**3, 2, 'GiB'),
diff --git a/ttystatus/bytesize_tests.py b/ttystatus/bytesize_tests.py
index e6ce761..15a0ff5 100644
--- a/ttystatus/bytesize_tests.py
+++ b/ttystatus/bytesize_tests.py
@@ -24,34 +24,37 @@ class ByteSizeTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.ByteSize('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_formats_zero_bytes_without_update(self):
- self.assertEqual(self.w.render(), '0 B')
+ self.assertEqual(self.w.render(0), '0 B')
def test_formats_zero_bytes_correctly(self):
self.w.update({ 'foo': 0 })
- self.assertEqual(self.w.render(), '0 B')
+ self.assertEqual(self.w.render(0), '0 B')
def test_formats_one_bytes_correctly(self):
self.w.update({ 'foo': 1 })
- self.assertEqual(self.w.render(), '1 B')
+ self.assertEqual(self.w.render(0), '1 B')
def test_formats_1023_bytes_correctly(self):
self.w.update({ 'foo': 1023 })
- self.assertEqual(self.w.render(), '1023 B')
+ self.assertEqual(self.w.render(0), '1023 B')
def test_formats_1024_bytes_correctly(self):
self.w.update({ 'foo': 1024 })
- self.assertEqual(self.w.render(), '1.0 KiB')
+ self.assertEqual(self.w.render(0), '1.0 KiB')
def test_formats_1_MiB_bytes_correctly(self):
self.w.update({ 'foo': 1024**2 })
- self.assertEqual(self.w.render(), '1.00 MiB')
+ self.assertEqual(self.w.render(0), '1.00 MiB')
def test_formats_1_GiB_bytes_correctly(self):
self.w.update({ 'foo': 1024**3 })
- self.assertEqual(self.w.render(), '1.00 GiB')
+ self.assertEqual(self.w.render(0), '1.00 GiB')
def test_formats_1_TiB_bytes_correctly(self):
self.w.update({ 'foo': 1024**4 })
- self.assertEqual(self.w.render(), '1.00 TiB')
+ self.assertEqual(self.w.render(0), '1.00 TiB')
diff --git a/ttystatus/bytespeed.py b/ttystatus/bytespeed.py
index d7e8b78..3331069 100644
--- a/ttystatus/bytespeed.py
+++ b/ttystatus/bytespeed.py
@@ -22,6 +22,8 @@ import ttystatus
class ByteSpeed(ttystatus.Widget):
'''Display data size in bytes, KiB, etc.'''
+
+ static_width = False
def __init__(self, name):
self.name = name
@@ -33,7 +35,7 @@ class ByteSpeed(ttystatus.Widget):
return time.time()
- def render(self):
+ def render(self, width):
units = (
(1024**4, 2, 'TiB/s'),
(1024**3, 2, 'GiB/s'),
diff --git a/ttystatus/bytespeed_tests.py b/ttystatus/bytespeed_tests.py
index b56afc3..9bcc1e0 100644
--- a/ttystatus/bytespeed_tests.py
+++ b/ttystatus/bytespeed_tests.py
@@ -24,31 +24,34 @@ class ByteSpeedTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.ByteSpeed('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_formats_zero_speed_without_update(self):
- self.assertEqual(self.w.render(), '0 B/s')
+ self.assertEqual(self.w.render(0), '0 B/s')
def test_formats_zero_bytes_correctly(self):
self.w.update({ 'foo': 0 })
- self.assertEqual(self.w.render(), '0 B/s')
+ self.assertEqual(self.w.render(0), '0 B/s')
def test_formats_one_byte_per_second_correctly(self):
self.w.now = lambda: 1
self.w.update({ 'foo': 0 })
self.w.now = lambda: 2
self.w.update({ 'foo': 1 })
- self.assertEqual(self.w.render(), '1 B/s')
+ self.assertEqual(self.w.render(0), '1 B/s')
def test_formats_ten_bytes_per_second_correctly(self):
self.w.now = lambda: 1
self.w.update({ 'foo': 0 })
self.w.now = lambda: 11
self.w.update({ 'foo': 100 })
- self.assertEqual(self.w.render(), '10 B/s')
+ self.assertEqual(self.w.render(0), '10 B/s')
def test_formats_ten_tibs_per_second_correctly(self):
self.w.now = lambda: 1
self.w.update({ 'foo': 0 })
self.w.now = lambda: 2
self.w.update({ 'foo': 10 * 1024**4 })
- self.assertEqual(self.w.render(), '10.00 TiB/s')
+ self.assertEqual(self.w.render(0), '10.00 TiB/s')
diff --git a/ttystatus/counter.py b/ttystatus/counter.py
index ae61fa0..d37ed90 100644
--- a/ttystatus/counter.py
+++ b/ttystatus/counter.py
@@ -21,12 +21,14 @@ class Counter(ttystatus.Widget):
'''Display a count of how many times a value has changed.'''
+ static_width = False
+
def __init__(self, name):
self.name = name
self.prev = None
self.count = 0
- def render(self):
+ def render(self, width):
return str(self.count)
def update(self, master):
diff --git a/ttystatus/counter_tests.py b/ttystatus/counter_tests.py
index 638de6a..cadadea 100644
--- a/ttystatus/counter_tests.py
+++ b/ttystatus/counter_tests.py
@@ -24,20 +24,23 @@ class CounterTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.Counter('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_counts_zero_initially(self):
- self.assertEqual(self.w.render(), '0')
+ self.assertEqual(self.w.render(0), '0')
def test_counts_one_change(self):
self.w.update({ 'foo': 'a' })
- self.assertEqual(self.w.render(), '1')
+ self.assertEqual(self.w.render(0), '1')
def test_counts_two_changes(self):
self.w.update({ 'foo': 'a' })
self.w.update({ 'foo': 'b' })
- self.assertEqual(self.w.render(), '2')
+ self.assertEqual(self.w.render(0), '2')
def test_does_not_count_if_value_does_not_change(self):
self.w.update({ 'foo': 'a' })
self.w.update({ 'foo': 'a' })
- self.assertEqual(self.w.render(), '1')
+ self.assertEqual(self.w.render(0), '1')
diff --git a/ttystatus/elapsed.py b/ttystatus/elapsed.py
index 3a85303..cb75722 100644
--- a/ttystatus/elapsed.py
+++ b/ttystatus/elapsed.py
@@ -31,7 +31,7 @@ class ElapsedTime(ttystatus.Widget):
'''Wrapper around time.time() for unit tests to override.'''
return time.time()
- def render(self):
+ def render(self, width):
secs = self.secs
hours = secs / 3600
secs %= 3600
diff --git a/ttystatus/elapsed_tests.py b/ttystatus/elapsed_tests.py
index 3197a77..b87d57c 100644
--- a/ttystatus/elapsed_tests.py
+++ b/ttystatus/elapsed_tests.py
@@ -24,18 +24,21 @@ class ElapsedtimeTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.ElapsedTime()
+ def test_is_static_width(self):
+ self.assertTrue(self.w.static_width)
+
def test_shows_zero_initially(self):
- self.assertEqual(self.w.render(), '00h00m00s')
+ self.assertEqual(self.w.render(0), '00h00m00s')
def test_shows_zero_after_first_update(self):
self.w.get_time = lambda: 1
self.w.update({})
- self.assertEqual(self.w.render(), '00h00m00s')
+ self.assertEqual(self.w.render(0), '00h00m00s')
def test_shows_one_one_one_after_second_update(self):
self.w.get_time = lambda: 0
self.w.update({})
self.w.get_time = lambda: 60*60 + 60 + 1
self.w.update({})
- self.assertEqual(self.w.render(), '01h01m01s')
+ self.assertEqual(self.w.render(0), '01h01m01s')
diff --git a/ttystatus/fmt_tests.py b/ttystatus/fmt_tests.py
index 422a7fe..310f131 100644
--- a/ttystatus/fmt_tests.py
+++ b/ttystatus/fmt_tests.py
@@ -32,13 +32,13 @@ class FormatTests(unittest.TestCase):
x = ttystatus.fmt.parse('hello, world')
self.assertEqual(len(x), 1)
self.assertEqual(type(x[0]), ttystatus.Literal)
- self.assertEqual(x[0].render(), 'hello, world')
+ self.assertEqual(x[0].render(0), 'hello, world')
def test_parses_escaped_pecent(self):
x = ttystatus.fmt.parse('%%')
self.assertEqual(len(x), 1)
self.assertEqual(type(x[0]), ttystatus.Literal)
- self.assertEqual(x[0].render(), '%')
+ self.assertEqual(x[0].render(0), '%')
def test_parses_parameterless_widget(self):
x = ttystatus.fmt.parse('%ElapsedTime()')
@@ -60,12 +60,12 @@ class FormatTests(unittest.TestCase):
self.assertEqual(len(x), 4)
self.assertEqual(type(x[0]), ttystatus.Literal)
- self.assertEqual(x[0].render(), 'hello, ')
+ self.assertEqual(x[0].render(0), 'hello, ')
self.assertEqual(type(x[1]), ttystatus.String)
self.assertEqual(type(x[2]), ttystatus.Literal)
- self.assertEqual(x[2].render(), ': ')
+ self.assertEqual(x[2].render(0), ': ')
self.assertEqual(type(x[3]), ttystatus.ElapsedTime)
diff --git a/ttystatus/index.py b/ttystatus/index.py
index 3e73502..0158037 100644
--- a/ttystatus/index.py
+++ b/ttystatus/index.py
@@ -21,13 +21,15 @@ class Index(ttystatus.Widget):
'''Display the position of a value in a list of values.'''
+ static_width = False
+
def __init__(self, name, listname):
self.name = name
self.listname = listname
self.value = None
self.listvalue = []
- def render(self):
+ def render(self, render):
try:
index = self.listvalue.index(self.value) + 1
except ValueError:
diff --git a/ttystatus/index_tests.py b/ttystatus/index_tests.py
index 05d2db7..ccc4d4c 100644
--- a/ttystatus/index_tests.py
+++ b/ttystatus/index_tests.py
@@ -24,14 +24,17 @@ class IndexTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.Index('foo', 'foos')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_is_zero_initially(self):
- self.assertEqual(self.w.render(), '0/0')
+ self.assertEqual(self.w.render(0), '0/0')
def test_gets_index_right(self):
self.w.update({ 'foo': 'x', 'foos': ['a', 'x', 'b'] })
- self.assertEqual(self.w.render(), '2/3')
+ self.assertEqual(self.w.render(0), '2/3')
def test_handles_value_not_in_list(self):
self.w.update({ 'foo': 'xxx', 'foos': ['a', 'x', 'b'] })
- self.assertEqual(self.w.render(), '0/3')
+ self.assertEqual(self.w.render(0), '0/3')
diff --git a/ttystatus/integer.py b/ttystatus/integer.py
index 7def9ff..7995bee 100644
--- a/ttystatus/integer.py
+++ b/ttystatus/integer.py
@@ -21,11 +21,13 @@ class Integer(ttystatus.Widget):
'''Display a value as an integer.'''
+ static_width = False
+
def __init__(self, key):
self._key = key
self.value = None
- def render(self):
+ def render(self, width):
try:
return str(int(self.value))
except (TypeError, ValueError):
diff --git a/ttystatus/integer_tests.py b/ttystatus/integer_tests.py
index 9ea8054..e064621 100644
--- a/ttystatus/integer_tests.py
+++ b/ttystatus/integer_tests.py
@@ -24,14 +24,17 @@ class IntegerTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.Integer('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_is_error_initially(self):
- self.assertEqual(self.w.render(), '#')
+ self.assertEqual(self.w.render(0), '#')
def test_updates(self):
self.w.update({'foo': 123})
- self.assertEqual(self.w.render(), '123')
+ self.assertEqual(self.w.render(0), '123')
def test_becomes_error_symbol_if_value_is_not_integer(self):
self.w.update({'foo': 'bar'})
- self.assertEqual(self.w.render(), '#')
+ self.assertEqual(self.w.render(0), '#')
diff --git a/ttystatus/literal.py b/ttystatus/literal.py
index fa09ceb..cdeb4d7 100644
--- a/ttystatus/literal.py
+++ b/ttystatus/literal.py
@@ -24,5 +24,5 @@ class Literal(ttystatus.Widget):
def __init__(self, string):
self.value = string
- def render(self):
+ def render(self, width):
return self.value
diff --git a/ttystatus/literal_tests.py b/ttystatus/literal_tests.py
index 73c8964..b1e4537 100644
--- a/ttystatus/literal_tests.py
+++ b/ttystatus/literal_tests.py
@@ -21,6 +21,11 @@ import ttystatus
class LiteralTests(unittest.TestCase):
+ def setUp(self):
+ self.w = ttystatus.Literal('foo')
+
+ def test_is_static_width(self):
+ self.assertTrue(self.w.static_width)
+
def test_sets_value_correctly(self):
- literal = ttystatus.Literal('foo')
- self.assertEqual(literal.render(), 'foo')
+ self.assertEqual(self.w.render(0), 'foo')
diff --git a/ttystatus/pathname.py b/ttystatus/pathname.py
index 0ecf4a7..bf5fe5a 100644
--- a/ttystatus/pathname.py
+++ b/ttystatus/pathname.py
@@ -24,12 +24,14 @@ class Pathname(ttystatus.Widget):
If it won't fit completely, truncate from the beginning of the string.
'''
+
+ static_width = False
def __init__(self, key):
self._key = key
self.pathname = ''
- def render(self):
+ def render(self, render):
return self.pathname
def update(self, master):
diff --git a/ttystatus/pathname_tests.py b/ttystatus/pathname_tests.py
index 7f09de0..1191f43 100644
--- a/ttystatus/pathname_tests.py
+++ b/ttystatus/pathname_tests.py
@@ -24,13 +24,16 @@ class PathnameTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.Pathname('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_is_empty_initially(self):
- self.assertEqual(self.w.render(), '')
+ self.assertEqual(self.w.render(0), '')
def test_updates(self):
self.w.update({'foo': 'bar'})
- self.assertEqual(self.w.render(), 'bar')
+ self.assertEqual(self.w.render(0), 'bar')
def test_handles_update_to_other_value(self):
self.w.update({'other': 1})
- self.assertEqual(self.w.render(), '')
+ self.assertEqual(self.w.render(0), '')
diff --git a/ttystatus/percent.py b/ttystatus/percent.py
index 117fecc..1a60c23 100644
--- a/ttystatus/percent.py
+++ b/ttystatus/percent.py
@@ -20,6 +20,8 @@ import ttystatus
class PercentDone(ttystatus.Widget):
'''Display percent of task done.'''
+
+ static_width = False
def __init__(self, done_name, total_name, decimals=0):
self.done_name = done_name
@@ -28,7 +30,7 @@ class PercentDone(ttystatus.Widget):
self.done = 0
self.total = 1
- def render(self):
+ def render(self, render):
try:
done = float(self.done)
total = float(self.total)
diff --git a/ttystatus/percent_tests.py b/ttystatus/percent_tests.py
index c7d8d64..9ffe0b9 100644
--- a/ttystatus/percent_tests.py
+++ b/ttystatus/percent_tests.py
@@ -24,18 +24,21 @@ class PercentDoneTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.PercentDone('done', 'total', decimals=1)
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
+
def test_shows_zero_value_initially(self):
- self.assertEqual(self.w.render(), '0.0 %')
+ self.assertEqual(self.w.render(0), '0.0 %')
def test_sets_value(self):
self.w.update({ 'done': 50, 'total': 100 })
- self.assertEqual(self.w.render(), '50.0 %')
+ self.assertEqual(self.w.render(0), '50.0 %')
def test_handles_empty_strings_as_values(self):
self.w.update({ 'done': '', 'total': '' })
- self.assertEqual(self.w.render(), '0.0 %')
+ self.assertEqual(self.w.render(0), '0.0 %')
def test_handles_zero_total(self):
self.w.update({ 'done': 0, 'total': 0 })
- self.assertEqual(self.w.render(), '0.0 %')
+ self.assertEqual(self.w.render(0), '0.0 %')
diff --git a/ttystatus/progressbar.py b/ttystatus/progressbar.py
index 54ca99f..cd0f1a3 100644
--- a/ttystatus/progressbar.py
+++ b/ttystatus/progressbar.py
@@ -20,15 +20,16 @@ import ttystatus
class ProgressBar(ttystatus.Widget):
'''Display a progress bar.'''
+
+ static_width = False
- def __init__(self, done_name, total_name, width):
+ def __init__(self, done_name, total_name):
self.done_name = done_name
self.total_name = total_name
self.done = 0
self.total = 1
- self.width = width
- def render(self):
+ def render(self, width):
try:
done = float(self.done)
total = float(self.total)
@@ -39,8 +40,8 @@ class ProgressBar(ttystatus.Widget):
fraction = 0
else:
fraction = done / total
- n_stars = int(round(fraction * self.width))
- n_dashes = int(self.width - n_stars)
+ n_stars = int(round(fraction * width))
+ n_dashes = int(width - n_stars)
return ('#' * n_stars) + ('-' * n_dashes)
def update(self, master):
diff --git a/ttystatus/progressbar_tests.py b/ttystatus/progressbar_tests.py
index 8c7c261..a5dd425 100644
--- a/ttystatus/progressbar_tests.py
+++ b/ttystatus/progressbar_tests.py
@@ -22,40 +22,44 @@ import ttystatus
class ProgressBarTests(unittest.TestCase):
def setUp(self):
- self.w = ttystatus.ProgressBar('done', 'total', 10)
+ self.w = ttystatus.ProgressBar('done', 'total')
+ self.width = 10
+
+ def test_is_not_static_width(self):
+ self.assertFalse(self.w.static_width)
def test_sets_initial_value_to_empty(self):
- self.assertEqual(self.w.render(), '-' * 10)
+ self.assertEqual(self.w.render(self.width), '-' * 10)
def test_shows_zero_percent_for_empty_string_total(self):
self.w.update({ 'done': 1, 'total': '' })
- self.assertEqual(self.w.render(), '-' * 10)
+ self.assertEqual(self.w.render(self.width), '-' * 10)
def test_shows_zero_percent_for_zero_total(self):
self.w.update({ 'done': 1, 'total': 0 })
- self.assertEqual(self.w.render(), '-' * 10)
+ self.assertEqual(self.w.render(self.width), '-' * 10)
def test_shows_zero_percent_correctly(self):
self.w.update({ 'done': 0, 'total': 100 })
- self.assertEqual(self.w.render(), '-' * 10)
+ self.assertEqual(self.w.render(self.width), '-' * 10)
def test_shows_one_percent_correctly(self):
self.w.update({ 'done': 1, 'total': 100 })
- self.assertEqual(self.w.render(), '-' * 10)
+ self.assertEqual(self.w.render(self.width), '-' * 10)
def test_shows_ten_percent_correctly(self):
self.w.update({ 'done': 10, 'total': 100 })
- self.assertEqual(self.w.render(), '#' + '-' * 9)
+ self.assertEqual(self.w.render(self.width), '#' + '-' * 9)
def test_shows_ninety_percent_correctly(self):
self.w.update({ 'done': 90, 'total': 100 })
- self.assertEqual(self.w.render(), '#' * 9 + '-')
+ self.assertEqual(self.w.render(self.width), '#' * 9 + '-')
def test_shows_ninety_ine_percent_correctly(self):
self.w.update({ 'done': 99, 'total': 100 })
- self.assertEqual(self.w.render(), '#' * 10)
+ self.assertEqual(self.w.render(self.width), '#' * 10)
def test_shows_one_hundred_percent_correctly(self):
self.w.update({ 'done': 100, 'total': 100 })
- self.assertEqual(self.w.render(), '#' * 10)
+ self.assertEqual(self.w.render(self.width), '#' * 10)
diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py
index 50529e6..cbf55bd 100644
--- a/ttystatus/remtime.py
+++ b/ttystatus/remtime.py
@@ -41,7 +41,7 @@ class RemainingTime(ttystatus.Widget):
return time.time()
- def render(self):
+ def render(self, render):
if self.started is None:
self.started = self.get_time()
duration = self.get_time() - self.started
diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py
index df6bced..90afd42 100644
--- a/ttystatus/remtime_tests.py
+++ b/ttystatus/remtime_tests.py
@@ -25,31 +25,34 @@ class RemainingTimeTests(unittest.TestCase):
self.w = ttystatus.RemainingTime('done', 'total')
self.w.get_time = lambda: 0.0
+ def test_is_static_width(self):
+ self.assertTrue(self.w.static_width)
+
def test_is_dashes_initially(self):
- self.assertEqual(self.w.render(), '--h--m--s')
+ self.assertEqual(self.w.render(0), '--h--m--s')
def test_estimates_and_formats_correctly(self):
- self.assertEqual(self.w.render(), '--h--m--s')
+ self.assertEqual(self.w.render(0), '--h--m--s')
self.w.update({ 'done': 0, 'total': 100 })
self.w.get_time = lambda: 5.0
self.w.update({ 'done': 5, 'total': 100 })
- self.assertEqual(self.w.render(), '00h01m35s')
+ self.assertEqual(self.w.render(0), '00h01m35s')
self.w.get_time = lambda: 10.0
self.w.update({ 'done': 5, 'total': 100 })
- self.assertEqual(self.w.render(), '00h03m10s')
+ self.assertEqual(self.w.render(0), '00h03m10s')
self.w.get_time = lambda: 20.0
self.w.update({ 'done': 80, 'total': 100 })
- self.assertEqual(self.w.render(), '00h00m05s')
+ self.assertEqual(self.w.render(0), '00h00m05s')
def test_handles_zero_speed(self):
self.w.update({ 'done': 0, 'total': 100 })
self.w.get_time = lambda: 5.0
self.w.update({ 'done': 0, 'total': 100 })
- self.assertEqual(self.w.render(), '--h--m--s')
+ self.assertEqual(self.w.render(0), '--h--m--s')
def test_handles_empty_strings_for_done_and_total(self):
self.w.update({ 'done': '', 'total': '' })
self.w.get_time = lambda: 5.0
self.w.update({ 'done': '', 'total': '' })
- self.assertEqual(self.w.render(), '--h--m--s')
+ self.assertEqual(self.w.render(0), '--h--m--s')
diff --git a/ttystatus/status.py b/ttystatus/status.py
index 526029b..8365017 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -76,7 +76,7 @@ class TerminalStatus(object):
def _render(self):
'''Format and output all widgets.'''
- self._m.write(''.join(w.render() for w in self._widgets))
+ self._m.write(''.join(w.render(1) 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 bb581f6..500cde2 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -94,9 +94,9 @@ class TerminalStatusTests(unittest.TestCase):
def test_updates_widgets_when_value_is_set(self):
w = ttystatus.String('foo')
self.ts.add(w)
- self.assertEqual(w.render(), '')
+ self.assertEqual(w.render(0), '')
self.ts['foo'] = 'bar'
- self.assertEqual(w.render(), 'bar')
+ self.assertEqual(w.render(0), 'bar')
def test_increases_value(self):
self.ts['foo'] = 10
@@ -128,5 +128,5 @@ class TerminalStatusTests(unittest.TestCase):
self.ts.add(w)
for i in range(n):
self.ts['value'] = i
- self.assertEqual(w.render(), str(n))
+ self.assertEqual(w.render(0), str(n))
diff --git a/ttystatus/string.py b/ttystatus/string.py
index 90fb48b..92fbf88 100644
--- a/ttystatus/string.py
+++ b/ttystatus/string.py
@@ -20,12 +20,14 @@ import ttystatus
class String(ttystatus.Widget):
'''Display a value as a string.'''
+
+ static_width = False
def __init__(self, key):
self._key = key
self.value = ''
- def render(self):
+ def render(self, render):
return str(self.value)
def update(self, master):
diff --git a/ttystatus/string_tests.py b/ttystatus/string_tests.py
index b377f37..faa266b 100644
--- a/ttystatus/string_tests.py
+++ b/ttystatus/string_tests.py
@@ -24,13 +24,16 @@ class StringTests(unittest.TestCase):
def setUp(self):
self.s = ttystatus.String('foo')
+ def test_is_not_static_width(self):
+ self.assertFalse(self.s.static_width)
+
def test_is_empty_initially(self):
- self.assertEqual(self.s.render(), '')
+ self.assertEqual(self.s.render(0), '')
def test_updates(self):
self.s.update({'foo': 'bar'})
- self.assertEqual(self.s.render(), 'bar')
+ self.assertEqual(self.s.render(0), 'bar')
def test_handles_non_string_value(self):
self.s.update({'foo': 123})
- self.assertEqual(self.s.render(), '123')
+ self.assertEqual(self.s.render(0), '123')
diff --git a/ttystatus/widget.py b/ttystatus/widget.py
index 9fccc29..9d14683 100644
--- a/ttystatus/widget.py
+++ b/ttystatus/widget.py
@@ -28,14 +28,29 @@ class Widget(object):
any of the values change
* the widget `render` method is called by TerminalStatus when it is
time to display things
+
+ Widgets may have a static size, or their size may vary. The
+ ``static_width`` property reveals this. This affects rendering:
+ static sized widgets are rendered at their one static size; variable
+ sized widgets are shrunk, if necessary, to make everything fit into
+ the available space. If it's not possible to shrink enough, widgets
+ are rendered from beginning until the space is full: variable sized
+ widgets are rendered as small as possible in this case.
'''
+ static_width = True
+
def __str__(self):
raise NotImplementedError()
- def render(self):
+ def render(self, width):
'''Format the current value.
+
+ ``width`` is the available width for the widget. It need not use
+ all of it. If it's not possible for the widget to render itself
+ small enough to fit into the given width, it may return a larger
+ string, but the caller will probably truncate it.
This will be called only when the value actually needs to be
formatted.