From 4ce279ab0d1ef39885b5e1571582e9e997483725 Mon Sep 17 00:00:00 2001 From: Russell Schmidt Date: Sun, 26 Jan 2025 14:44:51 -0600 Subject: [PATCH] Allow enum settings entry to scroll --- input_handlers.py | 48 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/input_handlers.py b/input_handlers.py index 4d2f541..9ccc125 100644 --- a/input_handlers.py +++ b/input_handlers.py @@ -126,6 +126,22 @@ def get_repeated_input(current_value): except ValueError: pass # Ignore invalid character inputs +def move_highlight(old_idx, new_idx, options, enum_win, enum_pad): + if old_idx == new_idx: + return # no-op + + enum_pad.chgat(old_idx, 0, enum_pad.getmaxyx()[1], get_color("settings_default")) + enum_pad.chgat(new_idx, 0, enum_pad.getmaxyx()[1], get_color("settings_default", reverse = True)) + + enum_win.refresh() + + start_index = max(0, new_idx - (enum_win.getmaxyx()[0] - 4)) + + enum_win.refresh() + enum_pad.refresh(start_index, 0, + enum_win.getbegyx()[0] + 2, enum_win.getbegyx()[1] + 4, + enum_win.getbegyx()[0] + enum_win.getmaxyx()[0] - 2, enum_win.getbegyx()[1] + 4 + enum_win.getmaxyx()[1] - 4) + def get_enum_input(options, current_value): selected_index = options.index(current_value) if current_value in options else 0 @@ -139,24 +155,34 @@ def get_enum_input(options, current_value): enum_win.attrset(get_color("window_frame")) enum_win.keypad(True) + enum_pad = curses.newpad(len(options) + 1, width - 8) + + enum_win.clear() + enum_win.border() + enum_win.addstr(1, 2, "Select an option:", get_color("settings_default", bold=True)) + + for idx, option in enumerate(options): + if idx == selected_index: + enum_pad.addstr(idx, 0, option.ljust(width - 8), get_color("settings_default", reverse=True)) + else: + enum_pad.addstr(idx, 0, option.ljust(width - 8), get_color("settings_default")) + + enum_win.refresh() + enum_pad.refresh(0, 0, + enum_win.getbegyx()[0] + 2, enum_win.getbegyx()[1] + 4, + enum_win.getbegyx()[0] + enum_win.getmaxyx()[0] - 2, enum_win.getbegyx()[1] + enum_win.getmaxyx()[1] - 4) + while True: - enum_win.clear() - enum_win.border() - enum_win.addstr(1, 2, "Select an option:", get_color("settings_default", bold=True)) - - for idx, option in enumerate(options): - if idx == selected_index: - enum_win.addstr(idx + 2, 4, option, get_color("settings_default", reverse=True)) - else: - enum_win.addstr(idx + 2, 4, option, get_color("settings_default")) - - enum_win.refresh() key = enum_win.getch() if key == curses.KEY_UP: + old_selected_index = selected_index selected_index = max(0, selected_index - 1) + move_highlight(old_selected_index, selected_index, options, enum_win, enum_pad) elif key == curses.KEY_DOWN: + old_selected_index = selected_index selected_index = min(len(options) - 1, selected_index + 1) + move_highlight(old_selected_index, selected_index, options, enum_win, enum_pad) elif key == ord('\n'): # Enter key return options[selected_index] elif key == 27 or key == curses.KEY_LEFT: # ESC or Left Arrow