summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2012-04-15 15:29:01 +0100
committerLars Wirzenius <liw@liw.fi>2012-04-15 15:29:01 +0100
commitc54f82a88e9592afd6bd0232f50a121acb1238e9 (patch)
tree18fd1614d2d3417764a4ee2ad8dd8fd777ae0223
parent1623d3599bd057677158bfd2167798585d2afc44 (diff)
downloadpython-ttystatus-c54f82a88e9592afd6bd0232f50a121acb1238e9.tar.gz
Improve widget split between updating and rendering
a) don't use format, use __str__, since nothing calls format directly b) don't declare interest in keys; just don't compute rendered value in update method, only when actually rendering (in __str__) c) don't pass in the available width to update, since that is only relevant during rendering (this will need to be fixed later, currently we don't get width at all)
-rw-r--r--ttystatus/bytesize.py10
-rw-r--r--ttystatus/bytesize_tests.py14
-rw-r--r--ttystatus/bytespeed.py5
-rw-r--r--ttystatus/bytespeed_tests.py14
-rw-r--r--ttystatus/counter.py5
-rw-r--r--ttystatus/counter_tests.py10
-rw-r--r--ttystatus/elapsed.py5
-rw-r--r--ttystatus/elapsed_tests.py6
-rw-r--r--ttystatus/index.py5
-rw-r--r--ttystatus/index_tests.py4
-rw-r--r--ttystatus/integer.py5
-rw-r--r--ttystatus/integer_tests.py4
-rw-r--r--ttystatus/literal.py3
-rw-r--r--ttystatus/pathname.py16
-rw-r--r--ttystatus/pathname_tests.py21
-rw-r--r--ttystatus/percent.py5
-rw-r--r--ttystatus/percent_tests.py6
-rw-r--r--ttystatus/progressbar.py11
-rw-r--r--ttystatus/progressbar_tests.py20
-rw-r--r--ttystatus/remtime.py5
-rw-r--r--ttystatus/remtime_tests.py16
-rw-r--r--ttystatus/status.py12
-rw-r--r--ttystatus/status_tests.py16
-rw-r--r--ttystatus/string.py5
-rw-r--r--ttystatus/string_tests.py4
-rw-r--r--ttystatus/widget.py45
26 files changed, 93 insertions, 179 deletions
diff --git a/ttystatus/bytesize.py b/ttystatus/bytesize.py
index f47d453..4c338aa 100644
--- a/ttystatus/bytesize.py
+++ b/ttystatus/bytesize.py
@@ -23,10 +23,12 @@ class ByteSize(ttystatus.Widget):
def __init__(self, name):
self.name = name
- self.interesting_keys = [name]
self._bytes = 0
+
+ def update(self, ts):
+ self._bytes = ts[self.name]
- def format(self):
+ def __str__(self):
units = (
(1024**4, 2, 'TiB'),
(1024**3, 2, 'GiB'),
@@ -40,6 +42,4 @@ class ByteSize(ttystatus.Widget):
float(self._bytes) / float(factor),
unit)
return '%d B' % self._bytes
-
- def update(self, master, width):
- self._bytes = master[self.name]
+
diff --git a/ttystatus/bytesize_tests.py b/ttystatus/bytesize_tests.py
index 11958f4..d1dd94d 100644
--- a/ttystatus/bytesize_tests.py
+++ b/ttystatus/bytesize_tests.py
@@ -28,30 +28,30 @@ class ByteSizeTests(unittest.TestCase):
self.assertEqual(str(self.w), '0 B')
def test_formats_zero_bytes_correctly(self):
- self.w.update({ 'foo': 0 }, 999)
+ self.w.update({ 'foo': 0 })
self.assertEqual(str(self.w), '0 B')
def test_formats_one_bytes_correctly(self):
- self.w.update({ 'foo': 1 }, 999)
+ self.w.update({ 'foo': 1 })
self.assertEqual(str(self.w), '1 B')
def test_formats_1023_bytes_correctly(self):
- self.w.update({ 'foo': 1023 }, 999)
+ self.w.update({ 'foo': 1023 })
self.assertEqual(str(self.w), '1023 B')
def test_formats_1024_bytes_correctly(self):
- self.w.update({ 'foo': 1024 }, 999)
+ self.w.update({ 'foo': 1024 })
self.assertEqual(str(self.w), '1.0 KiB')
def test_formats_1_MiB_bytes_correctly(self):
- self.w.update({ 'foo': 1024**2 }, 999)
+ self.w.update({ 'foo': 1024**2 })
self.assertEqual(str(self.w), '1.00 MiB')
def test_formats_1_GiB_bytes_correctly(self):
- self.w.update({ 'foo': 1024**3 }, 999)
+ self.w.update({ 'foo': 1024**3 })
self.assertEqual(str(self.w), '1.00 GiB')
def test_formats_1_TiB_bytes_correctly(self):
- self.w.update({ 'foo': 1024**4 }, 999)
+ self.w.update({ 'foo': 1024**4 })
self.assertEqual(str(self.w), '1.00 TiB')
diff --git a/ttystatus/bytespeed.py b/ttystatus/bytespeed.py
index 7f54b86..cc62300 100644
--- a/ttystatus/bytespeed.py
+++ b/ttystatus/bytespeed.py
@@ -25,7 +25,6 @@ class ByteSpeed(ttystatus.Widget):
def __init__(self, name):
self.name = name
- self.interesting_keys = [name]
self._bytes = 0
self._started = None
@@ -34,7 +33,7 @@ class ByteSpeed(ttystatus.Widget):
return time.time()
- def format(self):
+ def __str__(self):
units = (
(1024**4, 2, 'TiB/s'),
(1024**3, 2, 'GiB/s'),
@@ -55,7 +54,7 @@ class ByteSpeed(ttystatus.Widget):
unit)
return '%.0f B/s' % speed
- def update(self, master, width):
+ def update(self, master):
if self._started is None:
self._started = self.now()
self._bytes = master[self.name]
diff --git a/ttystatus/bytespeed_tests.py b/ttystatus/bytespeed_tests.py
index 4c481fe..369c2f6 100644
--- a/ttystatus/bytespeed_tests.py
+++ b/ttystatus/bytespeed_tests.py
@@ -28,27 +28,27 @@ class ByteSpeedTests(unittest.TestCase):
self.assertEqual(str(self.w), '0 B/s')
def test_formats_zero_bytes_correctly(self):
- self.w.update({ 'foo': 0 }, 999)
+ self.w.update({ 'foo': 0 })
self.assertEqual(str(self.w), '0 B/s')
def test_formats_one_byte_per_second_correctly(self):
self.w.now = lambda: 1
- self.w.update({ 'foo': 0 }, 999)
+ self.w.update({ 'foo': 0 })
self.w.now = lambda: 2
- self.w.update({ 'foo': 1 }, 999)
+ self.w.update({ 'foo': 1 })
self.assertEqual(str(self.w), '1 B/s')
def test_formats_ten_bytes_per_second_correctly(self):
self.w.now = lambda: 1
- self.w.update({ 'foo': 0 }, 999)
+ self.w.update({ 'foo': 0 })
self.w.now = lambda: 11
- self.w.update({ 'foo': 100 }, 999)
+ self.w.update({ 'foo': 100 })
self.assertEqual(str(self.w), '10 B/s')
def test_formats_ten_tibs_per_second_correctly(self):
self.w.now = lambda: 1
- self.w.update({ 'foo': 0 }, 999)
+ self.w.update({ 'foo': 0 })
self.w.now = lambda: 2
- self.w.update({ 'foo': 10 * 1024**4 }, 999)
+ self.w.update({ 'foo': 10 * 1024**4 })
self.assertEqual(str(self.w), '10.00 TiB/s')
diff --git a/ttystatus/counter.py b/ttystatus/counter.py
index 5d3e11f..5b92791 100644
--- a/ttystatus/counter.py
+++ b/ttystatus/counter.py
@@ -25,12 +25,11 @@ class Counter(ttystatus.Widget):
self.name = name
self.prev = None
self.count = 0
- self.interesting_keys = [name]
- def format(self):
+ def __str__(self):
return str(self.count)
- def update(self, master, width):
+ def update(self, master):
if master[self.name] != self.prev:
self.prev = master[self.name]
self.count += 1
diff --git a/ttystatus/counter_tests.py b/ttystatus/counter_tests.py
index 866bcbd..f7dc3e1 100644
--- a/ttystatus/counter_tests.py
+++ b/ttystatus/counter_tests.py
@@ -28,16 +28,16 @@ class CounterTests(unittest.TestCase):
self.assertEqual(str(self.w), '0')
def test_counts_one_change(self):
- self.w.update({ 'foo': 'a' }, 999)
+ self.w.update({ 'foo': 'a' })
self.assertEqual(str(self.w), '1')
def test_counts_two_changes(self):
- self.w.update({ 'foo': 'a' }, 999)
- self.w.update({ 'foo': 'b' }, 999)
+ self.w.update({ 'foo': 'a' })
+ self.w.update({ 'foo': 'b' })
self.assertEqual(str(self.w), '2')
def test_does_not_count_if_value_does_not_change(self):
- self.w.update({ 'foo': 'a' }, 999)
- self.w.update({ 'foo': 'a' }, 999)
+ self.w.update({ 'foo': 'a' })
+ self.w.update({ 'foo': 'a' })
self.assertEqual(str(self.w), '1')
diff --git a/ttystatus/elapsed.py b/ttystatus/elapsed.py
index 2032e6e..242c092 100644
--- a/ttystatus/elapsed.py
+++ b/ttystatus/elapsed.py
@@ -25,14 +25,13 @@ class ElapsedTime(ttystatus.Widget):
def __init__(self):
self.started = None
- self.interesting_keys = None
self.secs = 0
def get_time(self): # pragma: no cover
'''Wrapper around time.time() for unit tests to override.'''
return time.time()
- def format(self):
+ def __str__(self):
secs = self.secs
hours = secs / 3600
secs %= 3600
@@ -40,7 +39,7 @@ class ElapsedTime(ttystatus.Widget):
secs %= 60
return '%02dh%02dm%02ds' % (hours, mins, secs)
- def update(self, master, width):
+ def update(self, master):
if self.started is None:
self.started = self.get_time()
self.secs = self.get_time() - self.started
diff --git a/ttystatus/elapsed_tests.py b/ttystatus/elapsed_tests.py
index dc2bfe7..9c82acb 100644
--- a/ttystatus/elapsed_tests.py
+++ b/ttystatus/elapsed_tests.py
@@ -29,13 +29,13 @@ class ElapsedtimeTests(unittest.TestCase):
def test_shows_zero_after_first_update(self):
self.w.get_time = lambda: 1
- self.w.update({}, 999)
+ self.w.update({})
self.assertEqual(str(self.w), '00h00m00s')
def test_shows_one_one_one_after_second_update(self):
self.w.get_time = lambda: 0
- self.w.update({}, 999)
+ self.w.update({})
self.w.get_time = lambda: 60*60 + 60 + 1
- self.w.update({}, 999)
+ self.w.update({})
self.assertEqual(str(self.w), '01h01m01s')
diff --git a/ttystatus/index.py b/ttystatus/index.py
index 941165a..daa13df 100644
--- a/ttystatus/index.py
+++ b/ttystatus/index.py
@@ -24,18 +24,17 @@ class Index(ttystatus.Widget):
def __init__(self, name, listname):
self.name = name
self.listname = listname
- self.interesting_keys = [name, listname]
self.value = None
self.listvalue = []
- def format(self):
+ def __str__(self):
try:
index = self.listvalue.index(self.value) + 1
except ValueError:
index = 0
return '%d/%d' % (index, len(self.listvalue))
- def update(self, master, width):
+ def update(self, master):
self.value = master[self.name]
self.listvalue = master[self.listname]
diff --git a/ttystatus/index_tests.py b/ttystatus/index_tests.py
index 3c91903..d3d420e 100644
--- a/ttystatus/index_tests.py
+++ b/ttystatus/index_tests.py
@@ -28,10 +28,10 @@ class IndexTests(unittest.TestCase):
self.assertEqual(str(self.w), '0/0')
def test_gets_index_right(self):
- self.w.update({ 'foo': 'x', 'foos': ['a', 'x', 'b'] }, 999)
+ self.w.update({ 'foo': 'x', 'foos': ['a', 'x', 'b'] })
self.assertEqual(str(self.w), '2/3')
def test_handles_value_not_in_list(self):
- self.w.update({ 'foo': 'xxx', 'foos': ['a', 'x', 'b'] }, 999)
+ self.w.update({ 'foo': 'xxx', 'foos': ['a', 'x', 'b'] })
self.assertEqual(str(self.w), '0/3')
diff --git a/ttystatus/integer.py b/ttystatus/integer.py
index b86dbe0..b02a79f 100644
--- a/ttystatus/integer.py
+++ b/ttystatus/integer.py
@@ -23,13 +23,12 @@ class Integer(ttystatus.Widget):
def __init__(self, key):
self._key = key
- self.interesting_keys = [key]
self.value = '#'
- def format(self):
+ def __str__(self):
return self.value
- def update(self, master, width):
+ def update(self, master):
try:
self.value = str(int(master[self._key]))
except ValueError:
diff --git a/ttystatus/integer_tests.py b/ttystatus/integer_tests.py
index a3f04fe..f5b3f54 100644
--- a/ttystatus/integer_tests.py
+++ b/ttystatus/integer_tests.py
@@ -28,10 +28,10 @@ class IntegerTests(unittest.TestCase):
self.assertEqual(str(self.w), '#')
def test_updates(self):
- self.w.update({'foo': 123}, 999)
+ self.w.update({'foo': 123})
self.assertEqual(str(self.w), '123')
def test_becomes_error_symbol_if_value_is_not_integer(self):
- self.w.update({'foo': 'bar'}, 999)
+ self.w.update({'foo': 'bar'})
self.assertEqual(str(self.w), '#')
diff --git a/ttystatus/literal.py b/ttystatus/literal.py
index 5c41b69..d0a1dc7 100644
--- a/ttystatus/literal.py
+++ b/ttystatus/literal.py
@@ -23,7 +23,6 @@ class Literal(ttystatus.Widget):
def __init__(self, string):
self.value = string
- self.interesting_keys = []
- def format(self):
+ def __str__(self):
return self.value
diff --git a/ttystatus/pathname.py b/ttystatus/pathname.py
index 0f5fb43..a8b8253 100644
--- a/ttystatus/pathname.py
+++ b/ttystatus/pathname.py
@@ -27,21 +27,11 @@ class Pathname(ttystatus.Widget):
def __init__(self, key):
self._key = key
- self.interesting_keys = [key]
self.pathname = ''
- self.width = 0
- def format(self):
- v = self.pathname
- if len(v) > self.width:
- ellipsis = '...'
- if len(ellipsis) < self.width:
- v = ellipsis + v[-(self.width - len(ellipsis)):]
- else:
- v = v[-self.width:]
- return v
+ def __str__(self):
+ return self.pathname
- def update(self, master, width):
+ def update(self, master):
self.pathname = master.get(self._key, '')
- self.width = width
diff --git a/ttystatus/pathname_tests.py b/ttystatus/pathname_tests.py
index f0c54d5..f9e62ef 100644
--- a/ttystatus/pathname_tests.py
+++ b/ttystatus/pathname_tests.py
@@ -28,26 +28,9 @@ class PathnameTests(unittest.TestCase):
self.assertEqual(str(self.w), '')
def test_updates(self):
- self.w.update({'foo': 'bar'}, 999)
+ self.w.update({'foo': 'bar'})
self.assertEqual(str(self.w), 'bar')
def test_handles_update_to_other_value(self):
- self.w.update({'other': 1}, 999)
+ self.w.update({'other': 1})
self.assertEqual(str(self.w), '')
-
- def test_truncates_from_beginning(self):
- self.w.update({'foo': 'foobar'}, 5)
- self.assertEqual(str(self.w), '...ar')
-
- def test_does_not_truncate_for_exact_fit(self):
- self.w.update({'foo': 'foobar'}, 6)
- self.assertEqual(str(self.w), 'foobar')
-
- def test_does_not_add_ellipsis_if_it_will_not_fit(self):
- self.w.update({'foo': 'foobar'}, 3)
- self.assertEqual(str(self.w), 'bar')
-
- def test_adds_ellipsis_if_it_just_fits(self):
- self.w.update({'foo': 'foobar'}, 4)
- self.assertEqual(str(self.w), '...r')
-
diff --git a/ttystatus/percent.py b/ttystatus/percent.py
index af2935c..840c047 100644
--- a/ttystatus/percent.py
+++ b/ttystatus/percent.py
@@ -27,9 +27,8 @@ class PercentDone(ttystatus.Widget):
self.decimals = decimals
self.done = 0
self.total = 1
- self.interesting_keys = [done_name, total_name]
- def format(self):
+ def __str__(self):
try:
done = float(self.done)
total = float(self.total)
@@ -40,6 +39,6 @@ class PercentDone(ttystatus.Widget):
total = 1
return '%.*f %%' % (self.decimals, 100.0 * done / total)
- def update(self, master, width):
+ def update(self, master):
self.done = master[self.done_name]
self.total = master[self.total_name]
diff --git a/ttystatus/percent_tests.py b/ttystatus/percent_tests.py
index 29c7cb4..5f46498 100644
--- a/ttystatus/percent_tests.py
+++ b/ttystatus/percent_tests.py
@@ -28,14 +28,14 @@ class PercentDoneTests(unittest.TestCase):
self.assertEqual(str(self.w), '0.0 %')
def test_sets_value(self):
- self.w.update({ 'done': 50, 'total': 100 }, 999)
+ self.w.update({ 'done': 50, 'total': 100 })
self.assertEqual(str(self.w), '50.0 %')
def test_handles_empty_strings_as_values(self):
- self.w.update({ 'done': '', 'total': '' }, 999)
+ self.w.update({ 'done': '', 'total': '' })
self.assertEqual(str(self.w), '0.0 %')
def test_handles_zero_total(self):
- self.w.update({ 'done': 0, 'total': 0 }, 999)
+ self.w.update({ 'done': 0, 'total': 0 })
self.assertEqual(str(self.w), '0.0 %')
diff --git a/ttystatus/progressbar.py b/ttystatus/progressbar.py
index e8549f2..d8189d3 100644
--- a/ttystatus/progressbar.py
+++ b/ttystatus/progressbar.py
@@ -21,15 +21,14 @@ class ProgressBar(ttystatus.Widget):
'''Display a progress bar.'''
- def __init__(self, done_name, total_name):
+ def __init__(self, done_name, total_name, width):
self.done_name = done_name
self.total_name = total_name
- self.interesting_keys = [done_name, total_name]
self.done = 0
self.total = 1
- self.width = 0
+ self.width = width
- def format(self):
+ def __str__(self):
try:
done = float(self.done)
total = float(self.total)
@@ -44,8 +43,6 @@ class ProgressBar(ttystatus.Widget):
n_dashes = int(self.width - n_stars)
return ('#' * n_stars) + ('-' * n_dashes)
- def update(self, master, width):
+ def update(self, master):
self.done = master[self.done_name]
self.total = master[self.total_name]
- self.width = width
-
diff --git a/ttystatus/progressbar_tests.py b/ttystatus/progressbar_tests.py
index 0e3bf5a..a229f46 100644
--- a/ttystatus/progressbar_tests.py
+++ b/ttystatus/progressbar_tests.py
@@ -22,40 +22,40 @@ import ttystatus
class ProgressBarTests(unittest.TestCase):
def setUp(self):
- self.w = ttystatus.ProgressBar('done', 'total')
+ self.w = ttystatus.ProgressBar('done', 'total', 10)
def test_sets_initial_value_to_empty(self):
- self.assertEqual(str(self.w), '')
+ self.assertEqual(str(self.w), '-' * 10)
def test_shows_zero_percent_for_empty_string_total(self):
- self.w.update({ 'done': 1, 'total': '' }, 10)
+ self.w.update({ 'done': 1, 'total': '' })
self.assertEqual(str(self.w), '-' * 10)
def test_shows_zero_percent_for_zero_total(self):
- self.w.update({ 'done': 1, 'total': 0 }, 10)
+ self.w.update({ 'done': 1, 'total': 0 })
self.assertEqual(str(self.w), '-' * 10)
def test_shows_zero_percent_correctly(self):
- self.w.update({ 'done': 0, 'total': 100 }, 10)
+ self.w.update({ 'done': 0, 'total': 100 })
self.assertEqual(str(self.w), '-' * 10)
def test_shows_one_percent_correctly(self):
- self.w.update({ 'done': 1, 'total': 100 }, 10)
+ self.w.update({ 'done': 1, 'total': 100 })
self.assertEqual(str(self.w), '-' * 10)
def test_shows_ten_percent_correctly(self):
- self.w.update({ 'done': 10, 'total': 100 }, 10)
+ self.w.update({ 'done': 10, 'total': 100 })
self.assertEqual(str(self.w), '#' + '-' * 9)
def test_shows_ninety_percent_correctly(self):
- self.w.update({ 'done': 90, 'total': 100 }, 10)
+ self.w.update({ 'done': 90, 'total': 100 })
self.assertEqual(str(self.w), '#' * 9 + '-')
def test_shows_ninety_ine_percent_correctly(self):
- self.w.update({ 'done': 99, 'total': 100 }, 10)
+ self.w.update({ 'done': 99, 'total': 100 })
self.assertEqual(str(self.w), '#' * 10)
def test_shows_one_hundred_percent_correctly(self):
- self.w.update({ 'done': 100, 'total': 100 }, 10)
+ self.w.update({ 'done': 100, 'total': 100 })
self.assertEqual(str(self.w), '#' * 10)
diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py
index fd0d07c..9d90895 100644
--- a/ttystatus/remtime.py
+++ b/ttystatus/remtime.py
@@ -28,7 +28,6 @@ class RemainingTime(ttystatus.Widget):
self.total_name = total_name
self.started = None
self.default = '--h--m--s'
- self.interesting_keys = [done_name, total_name]
self.done = 0
self.total = 1
@@ -42,7 +41,7 @@ class RemainingTime(ttystatus.Widget):
return time.time()
- def format(self):
+ def __str__(self):
if self.started is None:
self.started = self.get_time()
duration = self.get_time() - self.started
@@ -58,6 +57,6 @@ class RemainingTime(ttystatus.Widget):
return '%02dh%02dm%02ds' % (hours, mins, secs)
return self.default
- def update(self, master, width):
+ def update(self, master):
self.done = master[self.done_name]
self.total = master[self.total_name]
diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py
index 2c67345..dced388 100644
--- a/ttystatus/remtime_tests.py
+++ b/ttystatus/remtime_tests.py
@@ -30,26 +30,26 @@ class RemainingTimeTests(unittest.TestCase):
def test_estimates_and_formats_correctly(self):
self.assertEqual(str(self.w), '--h--m--s')
- self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.w.update({ 'done': 0, 'total': 100 })
self.w.get_time = lambda: 5.0
- self.w.update({ 'done': 5, 'total': 100 }, 999)
+ self.w.update({ 'done': 5, 'total': 100 })
self.assertEqual(str(self.w), '00h01m35s')
self.w.get_time = lambda: 10.0
- self.w.update({ 'done': 5, 'total': 100 }, 999)
+ self.w.update({ 'done': 5, 'total': 100 })
self.assertEqual(str(self.w), '00h03m10s')
self.w.get_time = lambda: 20.0
- self.w.update({ 'done': 80, 'total': 100 }, 999)
+ self.w.update({ 'done': 80, 'total': 100 })
self.assertEqual(str(self.w), '00h00m05s')
def test_handles_zero_speed(self):
- self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.w.update({ 'done': 0, 'total': 100 })
self.w.get_time = lambda: 5.0
- self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.w.update({ 'done': 0, 'total': 100 })
self.assertEqual(str(self.w), '--h--m--s')
def test_handles_empty_strings_for_done_and_total(self):
- self.w.update({ 'done': '', 'total': '' }, 999)
+ self.w.update({ 'done': '', 'total': '' })
self.w.get_time = lambda: 5.0
- self.w.update({ 'done': '', 'total': '' }, 999)
+ self.w.update({ 'done': '', 'total': '' })
self.assertEqual(str(self.w), '--h--m--s')
diff --git a/ttystatus/status.py b/ttystatus/status.py
index 25fd1c1..ca1f873 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -39,11 +39,6 @@ class TerminalStatus(object):
def add(self, widget):
'''Add a new widget to the status display.'''
self._widgets.append(widget)
- if widget.interesting_keys is None:
- self._wildcards += [widget]
- else:
- for key in widget.interesting_keys:
- self._interests[key] = self._interests.get(key, []) + [widget]
def format(self, format_string):
'''Add new widgets based on format string.
@@ -61,9 +56,6 @@ class TerminalStatus(object):
'''Remove all widgets.'''
self._widgets = []
self._values = dict()
- self._interests = dict()
- self._wildcards = list()
- self._latest_width = None
self._m.clear()
def __getitem__(self, key):
@@ -82,10 +74,8 @@ class TerminalStatus(object):
def _format(self):
'''Format and output all widgets.'''
- width = self._m.width
for w in self._widgets:
- w.update(self, width)
- width -= len(str(w))
+ w.update(self)
self._m.write(''.join(str(w) for w in self._widgets))
def increase(self, key, delta):
diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py
index 3583028..d333404 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -63,22 +63,6 @@ class TerminalStatusTests(unittest.TestCase):
self.ts.add(w)
self.assertEqual(self.ts._widgets, [w])
- def test_adds_widget_as_interested_in_keys(self):
- class W(ttystatus.Widget):
- def __init__(self):
- self.interesting_keys = ['foo']
- w = W()
- self.ts.add(w)
- self.assert_(w in self.ts._interests['foo'])
-
- def test_adds_widget_to_wildcards(self):
- class W(ttystatus.Widget):
- def __init__(self):
- self.interesting_keys = None
- w = W()
- self.ts.add(w)
- self.assert_(w in self.ts._wildcards)
-
def test_adds_widgets_from_format_string(self):
self.ts.format('hello, %String(name)')
self.assertEqual(len(self.ts._widgets), 2)
diff --git a/ttystatus/string.py b/ttystatus/string.py
index 32a76ea..a23a8e5 100644
--- a/ttystatus/string.py
+++ b/ttystatus/string.py
@@ -23,11 +23,10 @@ class String(ttystatus.Widget):
def __init__(self, key):
self._key = key
- self.interesting_keys = [key]
self.value = ''
- def format(self):
+ def __str__(self):
return str(self.value)
- def update(self, master, width):
+ def update(self, master):
self.value = master[self._key]
diff --git a/ttystatus/string_tests.py b/ttystatus/string_tests.py
index 39f480a..aad21f5 100644
--- a/ttystatus/string_tests.py
+++ b/ttystatus/string_tests.py
@@ -28,9 +28,9 @@ class StringTests(unittest.TestCase):
self.assertEqual(str(self.s), '')
def test_updates(self):
- self.s.update({'foo': 'bar'}, 999)
+ self.s.update({'foo': 'bar'})
self.assertEqual(str(self.s), 'bar')
def test_handles_non_string_value(self):
- self.s.update({'foo': 123}, 999)
+ self.s.update({'foo': 123})
self.assertEqual(str(self.s), '123')
diff --git a/ttystatus/widget.py b/ttystatus/widget.py
index 0f03365..17c2df4 100644
--- a/ttystatus/widget.py
+++ b/ttystatus/widget.py
@@ -18,36 +18,20 @@ class Widget(object):
'''Base class for ttystatus widgets.
- Widgets are responsible for formatting part of the output. They
- get a value or values either directly from the user, or from the
- master TerminalStatus widget. They return the formatted string
- via __str__.
+ Widgets display stuff on screen. The value may depend on data provided
+ by the user (at creation time), or may be computed from one or more
+ values in the TerminalStatus object to which the widget object belongs.
- A widget's value may be derived from values stored in the TerminalStatus
- widget (called master). Each such value has a key. Computing a widget's
- value is a two-step process: when the values associated with keys
- are updated, the widget's update() method is called to notify it of
- this. update() may compute intermediate values, such as maintain a
- counter of the number of changes. It should avoid costly operations
- that are only needed when the widget's formatted value is needed.
- Those should go into the format() method instead. Thus, update() would
- update a counter, format() would create a string representing the
- counter.
+ There are two steps:
- This is necessary because actual on-screen updates only happen
- every so often, not every time a value in the master changes, and
- often the string formatting part is expensive.
-
- Widgets must have an attribute 'interesting_keys', listing the
- keys it is interested in.
+ * the widget `update` method is called by TerminalStatus whenever
+ any of the values change
+ * the widget `__str__` method is called by TerminalStatus when it is
+ time to display things
'''
-
+
def __str__(self):
- '''Return current value to be displayed for this widget.'''
- return self.format()
-
- def format(self):
'''Format the current value.
This will be called only when the value actually needs to be
@@ -57,11 +41,6 @@ class Widget(object):
return ''
- def update(self, master, width):
- '''Update displayed value for widget, from values in master.
-
- 'width' gives the width for which the widget should aim to fit.
- It is OK if it does not: for some widgets there is no way to
- adjust to a smaller size.
-
- '''
+ def update(self, terminal_status):
+ '''React to changes in values stored in a TerminalStatus.'''
+