Skip to content

Commit

Permalink
Merge load bar and the load string
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Feb 23, 2017
1 parent 7531846 commit f7781f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
25 changes: 16 additions & 9 deletions px/px_load_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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=")

Expand All @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions px/px_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions tests/px_load_bar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

0 comments on commit f7781f2

Please sign in to comment.