summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-14 14:52:25 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-14 14:52:25 +1200
commit9e0d092d09dccf7998ea392f8f70656694774b5a (patch)
treecfa264a45db5fedc6819852d072de1d297e93020
parentf188b3cee956afe0b7264712a7e47e5875603367 (diff)
downloadpython-ttystatus-9e0d092d09dccf7998ea392f8f70656694774b5a.tar.gz
Add ElapsedTime widget.
-rw-r--r--ttystatus/__init__.py1
-rw-r--r--ttystatus/elapsed.py46
-rw-r--r--ttystatus/elapsed_tests.py41
3 files changed, 88 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py
index aa97dd0..3cfc3cb 100644
--- a/ttystatus/__init__.py
+++ b/ttystatus/__init__.py
@@ -29,3 +29,4 @@ from index import Index
from percent import PercentDone
from progressbar import ProgressBar
from remtime import RemainingTime
+from elapsed import ElapsedTime
diff --git a/ttystatus/elapsed.py b/ttystatus/elapsed.py
new file mode 100644
index 0000000..bcea7df
--- /dev/null
+++ b/ttystatus/elapsed.py
@@ -0,0 +1,46 @@
+# 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 time
+
+import ttystatus
+
+
+class ElapsedTime(ttystatus.Widget):
+
+ '''Display elapsed time since widget was first updated.'''
+
+ def __init__(self):
+ self.started = None
+ self.value = self.format(0)
+
+ def get_time(self): # pragma: no cover
+ '''Wrapper around time.time() for unit tests to override.'''
+ return time.time()
+
+ def format(self, secs):
+ hours = secs / 3600
+ secs %= 3600
+ mins = secs / 60
+ secs %= 60
+ return '%02dh%02dm%02ds' % (hours, mins, secs)
+
+ def update(self, master, width):
+ if self.started is None:
+ self.started = self.get_time()
+ secs = self.get_time() - self.started
+ self.value = self.format(secs)
+
diff --git a/ttystatus/elapsed_tests.py b/ttystatus/elapsed_tests.py
new file mode 100644
index 0000000..dc2bfe7
--- /dev/null
+++ b/ttystatus/elapsed_tests.py
@@ -0,0 +1,41 @@
+# 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 ElapsedtimeTests(unittest.TestCase):
+
+ def setUp(self):
+ self.w = ttystatus.ElapsedTime()
+
+ def test_shows_zero_initially(self):
+ self.assertEqual(str(self.w), '00h00m00s')
+
+ def test_shows_zero_after_first_update(self):
+ self.w.get_time = lambda: 1
+ self.w.update({}, 999)
+ 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.get_time = lambda: 60*60 + 60 + 1
+ self.w.update({}, 999)
+ self.assertEqual(str(self.w), '01h01m01s')
+