From 40b223a5ec1396e76183b09edec68cd29b0e7936 Mon Sep 17 00:00:00 2001 From: serr Date: Tue, 4 Feb 2025 14:14:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20scroll=20?= =?UTF-8?q?=D0=B2=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8,=20match=20case?= =?UTF-8?q?=20=D0=B2=20handle=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers.py | 56 ++++++++++++-------------------------------------- main.py | 6 +++--- models.py | 31 ++++++++++++++++++---------- 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/controllers.py b/controllers.py index 296a1ee..2293123 100644 --- a/controllers.py +++ b/controllers.py @@ -8,53 +8,23 @@ class EditController: self.model = model self.view = view - def handle_input(self, symbolCode): + def HandleInput(self, symbolCode): """Обработка ввода пользователя""" - - # escape - if symbolCode == 27: - return False - - elif symbolCode == curses.KEY_LEFT: - self.model.MoveLeft() - - elif symbolCode == curses.KEY_RIGHT: - self.model.MoveRight() - - elif symbolCode == curses.KEY_UP: - self.model.MoveUp() - - elif symbolCode == curses.KEY_DOWN: - self.model.MoveDown() - - elif symbolCode in (127, 8): - self.model.Backspace() - - elif symbolCode == 10: - self.model.Enter() - - elif symbolCode == 19: - self.model.SaveFile() - - else: - if self.model.currentCol <= len(self.model.displayBuffer[self.model.currentLine]): # проверяем, не превышает ли индекс колонки длину строки - self.model.displayBuffer[self.model.currentLine].insert(self.model.currentCol, chr(symbolCode)) - self.model.currentCol += 1 - - # Прокрутка экрана - if self.model.currentLine < self.model.scrollY: - self.model.scrollY = self.model.currentLine - elif self.model.currentLine >= self.model.scrollY + self.view.lines - 1: - self.model.scrollY = self.model.currentLine - self.view.lines + 2 - - if self.model.currentCol < self.model.scrollX: - self.model.scrollX = self.model.currentCol - elif self.model.currentCol >= self.model.scrollX + self.view.cols: - self.model.scrollX = self.model.currentCol - self.view.cols + 1 + match symbolCode: + case 27: return False # escape + case curses.KEY_LEFT: self.model.MoveLeft() + case curses.KEY_RIGHT: self.model.MoveRight() + case curses.KEY_UP: self.model.MoveUp() + case curses.KEY_DOWN: self.model.MoveDown() + case 127 | 8: self.model.Backspace() + case 10: self.model.Enter() + case 19: self.model.SaveFile() + case _: self.model.InsertSymbol(symbolCode) + self.model.Scroll(self.view.lines, self.view.cols) return True - def render(self): + def Render(self): """Отрисовка текущего состояния""" self.view.screen.clear() diff --git a/main.py b/main.py index be6a109..4d4d938 100644 --- a/main.py +++ b/main.py @@ -9,12 +9,12 @@ def main(): # Загрузка файла для редактирования file_path = "example.txt" # Укажите путь к файлу - model.load_file(file_path) + model.LoadFile(file_path) while True: - controller.render() + controller.Render() symbolCode = view.GetChar() - if not controller.handle_input(symbolCode): + if not controller.HandleInput(symbolCode): break view.Cleanup() diff --git a/models.py b/models.py index e912f62..a403768 100644 --- a/models.py +++ b/models.py @@ -7,6 +7,22 @@ class VimModel: self.scrollX = 0 # горизонтальная прокрутка self.file_path = "" # путь к файлу + def Scroll(self, displayLines: int, displayCols: int) -> None: + if self.currentLine < self.scrollY: + self.scrollY = self.currentLine + elif self.currentLine >= self.scrollY + displayLines - 1: + self.scrollY = self.currentLine - displayLines + 2 + + if self.currentCol < self.scrollX: + self.scrollX = self.currentCol + elif self.currentCol >= self.scrollX + displayCols: + self.scrollX = self.currentCol - displayCols + 1 + + def InsertSymbol(self, symbolCode: int) -> None: + if self.currentCol <= len(self.displayBuffer[self.currentLine]): # проверяем, не превышает ли индекс колонки длину строки + self.displayBuffer[self.currentLine].insert(self.currentCol, chr(symbolCode)) + self.currentCol += 1 + def Enter(self) -> None: # Разделяем текущую строку на две части new_line = self.displayBuffer[self.currentLine][self.currentCol:] @@ -51,14 +67,7 @@ class VimModel: self.currentLine += 1 self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine])) - 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: + def LoadFile(self, file_path) -> None: """Загрузка файла для редактирования""" self.file_path = file_path try: @@ -68,12 +77,12 @@ class VimModel: print(f"File {file_path} not found. Starting with empty buffer.") self.displayBuffer = [] - def save_file(self) -> None: + def SaveFile(self) -> None: """Сохранение файла""" try: with open(self.file_path, "w") as file: for line in self.displayBuffer: file.write(''.join(line) + '\n') - return True + print(f"File {self.file_path} saved successfully.") except Exception as e: - return str(e) \ No newline at end of file + print(f"Error saving file: {str(e)}")