summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-11 18:53:17 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-11 18:53:17 +1200
commitae68f3386f6b1d125c8f26de308f8160e320427d (patch)
treeee6be266430a563dc48659506eddbc64587e4b2a
parent3b570cc2edaa880a915fc3fb2cf4cf338396d48b (diff)
downloadpython-ttystatus-ae68f3386f6b1d125c8f26de308f8160e320427d.tar.gz
Add ByteSize widget.
-rw-r--r--ttystatus/__init__.py1
-rw-r--r--ttystatus/bytesize.py44
-rw-r--r--ttystatus/bytesize_tests.py57
3 files changed, 102 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py
index 6194400..b28fcf0 100644
--- a/ttystatus/__init__.py
+++ b/ttystatus/__init__.py
@@ -23,3 +23,4 @@ from widget import Widget
from literal import Literal
from string import String
from pathname import Pathname
+from bytesize import ByteSize
diff --git a/ttystatus/bytesize.py b/ttystatus/bytesize.py
new file mode 100644
index 0000000..0652b2f
--- /dev/null
+++ b/ttystatus/bytesize.py
@@ -0,0 +1,44 @@
+# 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 ByteSize(ttystatus.Widget):
+
+ '''Display data size in bytes, KiB, etc.'''
+
+ def __init__(self, name):
+ self.name = name
+ self.value = self.format(0)
+
+ def format(self, bytes):
+ units = (
+ (1024**4, 2, 'TiB'),
+ (1024**3, 2, 'GiB'),
+ (1024**2, 2, 'MiB'),
+ (1024**1, 1, 'KiB'),
+ )
+
+ for factor, decimals, unit in units:
+ if bytes >= factor:
+ return '%.*f %s' % (decimals,
+ float(bytes) / float(factor),
+ unit)
+ return '%d B' % bytes
+
+ def update(self, master, width):
+ self.value = self.format(master[self.name])
diff --git a/ttystatus/bytesize_tests.py b/ttystatus/bytesize_tests.py
new file mode 100644
index 0000000..11958f4
--- /dev/null
+++ b/ttystatus/bytesize_tests.py
@@ -0,0 +1,57 @@
+# 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 ByteSizeTests(unittest.TestCase):
+
+ def setUp(self):
+ self.w = ttystatus.ByteSize('foo')
+
+ def test_formats_zero_bytes_without_update(self):
+ self.assertEqual(str(self.w), '0 B')
+
+ def test_formats_zero_bytes_correctly(self):
+ self.w.update({ 'foo': 0 }, 999)
+ self.assertEqual(str(self.w), '0 B')
+
+ def test_formats_one_bytes_correctly(self):
+ self.w.update({ 'foo': 1 }, 999)
+ self.assertEqual(str(self.w), '1 B')
+
+ def test_formats_1023_bytes_correctly(self):
+ self.w.update({ 'foo': 1023 }, 999)
+ self.assertEqual(str(self.w), '1023 B')
+
+ def test_formats_1024_bytes_correctly(self):
+ self.w.update({ 'foo': 1024 }, 999)
+ self.assertEqual(str(self.w), '1.0 KiB')
+
+ def test_formats_1_MiB_bytes_correctly(self):
+ self.w.update({ 'foo': 1024**2 }, 999)
+ self.assertEqual(str(self.w), '1.00 MiB')
+
+ def test_formats_1_GiB_bytes_correctly(self):
+ self.w.update({ 'foo': 1024**3 }, 999)
+ self.assertEqual(str(self.w), '1.00 GiB')
+
+ def test_formats_1_TiB_bytes_correctly(self):
+ self.w.update({ 'foo': 1024**4 }, 999)
+ self.assertEqual(str(self.w), '1.00 TiB')
+