From f7781f20a83d0b3c368dd78f59a9e0a674d28730 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Wed, 22 Feb 2017 21:18:41 +0100 Subject: [PATCH] Merge load bar and the load string --- px/px_load_bar.py | 25 ++++++++++++++++--------- px/px_top.py | 6 +++--- tests/px_load_bar_test.py | 20 ++++++++++---------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/px/px_load_bar.py b/px/px_load_bar.py index e595b3e..5a50e9e 100644 --- a/px/px_load_bar.py +++ b/px/px_load_bar.py @@ -36,15 +36,22 @@ def __init__(self, physical=None, logical=None): CSI = b"\x1b[" self.normal = CSI + b"m" - self.inverse = CSI + b"7m" - self.red = CSI + b"41m" - self.yellow = CSI + b"43m" - self.green = CSI + b"42m" + self.inverse = CSI + b"0;7m" + self.red = CSI + b"27;1;37;41m" + self.yellow = CSI + b"27;1;37;43m" + self.green = CSI + b"27;1;37;42m" - def _get_colored_bytes(self, load=None, columns=None): + def _get_colored_bytes(self, load=None, columns=None, text=""): "Yields pairs, with each pair containing a color and a byte" - max_value = 2.0 * self._physical + maxlength = columns - 2 # Leave room for a starting and an ending space + if len(text) > maxlength: + text = text[0:(maxlength - 1)] + text = text + ' ' * (maxlength - len(text)) + text = " " + text + " " + assert len(text) == columns + + max_value = self._physical if load > max_value: max_value = 1.0 * load @@ -79,9 +86,9 @@ def _get_colored_bytes(self, load=None, columns=None): if i >= normal_start: color = self.normal - yield (color, b' ') + yield (color, text[i].encode('utf-8')) - def get_bar(self, load=None, columns=None): + def get_bar(self, load=None, columns=None, text=""): if load is None: raise ValueError("Missing required parameter load=") @@ -90,7 +97,7 @@ def get_bar(self, load=None, columns=None): return_me = b'' color = self.normal - for color_and_byte in self._get_colored_bytes(load=load, columns=columns): + for color_and_byte in self._get_colored_bytes(load=load, columns=columns, text=text): if color_and_byte[0] != color: return_me += color_and_byte[0] color = color_and_byte[0] diff --git a/px/px_top.py b/px/px_top.py index 90d5550..6feae5a 100644 --- a/px/px_top.py +++ b/px/px_top.py @@ -153,10 +153,10 @@ def clear_screen(): def get_screen_lines(load_bar, baseline, rows, columns): load = px_load.get_load_values() - loadstring = px_load.get_load_string(load).encode('utf-8') - loadbar = load_bar.get_bar(load=load[0], columns=40) + loadstring = px_load.get_load_string(load) + loadbar = load_bar.get_bar(load=load[0], columns=40, text=loadstring) lines = [ - b"System load: " + loadstring + b" |" + loadbar + b"|", + b"System load: " + loadbar, b""] toplist_table_lines = px_terminal.to_screen_lines(get_toplist(baseline), columns) diff --git a/tests/px_load_bar_test.py b/tests/px_load_bar_test.py index 3eaaf25..e0f25a7 100644 --- a/tests/px_load_bar_test.py +++ b/tests/px_load_bar_test.py @@ -14,19 +14,19 @@ def get_load_bar(physical=None, logical=None): def test_single_core_no_ht(): test_me = get_load_bar(1, 1) - assert test_me.get_bar(load=0, columns=10) == b'i n ' - assert test_me.get_bar(load=1, columns=10) == b'g n ' + assert test_me.get_bar(load=0, columns=10) == b'i n' + assert test_me.get_bar(load=1, columns=10) == b'g n' assert test_me.get_bar(load=2, columns=10) == b'g r n' assert test_me.get_bar(load=4, columns=12) == b'g r n' - assert test_me.get_bar(load=0.5, columns=12) == b'g i n ' + assert test_me.get_bar(load=0.5, columns=12) == b'g i n' def test_single_core_with_ht(): test_me = get_load_bar(1, 2) - assert test_me.get_bar(load=0, columns=10) == b'i n ' - assert test_me.get_bar(load=1, columns=10) == b'g n ' + assert test_me.get_bar(load=0, columns=10) == b'i n' + assert test_me.get_bar(load=1, columns=10) == b'g n' assert test_me.get_bar(load=2, columns=10) == b'g y n' assert test_me.get_bar(load=3, columns=12) == b'g y r n' assert test_me.get_bar(load=4, columns=12) == b'g y r n' @@ -35,15 +35,15 @@ def test_single_core_with_ht(): def test_dual_core_no_ht(): test_me = get_load_bar(2, 2) - assert test_me.get_bar(load=0, columns=10) == b'i n ' - assert test_me.get_bar(load=2, columns=10) == b'g n ' + assert test_me.get_bar(load=0, columns=10) == b'i n' + assert test_me.get_bar(load=2, columns=10) == b'g n' assert test_me.get_bar(load=4, columns=10) == b'g r n' assert test_me.get_bar(load=6, columns=12) == b'g r n' def test_rounding(): - test_me = get_load_bar(1, 1) + test_me = get_load_bar(2, 2) - assert test_me.get_bar(load=0.49, columns=2) == b'i n ' - assert test_me.get_bar(load=0.51, columns=2) == b'g n ' + assert test_me.get_bar(load=0.49, columns=2) == b'i n' + assert test_me.get_bar(load=0.51, columns=2) == b'g i n' assert test_me.get_bar(load=1000, columns=2) == b'r n'