summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-13 20:52:45 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-13 20:52:45 +1200
commitf188b3cee956afe0b7264712a7e47e5875603367 (patch)
tree3a233cb0caa6dce8d69d62f96458b5d8b1b1b959
parent705f90a2108a22c4cf1ab334ebb98b1f00620fa3 (diff)
downloadpython-ttystatus-f188b3cee956afe0b7264712a7e47e5875603367.tar.gz
Add RemainingTime to example.py.
Updates to RemainingTime tests, as well.
-rw-r--r--example.py8
-rw-r--r--ttystatus/remtime.py13
-rw-r--r--ttystatus/remtime_tests.py20
3 files changed, 24 insertions, 17 deletions
diff --git a/example.py b/example.py
index e12d0d7..a1a2c73 100644
--- a/example.py
+++ b/example.py
@@ -40,13 +40,15 @@ def main():
ts.clear()
ts.add(ttystatus.Literal('Finding symlinks: '))
+ ts.add(ttystatus.Counter('symlink'))
+ ts.add(ttystatus.Literal(' found; now at '))
ts.add(ttystatus.Index('pathname', 'pathnames'))
ts.add(ttystatus.Literal(' ('))
ts.add(ttystatus.PercentDone('done', 'total', decimals=2))
ts.add(ttystatus.Literal(' done) '))
+ ts.add(ttystatus.RemainingTime('done', 'total'))
+ ts.add(ttystatus.Literal(' '))
ts.add(ttystatus.ProgressBar('done', 'total'))
- ts.add(ttystatus.Counter('symlink'))
- ts.add(ttystatus.Literal(' symlinks found'))
ts['pathnames'] = pathnames
ts['done'] = 0
ts['total'] = len(pathnames)
@@ -55,7 +57,7 @@ def main():
ts['pathname'] = pathname
if os.path.islink(pathname):
ts['symlink'] = pathname
- ts.notify('Symlink! %s' % pathname)
+# ts.notify('Symlink! %s' % pathname)
ts['done'] += 1
ts.finish()
diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py
index 07fb8bb..98a8643 100644
--- a/ttystatus/remtime.py
+++ b/ttystatus/remtime.py
@@ -27,7 +27,7 @@ class RemainingTime(ttystatus.Widget):
self.done_name = done_name
self.total_name = total_name
self.started = None
- self.value = self.format(0)
+ self.value = '--h--m--s'
def get_time(self): # pragma: no cover
'''Return current time.
@@ -50,7 +50,10 @@ class RemainingTime(ttystatus.Widget):
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)
+ if duration >= 1.0:
+ done = float(master[self.done_name])
+ total = float(master[self.total_name])
+ speed = done / duration
+ remaining = total - done
+ secs = remaining / speed
+ self.value = self.format(secs)
diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py
index 7e8abe3..817048b 100644
--- a/ttystatus/remtime_tests.py
+++ b/ttystatus/remtime_tests.py
@@ -23,18 +23,20 @@ class RemainingTimeTests(unittest.TestCase):
def setUp(self):
self.w = ttystatus.RemainingTime('done', 'total')
- self.w.get_time = lambda: 0
+ self.w.get_time = lambda: 0.0
- def test_is_zero_initially(self):
- self.assertEqual(str(self.w), '00h00m00s')
+ def test_is_dashes_initially(self):
+ self.assertEqual(str(self.w), '--h--m--s')
- 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):
+ def test_estimates_and_formats_correctly(self):
self.w.update({ 'done': 0, 'total': 100 }, 999)
- self.w.get_time = lambda: 5
+ self.w.get_time = lambda: 5.0
self.w.update({ 'done': 5, 'total': 100 }, 999)
self.assertEqual(str(self.w), '00h01m35s')
+ self.w.get_time = lambda: 10.0
+ self.w.update({ 'done': 5, 'total': 100 }, 999)
+ self.assertEqual(str(self.w), '00h03m10s')
+ self.w.get_time = lambda: 20.0
+ self.w.update({ 'done': 80, 'total': 100 }, 999)
+ self.assertEqual(str(self.w), '00h00m05s')