reduce number of lines
This commit is contained in:
parent
d4ed46a736
commit
25dd5420d1
54
kls
54
kls
|
@ -33,18 +33,12 @@ class CircularList:
|
||||||
self.index = 0
|
self.index = 0
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
if isinstance(index, slice):
|
|
||||||
start, stop, step = index.indices(self.size)
|
start, stop, step = index.indices(self.size)
|
||||||
return [self.elements[(self.index + i) % self.size] for i in range(start, stop, step)]
|
return [self.elements[(self.index + i) % self.size] for i in range(start, stop, step)]
|
||||||
else:
|
|
||||||
raise TypeError("Invalid index type")
|
|
||||||
|
|
||||||
def forward(self, steps):
|
def shift(self, steps):
|
||||||
self.index = (self.index + steps) % self.size
|
self.index = (self.index + steps) % self.size
|
||||||
|
|
||||||
def backward(self, steps):
|
|
||||||
self.index = (self.index - steps) % self.size
|
|
||||||
|
|
||||||
|
|
||||||
class Menu:
|
class Menu:
|
||||||
def __init__(self, title: str, rows: list, begin_x: int, width: int, rows_height: int):
|
def __init__(self, title: str, rows: list, begin_x: int, width: int, rows_height: int):
|
||||||
|
@ -116,8 +110,6 @@ def handle_filter_state(key: str, menu: Menu):
|
||||||
menu.filter = menu.filter[:-1] # Backspace key deletes a character (\x08 is also Backspace)
|
menu.filter = menu.filter[:-1] # Backspace key deletes a character (\x08 is also Backspace)
|
||||||
elif key.isalpha() or key == "-":
|
elif key.isalpha() or key == "-":
|
||||||
menu.filter += key.lower()
|
menu.filter += key.lower()
|
||||||
else:
|
|
||||||
return
|
|
||||||
menu.visible_row_index = 0
|
menu.visible_row_index = 0
|
||||||
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
|
||||||
draw_menu(menu)
|
draw_menu(menu)
|
||||||
|
@ -125,7 +117,11 @@ def handle_filter_state(key: str, menu: Menu):
|
||||||
menus[2].visible_row_index = 0 # reset the visible row index of third menu before redrawing
|
menus[2].visible_row_index = 0 # reset the visible row index of third menu before redrawing
|
||||||
|
|
||||||
|
|
||||||
def handle_mouse(mouse_info: tuple, menu: Menu):
|
def handle_mouse(menu: Menu):
|
||||||
|
try:
|
||||||
|
mouse_info = curses.getmouse()
|
||||||
|
except curses.error: # this fixes scrolling error
|
||||||
|
return
|
||||||
row_number = mouse_info[2] - HEADER_HEIGHT
|
row_number = mouse_info[2] - HEADER_HEIGHT
|
||||||
column_number = mouse_info[1]
|
column_number = mouse_info[1]
|
||||||
next_menu = None
|
next_menu = None
|
||||||
|
@ -155,24 +151,18 @@ def handle_mouse(mouse_info: tuple, menu: Menu):
|
||||||
|
|
||||||
|
|
||||||
def handle_vertical_navigation(key: str, menu: Menu):
|
def handle_vertical_navigation(key: str, menu: Menu):
|
||||||
if key == "KEY_DOWN":
|
if len(menu.visible_rows()) <= 1:
|
||||||
if (menu.visible_row_index + 1) == menu.rows_height and menu.filtered_rows.size > menu.rows_height:
|
return
|
||||||
menu.filtered_rows.forward(1)
|
shifts = {"KEY_DOWN": 1, "KEY_UP": -1, "KEY_NPAGE": 1, "KEY_PPAGE": -1, 'KEY_HOME': 0, 'KEY_END': -1}
|
||||||
|
if key in ["KEY_DOWN", "KEY_UP"]:
|
||||||
|
if menu.filtered_rows.size > menu.rows_height:
|
||||||
|
menu.filtered_rows.shift(shifts[key])
|
||||||
else:
|
else:
|
||||||
menu.visible_row_index = (menu.visible_row_index + 1) % menu.filtered_rows.size # index of the selected visible row
|
menu.visible_row_index = (menu.visible_row_index + shifts[key]) % menu.filtered_rows.size # index of the selected visible row
|
||||||
elif key == "KEY_UP":
|
elif key in ["KEY_NPAGE", "KEY_PPAGE"]:
|
||||||
if menu.visible_row_index == 0 and menu.filtered_rows.size > menu.rows_height:
|
menu.filtered_rows.shift(shifts[key] * len(menu.visible_rows()))
|
||||||
menu.filtered_rows.backward(1)
|
elif key in ['KEY_HOME','KEY_END']:
|
||||||
else:
|
menu.visible_row_index = shifts[key]
|
||||||
menu.visible_row_index = (menu.visible_row_index - 1) % menu.filtered_rows.size # index of the selected visible row
|
|
||||||
elif key == 'KEY_NPAGE':
|
|
||||||
menu.filtered_rows.forward(len(menu.visible_rows()))
|
|
||||||
elif key == 'KEY_PPAGE':
|
|
||||||
menu.filtered_rows.backward(len(menu.visible_rows()))
|
|
||||||
elif key == 'KEY_HOME':
|
|
||||||
menu.visible_row_index = 0
|
|
||||||
elif key == 'KEY_END':
|
|
||||||
menu.visible_row_index = len(menu.visible_rows()) - 1
|
|
||||||
draw_rows(menu)
|
draw_rows(menu)
|
||||||
if menu != menus[2]:
|
if menu != menus[2]:
|
||||||
menus[2].visible_row_index = 0
|
menus[2].visible_row_index = 0
|
||||||
|
@ -192,19 +182,15 @@ def catch_input(menu: Menu):
|
||||||
draw_row(menu.win, menu.title, 1, 2, selected=False) # remove selection from the current menu title
|
draw_row(menu.win, menu.title, 1, 2, selected=False) # remove selection from the current menu title
|
||||||
draw_row(next_menu.win, next_menu.title, 1, 2, selected=True) # and select the new menu title
|
draw_row(next_menu.win, next_menu.title, 1, 2, selected=True) # and select the new menu title
|
||||||
globals().update(selected_menu=next_menu)
|
globals().update(selected_menu=next_menu)
|
||||||
elif key in ["KEY_UP", "KEY_DOWN", "KEY_NPAGE", "KEY_PPAGE", "KEY_HOME", "KEY_END"] and len(menu.visible_rows()) > 1:
|
elif key in ["KEY_UP", "KEY_DOWN", "KEY_NPAGE", "KEY_PPAGE", "KEY_HOME", "KEY_END"]:
|
||||||
handle_vertical_navigation(key, menu)
|
handle_vertical_navigation(key, menu)
|
||||||
elif key == "KEY_MOUSE" and MOUSE_ENABLED:
|
elif key == "KEY_MOUSE" and MOUSE_ENABLED:
|
||||||
try:
|
handle_mouse(menu)
|
||||||
mouse_info = curses.getmouse()
|
|
||||||
handle_mouse(mouse_info, menu)
|
|
||||||
except curses.error: # this fixes scrolling error
|
|
||||||
pass
|
|
||||||
elif key in KEY_BINDINGS.keys() and menus[2].selected_row():
|
elif key in KEY_BINDINGS.keys() and menus[2].selected_row():
|
||||||
run_command(key, api_resource(), resource())
|
run_command(key, api_resource(), resource())
|
||||||
elif key == "\x1b" and not menu.filter:
|
elif key == "\x1b" and not menu.filter:
|
||||||
globals().update(selected_menu=None) # exit
|
globals().update(selected_menu=None) # exit
|
||||||
else:
|
elif (key in ["\x1b", "KEY_BACKSPACE", "\x08"] and menu.filter) or key.isalpha() or key == "-":
|
||||||
handle_filter_state(key, menu)
|
handle_filter_state(key, menu)
|
||||||
|
|
||||||
|
|
||||||
|
|
4
test.py
4
test.py
|
@ -12,11 +12,11 @@ class TestCircularList(unittest.TestCase):
|
||||||
self.circular_list = CircularList(['a', 'b', 'c'])
|
self.circular_list = CircularList(['a', 'b', 'c'])
|
||||||
|
|
||||||
def test_forward(self):
|
def test_forward(self):
|
||||||
self.circular_list.forward(1)
|
self.circular_list.shift(1)
|
||||||
self.assertEqual(self.circular_list.index, 1)
|
self.assertEqual(self.circular_list.index, 1)
|
||||||
|
|
||||||
def test_backward(self):
|
def test_backward(self):
|
||||||
self.circular_list.backward(1)
|
self.circular_list.shift(-1)
|
||||||
self.assertEqual(self.circular_list.index, 2) # Since it's circular, it goes to the end
|
self.assertEqual(self.circular_list.index, 2) # Since it's circular, it goes to the end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue