From de57f712d605515211dfb02aa17fd94ee1384e2c Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Mon, 29 Jan 2024 15:39:25 +0100 Subject: [PATCH] added jump to line functionality --- cat_win/util/editor.py | 36 ++++++++++++++++++++++++++++++++++++ cat_win/util/editorhelper.py | 1 + 2 files changed, 37 insertions(+) diff --git a/cat_win/util/editor.py b/cat_win/util/editor.py index fb53277e..fe264bbe 100644 --- a/cat_win/util/editor.py +++ b/cat_win/util/editor.py @@ -413,6 +413,42 @@ def _action_save(self, write_func) -> bool: print(self.error_bar, file=sys.stderr) return True + def _action_jump(self, _) -> bool: + def _render_scr(l_jmp: str) -> None: + max_y, max_x = self.getxymax() + try: + if self.error_bar: + self.curse_window.addstr(max_y + self.status_bar_size - 2, 0, + self.error_bar[:max_x].ljust(max_x), + self._get_color(2)) + jump_message = f"Confirm: [y]es, [n]o - Jump to line: {l_jmp}_"[:max_x].ljust(max_x) + self.curse_window.addstr(max_y + self.status_bar_size - 1, 0, jump_message, + self._get_color(5)) + except curses.error: + pass + self.curse_window.refresh() + curses.curs_set(0) + + wchar, l_jmp = '', '' + while str(wchar).upper() not in ['\x1b', 'N']: + _render_scr(l_jmp) + wchar, key = self._get_new_char() + if key in ACTION_HOTKEYS: + if key in [b'_action_quit', b'_action_interrupt']: + break + if key == b'_action_resize': + getattr(self, key.decode(), lambda *_: False)(None) + self._render_scr() + curses.curs_set(0) + if key == b'_key_string' and wchar.isdigit(): + l_jmp += wchar + elif (key == b'_key_string' and wchar.upper() in ['Y', 'J']) or \ + key == b'_key_enter': + if l_jmp: + self.cpos.row = min(int(l_jmp), len(self.window_content)-1) + break + return True + def _action_quit(self, write_func) -> bool: """ handles the quit editor action. diff --git a/cat_win/util/editorhelper.py b/cat_win/util/editorhelper.py index 833db201..a24e8f6c 100644 --- a/cat_win/util/editorhelper.py +++ b/cat_win/util/editorhelper.py @@ -141,6 +141,7 @@ b'^Y' : b'_key_redo', # actions b'^S' : b'_action_save', + b'^E' : b'_action_jump', b'^Q' : b'_action_quit', b'^C' : b'_action_interrupt', b'KEY_RESIZE' : b'_action_resize',