diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-06-11 19:14:51 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-06-11 19:14:51 +1200 |
commit | 1df68e5e213eaca5e7e76f8bbed45c4909d1f3da (patch) | |
tree | d86bb38446afb2d539ccaf72ea6dbfbc2076610b | |
parent | fadd1fe22cb7fa3f5b97bfbd0d0420a69e9aef3a (diff) | |
download | python-ttystatus-1df68e5e213eaca5e7e76f8bbed45c4909d1f3da.tar.gz |
Add PercentDone widget.
-rw-r--r-- | ttystatus/__init__.py | 1 | ||||
-rw-r--r-- | ttystatus/percent.py | 35 | ||||
-rw-r--r-- | ttystatus/percent_tests.py | 33 |
3 files changed, 69 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py index 60a29c5..11c6f72 100644 --- a/ttystatus/__init__.py +++ b/ttystatus/__init__.py @@ -26,3 +26,4 @@ from pathname import Pathname from bytesize import ByteSize from counter import Counter from index import Index +from percent import PercentDone diff --git a/ttystatus/percent.py b/ttystatus/percent.py new file mode 100644 index 0000000..6b3d1d5 --- /dev/null +++ b/ttystatus/percent.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 PercentDone(ttystatus.Widget): + + '''Display percent of task done.''' + + def __init__(self, done_name, total_name, decimals=0): + self.done_name = done_name + self.total_name = total_name + self.decimals = decimals + self.value = self.format(0, 1) + + def format(self, done, total): + return '%.*f %%' % (self.decimals, 100.0 * float(done) / total) + + def update(self, master, width): + self.value = self.format(master[self.done_name], + master[self.total_name]) diff --git a/ttystatus/percent_tests.py b/ttystatus/percent_tests.py new file mode 100644 index 0000000..15a907a --- /dev/null +++ b/ttystatus/percent_tests.py @@ -0,0 +1,33 @@ +# 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 PercentDoneTests(unittest.TestCase): + + def setUp(self): + self.w = ttystatus.PercentDone('done', 'total', decimals=1) + + def test_shows_zero_value_initially(self): + self.assertEqual(str(self.w), '0.0 %') + + def test_sets_value(self): + self.w.update({ 'done': 50, 'total': 100 }, 999) + self.assertEqual(str(self.w), '50.0 %') + |