Commit 417f6eb5 authored by Fjen Undso's avatar Fjen Undso
Browse files

initial commit. only curses test

parents
#!/usr/bin/env python3
import logging
from curses import wrapper
import curses
import time
class CursesHandler(logging.Handler):
def __init__(self, screen):
logging.Handler.__init__(self)
maxy, maxx = screen.getmaxyx()
win_log_border = screen.subwin(5, maxx, maxy - 5, 0)
win_log_border.border(0)
win_log = win_log_border.subwin(3, maxx - 2, maxy - 4, 1)
win_log.scrollok(True)
win_log.idlok(True)
win_log.leaveok(True)
self.screen = win_log
formatter = logging.Formatter(
'%(name) -10s | %(levelname) -5s | %(message)s')
self.setFormatter(formatter)
def emit(self, record):
try:
msg = self.format(record)
screen = self.screen
fs = "\n%s"
screen.addstr(fs % msg)
screen.refresh()
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
self.handleError(record)
class NumberRange:
def __init__(self, window, *, row=0, col=0, number=0, min_num=0, max_num=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.focus = False
self.prev_key = prev_key
self.next_key = next_key
self.draw()
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = curses.A_STANDOUT
self.window.move(self.row, self.col)
self.window.addstr("<")
formatter = "{:>" + str(len(str(self.max))) + "}"
self.window.addstr(formatter.format(self.number), attr)
self.window.addstr(">")
self.window.refresh()
def send_key(self, key):
if self.focus:
if key == self.prev_key:
self.decr()
elif key == self.next_key:
self.incr()
elif key.isdigit():
if len(str(self.number)) == 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))
def set(self, number):
if number > self.max:
self.number = self.max
elif number < self.min:
self.number = self.min
else:
self.number = number
self.draw()
def incr(self):
self.set(self.number + 1)
def decr(self):
self.set(self.number - 1)
def toggle_focus(self):
self.focus = not self.focus
self.draw()
class CheckBox:
def __init__(self, window, *, row=0, col=0, text=None, toggle_key=None):
self.window = window
self.row = row
self.col = col
self.text = text
self.toggle_key = toggle_key
self.focus = False
self.active = False
self.draw()
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = curses.A_STANDOUT
self.window.move(self.row, self.col)
if self.active:
self.window.addstr("(X) " + self.text, attr)
else:
self.window.addstr("( ) " + self.text, attr)
self.window.refresh()
def send_key(self, key):
if self.focus and key == self.toggle_key:
self.toggle()
def toggle(self):
self.active = not self.active
self.draw()
def toggle_focus(self):
self.focus = not self.focus
self.draw()
class FocusSwitcher:
def __init__(self, elements=[], prev_key=None, next_key=None):
self.elements = elements
self.prev_key = prev_key
self.next_key = next_key
self.focus = False
def toggle_focus(self):
if self.focus:
for e in self.elements:
if e.focus:
e.toggle_focus()
else:
self.elements[0].toggle_focus()
self.focus = not self.focus
def add(self, element):
self.elements.append(element)
def send_key(self, key):
if self.focus:
if key == self.prev_key:
self.prev()
elif key == self.next_key:
self.next()
else:
for e in self.elements:
e.send_key(key)
def next(self):
for i in range(len(self.elements)):
if self.elements[i].focus:
self.elements[i].toggle_focus()
if i == len(self.elements) - 1:
self.elements[0].toggle_focus()
else:
self.elements[i + 1].toggle_focus()
break
def prev(self):
for i in range(len(self.elements)):
if self.elements[i].focus:
self.elements[i].toggle_focus()
self.elements[i - 1].toggle_focus()
break
def progress(window):
# TODO: make it good
win = window.subwin(10, 3, 0, 0)
win.border(0)
win.addstr(1, 1, "#")
win.refresh()
def main(stdscr):
stdscr.clear()
# hide cursor
curses.curs_set(0)
# logging
logger = logging.getLogger(__name__)
logger.addHandler(CursesHandler(stdscr))
logger.setLevel(logging.INFO)
numbers = [
NumberRange(
stdscr,
row=i,
number=i,
prev_key='KEY_LEFT',
next_key='KEY_RIGHT') for i in range(5)]
checkboxes = [
CheckBox(
stdscr,
row=i,
toggle_key=' ',
text='Toggle' +
str(i)) for i in range(
5,
10)]
fs_n = FocusSwitcher(numbers, prev_key='KEY_UP', next_key='KEY_DOWN')
fs_cb = FocusSwitcher(checkboxes, prev_key='KEY_UP', next_key='KEY_DOWN')
fs_main = FocusSwitcher([fs_n, fs_cb], prev_key='\t')
fs_main.toggle_focus()
stdscr.refresh()
while True:
k = stdscr.getkey()
logger.info(k)
fs_main.send_key(k)
if __name__ == '__main__':
wrapper(main)
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