Commit 97dda306 authored by Fjen Undso's avatar Fjen Undso
Browse files

cleanup

parent 8d5f58d4
......@@ -32,12 +32,10 @@ class CheckBox(Drawable):
def _get_text(self):
prefix = "[X] " if self.active else "[ ] "
return "{}{}".format(prefix, self.text)
return f"{prefix}{self.text}"
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = curses.A_STANDOUT
attr = curses.A_STANDOUT if self.focus else curses.A_NORMAL
self.window.move(self.row, self.col)
self.window.addstr(self._get_text(), attr)
self.window.refresh()
......@@ -45,10 +43,8 @@ class CheckBox(Drawable):
def send_key(self, key):
if key == self.toggle_key_unfocused:
self.toggle()
if not self.focus:
return
if key == self.toggle_key:
self.toggle()
......
......@@ -43,7 +43,6 @@ class FocusSwitcher(Focusable):
"""Switch focus if known key or forward it to all our elements."""
if not self.focus:
return
if key == self.prev_key:
self.prev()
elif key == self.next_key:
......
......@@ -21,8 +21,7 @@ class CursesHandler(logging.Handler):
try:
msg = self.format(record)
screen = self.screen
fs = "\n%s"
screen.addstr(fs % msg)
screen.addstr("\n%s" % msg)
screen.refresh()
except (KeyboardInterrupt, SystemExit):
raise
......
......@@ -39,20 +39,16 @@ class NumberRange(Drawable):
Drawable.__init__(self, window=window, row=row, col=col)
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = self.attr
attr = self.attr if self.focus else curses.A_NORMAL
self.window.move(self.row, self.col)
self.window.addstr(self.text + "<")
formatter = "{:>" + str(len(str(self.max))) + "}"
self.window.addstr(formatter.format(self.value), attr)
self.window.addstr(f"{self.text}<")
self.window.addstr(f"{self.value:>{len(str(self.max))}}", attr)
self.window.addstr(">")
self.window.refresh()
def send_key(self, key):
if not self.focus:
return
if key == self.prev_key:
self.decr()
elif key == self.next_key:
......@@ -63,7 +59,7 @@ class NumberRange(Drawable):
self.set(int(key))
else:
# add number on right side
self.set(int(str(self.value) + key))
self.set(int(f"{self.value}{key}"))
def send_mouse(self, mx, my):
pass
......@@ -74,8 +70,7 @@ class NumberRange(Drawable):
Args:
value: the value to set.
"""
self.value = min(value, self.max)
self.value = max(self.value, self.min)
self.value = max(min(value, self.max), self.min)
self.draw()
def incr(self):
......
......@@ -10,9 +10,10 @@ class ProgressBar(Drawable):
height: The height including frame.
value: Initial value.
max_value: Maximum value.
text: Text to show on top of the progressbar.
title: Text to show on top of the progressbar.
attr: Curses attribute of the progress indicator.
"""
characters = "▁▂▃▄▅▆▇█"
def __init__(
self,
......@@ -24,23 +25,23 @@ class ProgressBar(Drawable):
height=0,
value=0,
max_value=100,
text="",
title="",
attr=curses.A_STANDOUT
):
assert width > 2
assert height > 2
self.characters = "▁▂▃▄▅▆▇█"
self.value = value
self.max = max_value
self.text = text
self.title = title
self.attr = attr
# draw frame
self.window = window.subwin(height, width, row, col)
self.window.border(0)
if len(self.text) > width:
self.window.addstr(self.text)
# add title if possible
if len(self.title) > width:
self.window.addstr(self.title)
else:
self.window.addstr(0, (width - len(self.text)) // 2, self.text)
self.window.addstr(0, (width - len(self.title)) // 2, self.title)
# inner box
self.width = width - 2
self.height = height - 2
......@@ -74,8 +75,7 @@ class ProgressBar(Drawable):
Args:
value: the value to set.
"""
self.value = min(value, self.max)
self.value = max(self.value, 0)
self.value = max(min(value, self.max), 0)
self.draw()
def send_key(self, key):
......
......@@ -80,7 +80,7 @@ class NCHL:
value=255,
max_value=255,
attr=curses.color_pair(numbers_text[i][1]),
text=numbers_text[i][0],
title=numbers_text[i][0],
)
for i in range(len(numbers_text))
]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment