Skip to content

Commit

Permalink
added shift-arrow & more efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Jan 21, 2024
1 parent 5537ac8 commit cc31609
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 78 deletions.
100 changes: 62 additions & 38 deletions cat_win/util/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, file: str, file_encoding: str, debug_mode: bool = False) -> N
self.error_bar = ''
self.unsaved_progress = False
self.changes_made = False
self.scrolling = False

# current cursor position
self.cpos = Position(0, 0)
Expand Down Expand Up @@ -207,33 +208,29 @@ def _key_ctl_backspace(self, _) -> str:
return ''
return None

def _key_left(self, _) -> str:
def _move_key_left(self) -> None:
if self.cpos.col:
self.cpos.col -= 1
elif self.cpos.row:
self.cpos.row -= 1
self.cpos.col = len(self.window_content[self.cpos.row])
return None

def _key_right(self, _) -> str:
def _move_key_right(self) -> None:
if self.cpos.col < len(self.window_content[self.cpos.row]):
self.cpos.col += 1
elif self.cpos.row < len(self.window_content)-1:
self.cpos.row += 1
self.cpos.col = 0
return None

def _key_up(self, _) -> str:
def _move_key_up(self) -> None:
if self.cpos.row:
self.cpos.row -= 1
return None

def _key_down(self, _) -> str:
def _move_key_down(self) -> None:
if self.cpos.row < len(self.window_content)-1:
self.cpos.row += 1
return None

def _key_ctl_left(self, _) -> str:
def _move_key_ctl_left(self) -> None:
if self.cpos.col == 1:
self.cpos.col = 0
elif self.cpos.col > 1:
Expand All @@ -247,9 +244,8 @@ def _key_ctl_left(self, _) -> str:
elif self.cpos.row:
self.cpos.row -= 1
self.cpos.col = len(self.window_content[self.cpos.row])
return None

def _key_ctl_right(self, _) -> str:
def _move_key_ctl_right(self) -> None:
if self.cpos.col == len(self.window_content[self.cpos.row])-1:
self.cpos.col = len(self.window_content[self.cpos.row])
elif self.cpos.col < len(self.window_content[self.cpos.row])-1:
Expand All @@ -261,53 +257,73 @@ def _key_ctl_right(self, _) -> str:
elif self.cpos.row < len(self.window_content)-1:
self.cpos.row += 1
self.cpos.col = 0
return None

def _key_ctl_up(self, _) -> str:
def _move_key_ctl_up(self) -> None:
if self.cpos.row >= 10:
self.cpos.row -= 10
else:
self.cpos.row = 0
self.cpos.col = 0
return None

def _key_ctl_down(self, _) -> str:
def _move_key_ctl_down(self) -> None:
if self.cpos.row < len(self.window_content)-10:
self.cpos.row += 10
else:
self.cpos.row = len(self.window_content)-1
self.cpos.col = len(self.window_content[self.cpos.row])
return None

def _key_page_up(self, _) -> str:
def _scroll_key_shift_left(self) -> None:
self.scrolling = True
# _, max_x = self.getxymax()
self.wpos.col = max(self.wpos.col-1, 0)
# if self.cpos.col == self.wpos.col + max_x:
# self.cpos.col -= 1

def _scroll_key_shift_right(self) -> None:
self.scrolling = True
max_y, max_x = self.getxymax()
max_line = max(map(len,self.window_content[self.wpos.row:self.wpos.row+max_y]))
self.wpos.col = max(min(self.wpos.col+1, max_line+1-max_x), 0)
# if self.cpos.col == self.wpos.col-1:
# self.cpos.col += 1

def _scroll_key_shift_up(self) -> None:
self.scrolling = True
# max_y, _ = self.getxymax()
self.wpos.row = max(self.wpos.row-1, 0)
# if self.cpos.row == self.wpos.row + max_y:
# self.cpos.row -= 1

def _scroll_key_shift_down(self) -> None:
self.scrolling = True
max_y, _ = self.getxymax()
self.wpos.row = max(min(self.wpos.row+1, len(self.window_content)-max_y), 0)
# if self.cpos.row == self.wpos.row-1:
# self.cpos.row += 1

def _move_key_page_up(self) -> None:
max_y, _ = self.getxymax()
self.wpos.row = max(self.wpos.row-max_y, 0)
self.cpos.row = max(self.cpos.row-max_y, 0)
return None

def _key_page_down(self, _) -> str:
def _move_key_page_down(self) -> None:
max_y, _ = self.getxymax()
self.wpos.row = max(min(self.wpos.row+max_y, len(self.window_content)-max_y), 0)
self.cpos.row = min(self.cpos.row+max_y, len(self.window_content)-1)
return None

def _key_end(self, _) -> str:
def _move_key_end(self) -> None:
self.cpos.col = len(self.window_content[self.cpos.row])
return None

def _key_ctl_end(self, _) -> str:
def _move_key_ctl_end(self) -> None:
self.cpos.row = len(self.window_content)-1
self.cpos.col = len(self.window_content[-1])
return None

def _key_home(self, _) -> str:
def _move_key_home(self) -> None:
self.cpos.col = 0
return None

def _key_ctl_home(self, _) -> str:
def _move_key_ctl_home(self) -> None:
self.cpos.row = 0
self.cpos.col = 0
return None

def _key_string(self, wchars) -> str:
"""
Expand Down Expand Up @@ -482,19 +498,21 @@ def _render_scr(self) -> None:
self.cpos.col = min(self.cpos.col, rowlen)

# set/enforce the boundaries
curses.curs_set(0)
try:
self.curse_window.move(0, 0)
except curses.error:
pass
if self.cpos.row < self.wpos.row:
self.wpos.row = self.cpos.row
elif self.cpos.row >= self.wpos.row + max_y:
self.wpos.row = self.cpos.row - max_y + 1
if self.cpos.col < self.wpos.col:
self.wpos.col = self.cpos.col
elif self.cpos.col >= self.wpos.col + max_x:
self.wpos.col = self.cpos.col - max_x + 1

if not self.scrolling:
if self.cpos.row < self.wpos.row:
self.wpos.row = self.cpos.row
elif self.cpos.row >= self.wpos.row + max_y:
self.wpos.row = self.cpos.row - max_y + 1
if self.cpos.col < self.wpos.col:
self.wpos.col = self.cpos.col
elif self.cpos.col >= self.wpos.col + max_x:
self.wpos.col = self.cpos.col - max_x + 1

# display screen
for row in range(max_y):
brow = row + self.wpos.row
Expand Down Expand Up @@ -547,7 +565,11 @@ def _render_scr(self) -> None:
max(self.cpos.col-self.wpos.col, 0))
except curses.error:
pass
curses.curs_set(1)
curses.curs_set(not self.scrolling or not (
self.cpos.row < self.wpos.row or self.cpos.col < self.wpos.col
))

self.scrolling = False
self.curse_window.refresh()

def _run(self, write_func) -> None:
Expand All @@ -573,6 +595,8 @@ def _run(self, write_func) -> None:
self.history.add(key, action_text, f_len, pre_pos, self.cpos.get_pos())
elif key in ACTION_HOTKEYS:
running &= getattr(self, key.decode(), lambda *_: False)(write_func)
else:
getattr(self, key.decode(), lambda *_: None)()

def _open(self, curse_window, write_func) -> None:
"""
Expand Down
87 changes: 47 additions & 40 deletions cat_win/util/editorhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,60 @@
# ctrl-backspace
b'^?' : b'_key_ctl_backspace', # windows
# arrows
b'KEY_LEFT' : b'_key_left', # windows & xterm
b'KEY_RIGHT' : b'_key_right',
b'KEY_UP' : b'_key_up',
b'KEY_DOWN' : b'_key_down',
b'KEY_B1' : b'_key_left', # numpad
b'KEY_B3' : b'_key_right',
b'KEY_A2' : b'_key_up',
b'KEY_C2' : b'_key_down',
b'KEY_LEFT' : b'_move_key_left', # windows & xterm
b'KEY_RIGHT' : b'_move_key_right',
b'KEY_UP' : b'_move_key_up',
b'KEY_DOWN' : b'_move_key_down',
b'KEY_B1' : b'_move_key_left', # numpad
b'KEY_B3' : b'_move_key_right',
b'KEY_A2' : b'_move_key_up',
b'KEY_C2' : b'_move_key_down',
# ctrl-arrows
b'CTL_LEFT' : b'_key_ctl_left', # windows
b'CTL_RIGHT' : b'_key_ctl_right',
b'CTL_UP' : b'_key_ctl_up',
b'CTL_DOWN' : b'_key_ctl_down',
b'kLFT5' : b'_key_ctl_left', # xterm
b'kRIT5' : b'_key_ctl_right',
b'kUP5' : b'_key_ctl_up',
b'kDN5' : b'_key_ctl_down',
b'CTL_PAD4' : b'_key_ctl_left', # numpad
b'CTL_PAD6' : b'_key_ctl_right',
b'CTL_PAD8' : b'_key_ctl_up',
b'CTL_PAD2' : b'_key_ctl_down',
b'CTL_LEFT' : b'_move_key_ctl_left', # windows
b'CTL_RIGHT' : b'_move_key_ctl_right',
b'CTL_UP' : b'_move_key_ctl_up',
b'CTL_DOWN' : b'_move_key_ctl_down',
b'kLFT5' : b'_move_key_ctl_left', # xterm
b'kRIT5' : b'_move_key_ctl_right',
b'kUP5' : b'_move_key_ctl_up',
b'kDN5' : b'_move_key_ctl_down',
b'CTL_PAD4' : b'_move_key_ctl_left', # numpad
b'CTL_PAD6' : b'_move_key_ctl_right',
b'CTL_PAD8' : b'_move_key_ctl_up',
b'CTL_PAD2' : b'_move_key_ctl_down',
# shift-arrows
b'KEY_SLEFT' : b'_scroll_key_shift_left', # windows
b'KEY_SRIGHT' : b'_scroll_key_shift_right',
b'KEY_SUP' : b'_scroll_key_shift_up',
b'KEY_SDOWN' : b'_scroll_key_shift_down',
b'KEY_SR' : b'_scroll_key_shift_up', # xterm
b'KEY_SF' : b'_scroll_key_shift_down',
# page
b'KEY_PPAGE' : b'_key_page_up', # windows & xterm
b'KEY_NPAGE' : b'_key_page_down',
b'KEY_A3' : b'_key_page_up', # numpad
b'KEY_C3' : b'_key_page_down',
b'KEY_PPAGE' : b'_move_key_page_up', # windows & xterm
b'KEY_NPAGE' : b'_move_key_page_down',
b'KEY_A3' : b'_move_key_page_up', # numpad
b'KEY_C3' : b'_move_key_page_down',
# ctrl - page
b'CTL_PGUP' : b'_key_page_up', # windows
b'CTL_PGDN' : b'_key_page_down',
b'kPRV5' : b'_key_page_up', # xterm
b'kNXT5' : b'_key_page_down',
b'CTL_PAD9' : b'_key_page_up', # numpad
b'CTL_PAD3' : b'_key_page_down',
b'CTL_PGUP' : b'_move_key_page_up', # windows
b'CTL_PGDN' : b'_move_key_page_down',
b'kPRV5' : b'_move_key_page_up', # xterm
b'kNXT5' : b'_move_key_page_down',
b'CTL_PAD9' : b'_move_key_page_up', # numpad
b'CTL_PAD3' : b'_move_key_page_down',
# end
b'KEY_END' : b'_key_end', # windows & xterm
b'KEY_C1' : b'_key_end', # numpad
b'KEY_END' : b'_move_key_end', # windows & xterm
b'KEY_C1' : b'_move_key_end', # numpad
# ctrl - end
b'CTL_END' : b'_key_ctl_end', # windows
b'kEND5' : b'_key_ctl_end', # xterm
b'CTL_PAD1' : b'_key_ctl_end', # numpad
b'CTL_END' : b'_move_key_ctl_end', # windows
b'kEND5' : b'_move_key_ctl_end', # xterm
b'CTL_PAD1' : b'_move_key_ctl_end', # numpad
# pos/home
b'KEY_HOME' : b'_key_home', # windows & xterm
b'KEY_A1' : b'_key_home', # numpad
b'KEY_HOME' : b'_move_key_home', # windows & xterm
b'KEY_A1' : b'_move_key_home', # numpad
# ctrl - pos/home
b'CTL_HOME' : b'_key_ctl_home', # windows
b'kHOM5' : b'_key_ctl_home', # xterm
b'CTL_PAD7' : b'_key_ctl_home', # numpad
b'CTL_HOME' : b'_move_key_ctl_home', # windows
b'kHOM5' : b'_move_key_ctl_home', # xterm
b'CTL_PAD7' : b'_move_key_ctl_home', # numpad
# default alnum key
b'_key_string' : b'_key_string',
# history
Expand Down

0 comments on commit cc31609

Please sign in to comment.