From f059dd36ec2ec9fb2d3c036b97fe5c54949b6ebd Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Sat, 9 Sep 2023 12:47:47 +0200 Subject: [PATCH] optimized editor to use strings instead of lists --- cat_win/util/editor.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/cat_win/util/editor.py b/cat_win/util/editor.py index c989be43..edc0cffe 100644 --- a/cat_win/util/editor.py +++ b/cat_win/util/editor.py @@ -76,9 +76,9 @@ def _setup_file(self) -> None: self.line_sep = get_newline(self.file) with open(self.file, 'r', encoding=self.file_encoding) as _f: for line in _f.read().split('\n'): - self.window_content.append([*line]) + self.window_content.append(line) except (OSError, UnicodeDecodeError) as exc: - self.window_content.append([]) + self.window_content.append('') self.status_bar_size = 2 self.error_bar = str(exc) self.unsaved_progress = True @@ -100,14 +100,16 @@ def _key_enter(self, _) -> str: self.window_content[self.cpos.row] = self.window_content[self.cpos.row][:self.cpos.col] self.cpos.row += 1 self.cpos.col = 0 - self.window_content.insert(self.cpos.row, [] + new_line) + self.window_content.insert(self.cpos.row, new_line) self.unsaved_progress = True return '' def _key_dc(self, _) -> str: if self.cpos.col < len(self.window_content[self.cpos.row]): deleted = self.window_content[self.cpos.row][self.cpos.col] - del self.window_content[self.cpos.row][self.cpos.col] + self.window_content[self.cpos.row] = \ + self.window_content[self.cpos.row][:self.cpos.col] + \ + self.window_content[self.cpos.row][self.cpos.col+1:] self.unsaved_progress = True return deleted if self.cpos.row < len(self.window_content)-1: @@ -120,7 +122,7 @@ def _key_dc(self, _) -> str: def _key_dl(self, _) -> str: if self.cpos.col == len(self.window_content[self.cpos.row])-1: deleted = self.window_content[self.cpos.row][-1] - self.window_content[self.cpos.row] = self.window_content[self.cpos.row][:self.cpos.col] + self.window_content[self.cpos.row] = self.window_content[self.cpos.row][:-1] self.unsaved_progress = True return deleted if self.cpos.col < len(self.window_content[self.cpos.row])-1: @@ -129,7 +131,7 @@ def _key_dl(self, _) -> str: while cur_col < len(self.window_content[self.cpos.row]) and \ t_p == self.window_content[self.cpos.row][cur_col].isalnum(): cur_col += 1 - deleted = ''.join(self.window_content[self.cpos.row][self.cpos.col:cur_col]) + deleted = self.window_content[self.cpos.row][self.cpos.col:cur_col] self.window_content[self.cpos.row] = ( self.window_content[self.cpos.row][:self.cpos.col] + \ self.window_content[self.cpos.row][cur_col:]) @@ -146,7 +148,9 @@ def _key_backspace(self, _) -> str: if self.cpos.col: # delete char self.cpos.col -= 1 deleted = self.window_content[self.cpos.row][self.cpos.col] - del self.window_content[self.cpos.row][self.cpos.col] + self.window_content[self.cpos.row] = \ + self.window_content[self.cpos.row][:self.cpos.col] + \ + self.window_content[self.cpos.row][self.cpos.col+1:] self.unsaved_progress = True return deleted if self.cpos.row: # or delete line @@ -163,7 +167,7 @@ def _key_ctl_backspace(self, _) -> str: if self.cpos.col == 1: # delete char self.cpos.col = 0 deleted = self.window_content[self.cpos.row][self.cpos.col] - del self.window_content[self.cpos.row][self.cpos.col] + self.window_content[self.cpos.row] = self.window_content[self.cpos.row][1:] self.unsaved_progress = True return deleted if self.cpos.col > 1: @@ -175,8 +179,10 @@ def _key_ctl_backspace(self, _) -> str: self.cpos.col -= 1 if self.cpos.col: self.cpos.col += 1 - deleted = ''.join(self.window_content[self.cpos.row][self.cpos.col:old_col]) - del self.window_content[self.cpos.row][self.cpos.col:old_col] + deleted = self.window_content[self.cpos.row][self.cpos.col:old_col] + self.window_content[self.cpos.row] = \ + self.window_content[self.cpos.row][:self.cpos.col] + \ + self.window_content[self.cpos.row][old_col:] self.unsaved_progress = True return deleted if self.cpos.row: # or delete line @@ -297,7 +303,9 @@ def _key_string(self, wchars) -> str: if not (isinstance(wchars, str) and wchars.isprintable() or wchars == '\t'): return '' self.unsaved_progress = True - self.window_content[self.cpos.row][self.cpos.col:self.cpos.col] = list(wchars) + self.window_content[self.cpos.row] = \ + self.window_content[self.cpos.row][:self.cpos.col] + wchars + \ + self.window_content[self.cpos.row][self.cpos.col:] self.cpos.col += len(wchars) return wchars @@ -321,7 +329,7 @@ def _action_save(self, write_func) -> bool: (bool): indicates if the editor should keep running """ - content = self.line_sep.join([''.join(line) for line in self.window_content]) + content = self.line_sep.join(self.window_content) try: write_func(content, self.file, self.file_encoding) self.changes_made = True