From 72dda17eed9fcf8ed8f9c6d0e1094166458e89e7 Mon Sep 17 00:00:00 2001 From: serr Date: Tue, 4 Feb 2025 13:49:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20backspace,=20e?= =?UTF-8?q?nter,=20savefile=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers.py | 37 +++++++++---------------------------- models.py | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/controllers.py b/controllers.py index 84d49db..296a1ee 100644 --- a/controllers.py +++ b/controllers.py @@ -10,7 +10,9 @@ class EditController: def handle_input(self, symbolCode): """Обработка ввода пользователя""" - if symbolCode == 27: # EXIT + + # escape + if symbolCode == 27: return False elif symbolCode == curses.KEY_LEFT: @@ -25,35 +27,14 @@ class EditController: elif symbolCode == curses.KEY_DOWN: self.model.MoveDown() - # BACKSPACE - elif symbolCode in (127, 8): # Backspace - if self.model.currentCol > 0: # Если символ существует в текущей строке - self.model.currentCol -= 1 - del self.model.displayBuffer[self.model.currentLine][self.model.currentCol] # Удаляем символ - elif self.model.currentLine > 0: # Если текущая строка не первая - # Объединяем текущую строку с предыдущей - prev_line_length = len(self.model.displayBuffer[self.model.currentLine - 1]) - self.model.displayBuffer[self.model.currentLine - 1].extend(self.model.displayBuffer[self.model.currentLine]) - del self.model.displayBuffer[self.model.currentLine] - self.model.currentLine -= 1 - self.model.currentCol = prev_line_length # Переходим в конец предыдущей строки + elif symbolCode in (127, 8): + self.model.Backspace() - # ENTER - elif symbolCode == 10: # Enter - # Разделяем текущую строку на две части - new_line = self.model.displayBuffer[self.model.currentLine][self.model.currentCol:] - self.model.displayBuffer[self.model.currentLine] = self.model.displayBuffer[self.model.currentLine][:self.model.currentCol] - self.model.currentLine += 1 # Переходим на следующую строку - self.model.displayBuffer.insert(self.model.currentLine, new_line) # Вставляем новую строку - self.model.currentCol = 0 # Сбрасываем индекс колонки + elif symbolCode == 10: + self.model.Enter() - # Сохранение файла (Ctrl+S) - elif symbolCode == 19: # Ctrl+S - result = self.model.save_file() - if result is True: - self.view.screen.addstr(self.view.lines - 1, 0, f"File {self.model.file_path} saved successfully.") - else: - self.view.screen.addstr(self.view.lines - 1, 0, f"Error saving file: {result}") + elif symbolCode == 19: + self.model.SaveFile() else: if self.model.currentCol <= len(self.model.displayBuffer[self.model.currentLine]): # проверяем, не превышает ли индекс колонки длину строки diff --git a/models.py b/models.py index 58a7388..e912f62 100644 --- a/models.py +++ b/models.py @@ -7,31 +7,58 @@ class VimModel: self.scrollX = 0 # горизонтальная прокрутка self.file_path = "" # путь к файлу - def MoveLeft(self): + def Enter(self) -> None: + # Разделяем текущую строку на две части + new_line = self.displayBuffer[self.currentLine][self.currentCol:] + self.displayBuffer[self.currentLine] = self.displayBuffer[self.currentLine][:self.currentCol] + self.currentLine += 1 # Переходим на следующую строку + self.displayBuffer.insert(self.currentLine, new_line) # Вставляем новую строку + self.currentCol = 0 # Сбрасываем индекс колонки + + def Backspace(self) -> None: + if self.currentCol > 0: # Если символ существует в текущей строке + self.currentCol -= 1 + del self.displayBuffer[self.currentLine][self.currentCol] # Удаляем символ + elif self.currentLine > 0: # Если текущая строка не первая + # Объединяем текущую строку с предыдущей + prev_line_length = len(self.displayBuffer[self.currentLine - 1]) + self.displayBuffer[self.currentLine - 1].extend(self.displayBuffer[self.currentLine]) + del self.displayBuffer[self.currentLine] + self.currentLine -= 1 + self.currentCol = prev_line_length # Переходим в конец предыдущей строки + + def MoveLeft(self) -> None: if self.currentCol > 0: self.currentCol -= 1 elif self.currentLine > 0: self.currentLine -= 1 self.currentCol = len(self.displayBuffer[self.currentLine]) - def MoveRight(self): + def MoveRight(self) -> None: if self.currentCol < len(self.displayBuffer[self.currentLine]): self.currentCol += 1 elif self.currentLine < len(self.displayBuffer) - 1: self.currentLine += 1 self.currentCol = 0 - def MoveUp(self): + def MoveUp(self) -> None: if self.currentLine > 0: self.currentLine -= 1 self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine])) - def MoveDown(self): + def MoveDown(self) -> None: if self.currentLine < len(self.displayBuffer) - 1: self.currentLine += 1 self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine])) - def load_file(self, file_path): + def SaveFile(self) -> None: + result = self.save_file() + if result is True: + print(f"File {self.file_path} saved successfully.") + else: + print(f"Error saving file: {result}") + + def load_file(self, file_path) -> None: """Загрузка файла для редактирования""" self.file_path = file_path try: @@ -41,7 +68,7 @@ class VimModel: print(f"File {file_path} not found. Starting with empty buffer.") self.displayBuffer = [] - def save_file(self): + def save_file(self) -> None: """Сохранение файла""" try: with open(self.file_path, "w") as file: