From 6afbe1c0b35317ef1571c2b429c2b5a6cf65ca6c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 12 Feb 2011 21:58:57 +0000 Subject: Add ttystatus.Integer. --- ttystatus/__init__.py | 1 + ttystatus/integer.py | 37 +++++++++++++++++++++++++++++++++++++ ttystatus/integer_tests.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 ttystatus/integer.py create mode 100644 ttystatus/integer_tests.py 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 . + + +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 . + + +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), '#') + -- cgit v1.2.1