summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-11 19:00:38 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-11 19:00:38 +1200
commitc8151180d084a7c91c508024be159ee5b71f18a3 (patch)
tree7333f26e7bc4b33e6deba667ad9494fadb2b7aec
parentae68f3386f6b1d125c8f26de308f8160e320427d (diff)
downloadpython-ttystatus-c8151180d084a7c91c508024be159ee5b71f18a3.tar.gz
Add Counter widget.
-rw-r--r--ttystatus/__init__.py1
-rw-r--r--ttystatus/counter.py35
-rw-r--r--ttystatus/counter_tests.py43
3 files changed, 79 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py
index b28fcf0..29c215e 100644
--- a/ttystatus/__init__.py
+++ b/ttystatus/__init__.py
@@ -24,3 +24,4 @@ from literal import Literal
from string import String
from pathname import Pathname
from bytesize import ByteSize
+from counter import Counter
diff --git a/ttystatus/counter.py b/ttystatus/counter.py
new file mode 100644
index 0000000..b409114
--- /dev/null
+++ b/ttystatus/counter.py
@@ -0,0 +1,35 @@
+# Copyright 2010 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import ttystatus
+
+
+class Counter(ttystatus.Widget):
+
+ '''Display a count of how many times a value has changed.'''
+
+ def __init__(self, name):
+ self.name = name
+ self.prev = None
+ self.count = 0
+ self.value = '0'
+
+ def update(self, master, width):
+ if master[self.name] != self.prev:
+ self.prev = master[self.name]
+ self.count += 1
+ self.value = str(self.count)
+
diff --git a/ttystatus/counter_tests.py b/ttystatus/counter_tests.py
new file mode 100644
index 0000000..866bcbd
--- /dev/null
+++ b/ttystatus/counter_tests.py
@@ -0,0 +1,43 @@
+# Copyright 2010 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import unittest
+
+import ttystatus
+
+
+class CounterTests(unittest.TestCase):
+
+ def setUp(self):
+ self.w = ttystatus.Counter('foo')
+
+ def test_counts_zero_initially(self):
+ self.assertEqual(str(self.w), '0')
+
+ def test_counts_one_change(self):
+ self.w.update({ 'foo': 'a' }, 999)
+ 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.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.assertEqual(str(self.w), '1')
+