Commit 76b348f3 authored by Fjen Undso's avatar Fjen Undso
Browse files

c.py/NumberRange: add text and color support

parent 600e5593
......@@ -34,13 +34,15 @@ class CursesHandler(logging.Handler):
class NumberRange:
def __init__(self, window, *, row=0, col=0, number=0, min_num=0, max_num=255, prev_key=None, next_key=None):
def __init__(self, window, *, row=0, col=0, value=0, text='', attr=curses.A_STANDOUT, min_value=0, max_value=255, prev_key=None, next_key=None):
self.window = window
self.row = row
self.col = col
self.number = number
self.min = min_num
self.max = max_num
self.value = value
self.text = text
self.attr = attr
self.min = min_value
self.max = max_value
self.focus = False
self.prev_key = prev_key
self.next_key = next_key
......@@ -49,11 +51,11 @@ class NumberRange:
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = curses.A_STANDOUT
attr = self.attr
self.window.move(self.row, self.col)
self.window.addstr("<")
self.window.addstr(self.text + "<")
formatter = "{:>" + str(len(str(self.max))) + "}"
self.window.addstr(formatter.format(self.number), attr)
self.window.addstr(formatter.format(self.value), attr)
self.window.addstr(">")
self.window.refresh()
......@@ -64,27 +66,23 @@ class NumberRange:
elif key == self.next_key:
self.incr()
elif key.isdigit():
if len(str(self.number)) == len(str(self.max)):
if len(str(self.value)) == len(str(self.max)):
# reset number if field is full
self.set(int(key))
else:
# add number on right side
self.set(int(str(self.number) + key))
self.set(int(str(self.value) + key))
def set(self, number):
if number > self.max:
self.number = self.max
elif number < self.min:
self.number = self.min
else:
self.number = number
def set(self, value):
self.value = min(value, self.max)
self.value = max(value, self.min)
self.draw()
def incr(self):
self.set(self.number + 1)
self.set(self.value + 1)
def decr(self):
self.set(self.number - 1)
self.set(self.value - 1)
def toggle_focus(self):
self.focus = not self.focus
......
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