From fadd1fe22cb7fa3f5b97bfbd0d0420a69e9aef3a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 11 Jun 2010 19:07:24 +1200 Subject: Add Index widget. --- ttystatus/__init__.py | 1 + ttystatus/index.py | 33 +++++++++++++++++++++++++++++++++ ttystatus/index_tests.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 ttystatus/index.py create mode 100644 ttystatus/index_tests.py 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 . + + +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 . + + +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') + -- cgit v1.2.1