From f7fe880d9dcf553e8c7c6720ce81f7df50b1cf1f Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Fri, 29 Nov 2024 17:40:48 +0100 Subject: [PATCH] added F2 hotkey to replace next --- cat_win/src/service/editor.py | 45 +++++++++++++--------- cat_win/src/service/helper/editorhelper.py | 3 +- cat_win/src/service/hexeditor.py | 18 ++++----- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/cat_win/src/service/editor.py b/cat_win/src/service/editor.py index 41b9816..fbaa5e4 100644 --- a/cat_win/src/service/editor.py +++ b/cat_win/src/service/editor.py @@ -847,13 +847,13 @@ def _action_find(self, find_next: bool = False) -> bool: wchar, sub_s, tmp_error = '', '', '' key = b'_key_enter' while str(wchar) != ESC_CODE: - pre_s = '' - if self.search and isinstance(self.search, str): - pre_s = f" [{repr(self.search)[1:-1]}]" - elif self.search: - pre_s = f" re:[{repr(self.search.pattern)[1:-1]}]" - rep_r = 'Match' if search_regex else 'Search for' if not find_next: + pre_s = '' + if self.search and isinstance(self.search, str): + pre_s = f" [{repr(self.search)[1:-1]}]" + elif self.search: + pre_s = f" re:[{repr(self.search.pattern)[1:-1]}]" + rep_r = 'Match' if search_regex else 'Search for' self._action_render_scr(f"Confirm: 'ENTER' - {rep_r}{pre_s}: {sub_s}␣", tmp_error) wchar, key = next(self.get_char) if key in ACTION_HOTKEYS: @@ -927,7 +927,7 @@ def _action_find(self, find_next: bool = False) -> bool: tmp_error+= ' within the selection!' if self.selecting else '!' return True - def _action_replace(self) -> bool: + def _action_replace(self, replace_next: bool = False) -> bool: """ handles the replace in editor action. @@ -939,18 +939,20 @@ def _action_replace(self) -> bool: replace_all = False wchar, sub_s, tmp_error = '', '', '' + key = b'_key_enter' while str(wchar) != ESC_CODE: - pre_s = '[]' - if self.search and isinstance(self.search, str): - pre_s = f"[{repr(self.search)[1:-1]}]" - elif self.search: - pre_s = f"re:[{repr(self.search.pattern)[1:-1]}]" - pre_r = f" [{repr(self.replace)[1:-1]}]" if self.replace else '' - rep_a = 'ALL ' if replace_all else '' - self._action_render_scr( - f"Confirm: 'ENTER' - Replace {rep_a}{pre_s} with{pre_r}: {sub_s}␣", tmp_error - ) - wchar, key = next(self.get_char) + if not replace_next: + pre_s = '[]' + if self.search and isinstance(self.search, str): + pre_s = f"[{repr(self.search)[1:-1]}]" + elif self.search: + pre_s = f"re:[{repr(self.search.pattern)[1:-1]}]" + pre_r = f" [{repr(self.replace)[1:-1]}]" if self.replace else '' + rep_a = 'ALL ' if replace_all else '' + self._action_render_scr( + f"Confirm: 'ENTER' - Replace {rep_a}{pre_s} with{pre_r}: {sub_s}␣", tmp_error + ) + wchar, key = next(self.get_char) if key in ACTION_HOTKEYS: if key in [b'_action_quit', b'_action_interrupt']: break @@ -1230,11 +1232,16 @@ def _function_help(self) -> None: self.curse_window.refresh() next(self.get_char) - def _function_next(self) -> None: + def _function_search(self) -> None: if not self.search: return self._action_find(True) + def _function_replace(self) -> None: + if not self.search: + return + self._action_replace(True) + def _get_new_char(self): """ get next char diff --git a/cat_win/src/service/helper/editorhelper.py b/cat_win/src/service/helper/editorhelper.py index 0cef223..24520a2 100644 --- a/cat_win/src/service/helper/editorhelper.py +++ b/cat_win/src/service/helper/editorhelper.py @@ -179,7 +179,8 @@ def initscr(): b'KEY_RESIZE' : b'_action_resize', b'KEY_F(1)' : b'_function_help', - b'KEY_F(3)' : b'_function_next', + b'KEY_F(2)' : b'_function_replace', + b'KEY_F(3)' : b'_function_search', } # translates key-inputs to pre-defined actions/methods KEY_HOTKEYS = set(v for v in UNIFY_HOTKEYS.values() if v.startswith(b'_key' )) diff --git a/cat_win/src/service/hexeditor.py b/cat_win/src/service/hexeditor.py index 260c6cd..d1ccb13 100644 --- a/cat_win/src/service/hexeditor.py +++ b/cat_win/src/service/hexeditor.py @@ -523,15 +523,15 @@ def _action_find(self, find_next: bool = False) -> bool: wchar, sub_s, tmp_error= '', '', '' key = b'_key_enter' while str(wchar) != ESC_CODE: - pre_s = '' - if self.search: - pre_s = f" [{bm_ind}{repr(self.search)[1:-1]}]" - if not search_byte_mode: - try: - pre_s = f" [{bm_ind}{repr(bytes.fromhex(self.search))[2:-1]}]" - except ValueError: - pass if not find_next: + pre_s = '' + if self.search: + pre_s = f" [{bm_ind}{repr(self.search)[1:-1]}]" + if not search_byte_mode: + try: + pre_s = f" [{bm_ind}{repr(bytes.fromhex(self.search))[2:-1]}]" + except ValueError: + pass self._action_render_scr(f"Confirm: 'ENTER' - Search for{pre_s}: {bm_ind}{sub_s}␣", tmp_error) wchar, key = self._get_next_char() @@ -801,7 +801,7 @@ def _function_help(self) -> None: self.curse_window.refresh() self._get_next_char() - def _function_next(self) -> None: + def _function_search(self) -> None: if not self.search: return self._action_find(True)