Skip to content

Commit

Permalink
added F2 hotkey to replace next
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Nov 29, 2024
1 parent b660977 commit f7fe880
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
45 changes: 26 additions & 19 deletions cat_win/src/service/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cat_win/src/service/helper/editorhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' ))
Expand Down
18 changes: 9 additions & 9 deletions cat_win/src/service/hexeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f7fe880

Please sign in to comment.