метод backspace, enter, savefile для модели
parent
f79ad4010f
commit
72dda17eed
|
@ -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]): # проверяем, не превышает ли индекс колонки длину строки
|
||||
|
|
39
models.py
39
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:
|
||||
|
|
Loading…
Reference in New Issue