diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-06-12 20:59:49 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-06-12 20:59:49 +1200 |
commit | 657dfc1efb70a4eb97fc0518e43e13425ac01fd7 (patch) | |
tree | 409fe0287698ce129601ca72a15c31b6dc264121 | |
parent | d2cad5f1d4a5193a47fddebd8e7ea611c1c5542b (diff) | |
download | python-ttystatus-657dfc1efb70a4eb97fc0518e43e13425ac01fd7.tar.gz |
Add ProgressBar widget.
-rw-r--r-- | ttystatus/__init__.py | 1 | ||||
-rw-r--r-- | ttystatus/progressbar.py | 35 | ||||
-rw-r--r-- | ttystatus/progressbar_tests.py | 53 |
3 files changed, 89 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py index 11c6f72..ade6907 100644 --- a/ttystatus/__init__.py +++ b/ttystatus/__init__.py @@ -27,3 +27,4 @@ from bytesize import ByteSize from counter import Counter from index import Index from percent import PercentDone +from progressbar import ProgressBar diff --git a/ttystatus/progressbar.py b/ttystatus/progressbar.py new file mode 100644 index 0000000..4ed9bf3 --- /dev/null +++ b/ttystatus/progressbar.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 ProgressBar(ttystatus.Widget): + + '''Display a progress bar.''' + + def __init__(self, done_name, total_name): + self.done_name = done_name + self.total_name = total_name + + def update(self, master, width): + done = float(master.get(self.done_name, 0)) + total = float(master.get(self.total_name, 1)) + fraction = done / total + n_stars = int(round(fraction * width)) + n_dashes = int(width - n_stars) + self.value = ('#' * n_stars) + ('-' * n_dashes) + diff --git a/ttystatus/progressbar_tests.py b/ttystatus/progressbar_tests.py new file mode 100644 index 0000000..a84a1f0 --- /dev/null +++ b/ttystatus/progressbar_tests.py @@ -0,0 +1,53 @@ +# 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 ProgressBarTests(unittest.TestCase): + + def setUp(self): + self.w = ttystatus.ProgressBar('done', 'total') + + def test_sets_initial_value_to_empty(self): + self.assertEqual(str(self.w), '') + + def test_shows_zero_percent_correctly(self): + self.w.update({ 'done': 0, 'total': 100 }, 10) + self.assertEqual(str(self.w), '-' * 10) + + def test_shows_one_percent_correctly(self): + self.w.update({ 'done': 1, 'total': 100 }, 10) + self.assertEqual(str(self.w), '-' * 10) + + def test_shows_ten_percent_correctly(self): + self.w.update({ 'done': 10, 'total': 100 }, 10) + self.assertEqual(str(self.w), '#' + '-' * 9) + + def test_shows_ninety_percent_correctly(self): + self.w.update({ 'done': 90, 'total': 100 }, 10) + self.assertEqual(str(self.w), '#' * 9 + '-') + + def test_shows_ninety_ine_percent_correctly(self): + self.w.update({ 'done': 99, 'total': 100 }, 10) + self.assertEqual(str(self.w), '#' * 10) + + def test_shows_one_hundred_percent_correctly(self): + self.w.update({ 'done': 100, 'total': 100 }, 10) + self.assertEqual(str(self.w), '#' * 10) + |