метод scroll в модели, match case в handle input
parent
72dda17eed
commit
40b223a5ec
|
@ -8,53 +8,23 @@ class EditController:
|
||||||
self.model = model
|
self.model = model
|
||||||
self.view = view
|
self.view = view
|
||||||
|
|
||||||
def handle_input(self, symbolCode):
|
def HandleInput(self, symbolCode):
|
||||||
"""Обработка ввода пользователя"""
|
"""Обработка ввода пользователя"""
|
||||||
|
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)
|
||||||
|
|
||||||
# escape
|
self.model.Scroll(self.view.lines, self.view.cols)
|
||||||
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
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def render(self):
|
def Render(self):
|
||||||
"""Отрисовка текущего состояния"""
|
"""Отрисовка текущего состояния"""
|
||||||
self.view.screen.clear()
|
self.view.screen.clear()
|
||||||
|
|
||||||
|
|
6
main.py
6
main.py
|
@ -9,12 +9,12 @@ def main():
|
||||||
|
|
||||||
# Загрузка файла для редактирования
|
# Загрузка файла для редактирования
|
||||||
file_path = "example.txt" # Укажите путь к файлу
|
file_path = "example.txt" # Укажите путь к файлу
|
||||||
model.load_file(file_path)
|
model.LoadFile(file_path)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
controller.render()
|
controller.Render()
|
||||||
symbolCode = view.GetChar()
|
symbolCode = view.GetChar()
|
||||||
if not controller.handle_input(symbolCode):
|
if not controller.HandleInput(symbolCode):
|
||||||
break
|
break
|
||||||
|
|
||||||
view.Cleanup()
|
view.Cleanup()
|
||||||
|
|
31
models.py
31
models.py
|
@ -7,6 +7,22 @@ class VimModel:
|
||||||
self.scrollX = 0 # горизонтальная прокрутка
|
self.scrollX = 0 # горизонтальная прокрутка
|
||||||
self.file_path = "" # путь к файлу
|
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:
|
def Enter(self) -> None:
|
||||||
# Разделяем текущую строку на две части
|
# Разделяем текущую строку на две части
|
||||||
new_line = self.displayBuffer[self.currentLine][self.currentCol:]
|
new_line = self.displayBuffer[self.currentLine][self.currentCol:]
|
||||||
|
@ -51,14 +67,7 @@ class VimModel:
|
||||||
self.currentLine += 1
|
self.currentLine += 1
|
||||||
self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine]))
|
self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine]))
|
||||||
|
|
||||||
def SaveFile(self) -> None:
|
def LoadFile(self, file_path) -> 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
|
self.file_path = file_path
|
||||||
try:
|
try:
|
||||||
|
@ -68,12 +77,12 @@ class VimModel:
|
||||||
print(f"File {file_path} not found. Starting with empty buffer.")
|
print(f"File {file_path} not found. Starting with empty buffer.")
|
||||||
self.displayBuffer = []
|
self.displayBuffer = []
|
||||||
|
|
||||||
def save_file(self) -> None:
|
def SaveFile(self) -> None:
|
||||||
"""Сохранение файла"""
|
"""Сохранение файла"""
|
||||||
try:
|
try:
|
||||||
with open(self.file_path, "w") as file:
|
with open(self.file_path, "w") as file:
|
||||||
for line in self.displayBuffer:
|
for line in self.displayBuffer:
|
||||||
file.write(''.join(line) + '\n')
|
file.write(''.join(line) + '\n')
|
||||||
return True
|
print(f"File {self.file_path} saved successfully.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
print(f"Error saving file: {str(e)}")
|
||||||
|
|
Loading…
Reference in New Issue