diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-02-12 21:58:57 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-02-12 21:58:57 +0000 |
commit | 6afbe1c0b35317ef1571c2b429c2b5a6cf65ca6c (patch) | |
tree | b121d34b1d914bc6b21dd7dbc704329bcb6b5ade | |
parent | 215a78a512b24cab53d357f055a1eaa8ccd744df (diff) | |
download | python-ttystatus-6afbe1c0b35317ef1571c2b429c2b5a6cf65ca6c.tar.gz |
Add ttystatus.Integer.
-rw-r--r-- | ttystatus/__init__.py | 1 | ||||
-rw-r--r-- | ttystatus/integer.py | 37 | ||||
-rw-r--r-- | ttystatus/integer_tests.py | 37 |
3 files changed, 75 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py index 8194184..205824d 100644 --- a/ttystatus/__init__.py +++ b/ttystatus/__init__.py @@ -22,6 +22,7 @@ from widget import Widget from literal import Literal from string import String +from integer import Integer from pathname import Pathname from bytesize import ByteSize from counter import Counter diff --git a/ttystatus/integer.py b/ttystatus/integer.py new file mode 100644 index 0000000..b86dbe0 --- /dev/null +++ b/ttystatus/integer.py @@ -0,0 +1,37 @@ +# Copyright 2011 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 Integer(ttystatus.Widget): + + '''Display a value as an integer.''' + + def __init__(self, key): + self._key = key + self.interesting_keys = [key] + self.value = '#' + + def format(self): + return self.value + + def update(self, master, width): + try: + self.value = str(int(master[self._key])) + except ValueError: + self.value = '#' + diff --git a/ttystatus/integer_tests.py b/ttystatus/integer_tests.py new file mode 100644 index 0000000..a3f04fe --- /dev/null +++ b/ttystatus/integer_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 IntegerTests(unittest.TestCase): + + def setUp(self): + self.w = ttystatus.Integer('foo') + + def test_is_error_initially(self): + self.assertEqual(str(self.w), '#') + + def test_updates(self): + self.w.update({'foo': 123}, 999) + self.assertEqual(str(self.w), '123') + + def test_becomes_error_symbol_if_value_is_not_integer(self): + self.w.update({'foo': 'bar'}, 999) + self.assertEqual(str(self.w), '#') + |