diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-06-11 19:07:24 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-06-11 19:07:24 +1200 |
commit | fadd1fe22cb7fa3f5b97bfbd0d0420a69e9aef3a (patch) | |
tree | 9dc2ee4e0585faecbeafd2b60fab1a3815dd5604 | |
parent | c8151180d084a7c91c508024be159ee5b71f18a3 (diff) | |
download | python-ttystatus-fadd1fe22cb7fa3f5b97bfbd0d0420a69e9aef3a.tar.gz |
Add Index widget.
-rw-r--r-- | ttystatus/__init__.py | 1 | ||||
-rw-r--r-- | ttystatus/index.py | 33 | ||||
-rw-r--r-- | ttystatus/index_tests.py | 37 |
3 files changed, 71 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py index 29c215e..60a29c5 100644 --- a/ttystatus/__init__.py +++ b/ttystatus/__init__.py @@ -25,3 +25,4 @@ from string import String from pathname import Pathname from bytesize import ByteSize from counter import Counter +from index import Index diff --git a/ttystatus/index.py b/ttystatus/index.py new file mode 100644 index 0000000..d085a40 --- /dev/null +++ b/ttystatus/index.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 ttystatus + + +class Index(ttystatus.Widget): + + '''Display the position of a value in a list of values.''' + + def __init__(self, name, listname): + self.name = name + self.listname = listname + self.value = '0' + + def update(self, master, width): + try: + self.value = str(master[self.listname].index(master[self.name])) + except ValueError: + pass diff --git a/ttystatus/index_tests.py b/ttystatus/index_tests.py new file mode 100644 index 0000000..4aceaba --- /dev/null +++ b/ttystatus/index_tests.py @@ -0,0 +1,37 @@ +# 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 IndexTests(unittest.TestCase): + + def setUp(self): + self.w = ttystatus.Index('foo', 'foos') + + def test_is_zero_initially(self): + self.assertEqual(str(self.w), '0') + + def test_gets_index_right(self): + self.w.update({ 'foo': 'x', 'foos': ['a', 'x', 'b'] }, 999) + self.assertEqual(str(self.w), '1') + + def test_handles_value_not_in_list(self): + self.w.update({ 'foo': 'xxx', 'foos': ['a', 'x', 'b'] }, 999) + self.assertEqual(str(self.w), '0') + |