summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-13 19:55:47 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-13 19:55:47 +1200
commit705f90a2108a22c4cf1ab334ebb98b1f00620fa3 (patch)
tree5023c2549e39abcf8918437b898a292b3177d6e3
parentb55468fe09e8c2d26f3ae6e6573d1e390f52ee8f (diff)
downloadpython-ttystatus-705f90a2108a22c4cf1ab334ebb98b1f00620fa3.tar.gz
Add RemainingTime widget.
-rw-r--r--ttystatus/__init__.py1
-rw-r--r--ttystatus/remtime.py56
-rw-r--r--ttystatus/remtime_tests.py40
3 files changed, 97 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py
index ade6907..aa97dd0 100644
--- a/ttystatus/__init__.py
+++ b/ttystatus/__init__.py
@@ -28,3 +28,4 @@ from counter import Counter
from index import Index
from percent import PercentDone
from progressbar import ProgressBar
+from remtime import RemainingTime
diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py
new file mode 100644
index 0000000..07fb8bb
--- /dev/null
+++ b/ttystatus/remtime.py
@@ -0,0 +1,56 @@
+# 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 RemainingTime(ttystatus.Widget):
+
+ '''Display an estimate of the remaining time.'''
+
+ def __init__(self, done_name, total_name):
+ self.done_name = done_name
+ self.total_name = total_name
+ self.started = None
+ self.value = self.format(0)
+
+ def get_time(self): # pragma: no cover
+ '''Return current time.
+
+ This is just a wrapper around time.time() so that it is easier
+ to override during unit tests.
+
+ '''
+
+ return time.time()
+
+ def format(self, secs):
+ hours = secs / (60 * 60)
+ secs %= (60 * 60)
+ 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()
+ duration = self.get_time() - self.started
+ if duration >= 5.0:
+ speed = master[self.done_name] / duration
+ remaining = master[self.total_name] - master[self.done_name]
+ self.value = self.format(remaining / speed)
diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py
new file mode 100644
index 0000000..7e8abe3
--- /dev/null
+++ b/ttystatus/remtime_tests.py
@@ -0,0 +1,40 @@
+# 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 RemainingTimeTests(unittest.TestCase):
+
+ def setUp(self):
+ self.w = ttystatus.RemainingTime('done', 'total')
+ self.w.get_time = lambda: 0
+
+ def test_is_zero_initially(self):
+ self.assertEqual(str(self.w), '00h00m00s')
+
+ def test_formats_zero_correctly(self):
+ self.w.update({ 'done': 0, 'total': 0 }, 999)
+ self.assertEqual(str(self.w), '00h00m00s')
+
+ def test_formats_nonzero_correctly(self):
+ self.w.update({ 'done': 0, 'total': 100 }, 999)
+ self.w.get_time = lambda: 5
+ self.w.update({ 'done': 5, 'total': 100 }, 999)
+ self.assertEqual(str(self.w), '00h01m35s')
+