2025-02-04 18:36:12 +03:00
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
class ReturnCode(Enum):
|
|
|
|
|
GOOD = -101
|
|
|
|
|
EXIT_CODE = -100
|
|
|
|
|
SET_BASIC_MODE = -99
|
|
|
|
|
SET_COMMAND_MODE = -98
|
2025-02-04 18:46:16 +03:00
|
|
|
|
SET_EDIT_MODE = -97
|
2025-02-04 19:06:51 +03:00
|
|
|
|
CONTINUE = -96
|
2025-02-04 18:36:12 +03:00
|
|
|
|
|
2025-02-04 13:16:23 +03:00
|
|
|
|
class VimModel:
|
2025-02-04 20:01:25 +03:00
|
|
|
|
def __init__(self, displayLines: int, displayCols: int):
|
|
|
|
|
self.displayLines = displayLines
|
|
|
|
|
self.displayCols = displayCols
|
2025-02-04 13:16:23 +03:00
|
|
|
|
self.displayBuffer = [] # буфер для хранения всех строк
|
|
|
|
|
self.currentLine = 0 # текущий индекс строки
|
|
|
|
|
self.currentCol = 0 # текущий индекс колонки
|
|
|
|
|
self.scrollY = 0 # вертикальная прокрутка
|
|
|
|
|
self.scrollX = 0 # горизонтальная прокрутка
|
|
|
|
|
self.file_path = "" # путь к файлу
|
2025-02-04 18:36:12 +03:00
|
|
|
|
self.mode = ""
|
|
|
|
|
self.commandBuffer = [] # буффер для команды
|
|
|
|
|
|
|
|
|
|
def ModeBar(self) -> str:
|
|
|
|
|
modeBar = f"MODE: {self.mode} | FILE: {self.file_path} | LINE: {self.currentLine+1}/{len(self.displayBuffer)}"
|
|
|
|
|
if self.mode == "COMMAND":
|
|
|
|
|
modeBar += f" | COMMAND BUFFER: {''.join(self.commandBuffer)}"
|
|
|
|
|
return modeBar
|
2025-02-04 13:16:23 +03:00
|
|
|
|
|
2025-02-04 14:14:46 +03:00
|
|
|
|
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
|
|
|
|
|
|
2025-02-04 18:36:12 +03:00
|
|
|
|
def InsertCommandSymbol(self, symbolCode: int) -> None:
|
|
|
|
|
self.commandBuffer.append(chr(symbolCode))
|
|
|
|
|
|
2025-02-04 14:14:46 +03:00
|
|
|
|
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
|
|
|
|
|
|
2025-02-04 20:01:25 +03:00
|
|
|
|
def MoveToLineStart(self) -> None:
|
|
|
|
|
"""Переместить курсор в начало строки и сбросить прокрутку"""
|
|
|
|
|
self.currentCol = 0
|
|
|
|
|
self.scrollX = 0 # Сброс горизонтальной прокрутки
|
|
|
|
|
|
|
|
|
|
def MoveToLineEnd(self) -> None:
|
|
|
|
|
"""Переместить курсор в конец строки с учетом прокрутки"""
|
|
|
|
|
line_length = len(self.displayBuffer[self.currentLine])
|
|
|
|
|
self.currentCol = line_length
|
|
|
|
|
|
|
|
|
|
# Если строка длиннее экрана, корректируем прокрутку
|
|
|
|
|
if line_length > self.displayCols:
|
|
|
|
|
self.scrollX = max(0, line_length - self.displayCols + 1)
|
|
|
|
|
|
2025-02-04 18:36:12 +03:00
|
|
|
|
def EnterCommand(self):
|
2025-02-04 20:01:25 +03:00
|
|
|
|
"""Обработка введенной команды"""
|
2025-02-04 18:36:12 +03:00
|
|
|
|
cmd = ''.join(self.commandBuffer)
|
|
|
|
|
self.commandBuffer.clear()
|
|
|
|
|
match cmd:
|
|
|
|
|
case "$":
|
2025-02-04 20:01:25 +03:00
|
|
|
|
self.MoveToLineEnd() # Перемещение в конец строки
|
|
|
|
|
return ReturnCode.GOOD
|
|
|
|
|
case "^" | "0":
|
|
|
|
|
self.MoveToLineStart() # Перемещение в начало строки
|
2025-02-04 18:36:12 +03:00
|
|
|
|
return ReturnCode.GOOD
|
|
|
|
|
case "q":
|
|
|
|
|
return ReturnCode.EXIT_CODE
|
2025-02-04 20:01:25 +03:00
|
|
|
|
case "i":
|
|
|
|
|
return ReturnCode.SET_EDIT_MODE
|
2025-02-04 18:46:16 +03:00
|
|
|
|
case "S":
|
2025-02-04 20:01:25 +03:00
|
|
|
|
self.MoveToLineStart()
|
2025-02-04 18:46:16 +03:00
|
|
|
|
self.displayBuffer[self.currentLine] = []
|
|
|
|
|
return ReturnCode.SET_EDIT_MODE
|
2025-02-04 20:01:25 +03:00
|
|
|
|
case "RIGHT":
|
|
|
|
|
if self.currentCol < len(self.displayBuffer[self.currentLine]):
|
|
|
|
|
self.currentCol += 1
|
|
|
|
|
return ReturnCode.GOOD
|
|
|
|
|
case "LEFT":
|
|
|
|
|
if self.currentCol > 0:
|
|
|
|
|
self.currentCol -= 1
|
|
|
|
|
return ReturnCode.GOOD
|
|
|
|
|
case "UP":
|
|
|
|
|
if self.currentLine > 0:
|
|
|
|
|
self.currentLine -= 1
|
|
|
|
|
if self.currentCol > len(self.displayBuffer[self.currentLine]):
|
|
|
|
|
self.currentCol = len(self.displayBuffer[self.currentLine])
|
|
|
|
|
return ReturnCode.GOOD
|
|
|
|
|
case "DOWN":
|
|
|
|
|
if self.currentLine < len(self.displayBuffer) - 1:
|
|
|
|
|
self.currentLine += 1
|
|
|
|
|
if self.currentCol > len(self.displayBuffer[self.currentLine]):
|
|
|
|
|
self.currentCol = len(self.displayBuffer[self.currentLine])
|
|
|
|
|
return ReturnCode.GOOD
|
2025-02-04 18:36:12 +03:00
|
|
|
|
|
2025-02-04 13:49:40 +03:00
|
|
|
|
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 # Сбрасываем индекс колонки
|
|
|
|
|
|
2025-02-04 18:36:12 +03:00
|
|
|
|
def BackspaceCommand(self) -> None:
|
|
|
|
|
if len(self.commandBuffer) > 0:
|
|
|
|
|
self.commandBuffer.pop()
|
|
|
|
|
|
2025-02-04 13:49:40 +03:00
|
|
|
|
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:
|
2025-02-04 13:37:47 +03:00
|
|
|
|
if self.currentCol > 0:
|
|
|
|
|
self.currentCol -= 1
|
|
|
|
|
elif self.currentLine > 0:
|
|
|
|
|
self.currentLine -= 1
|
|
|
|
|
self.currentCol = len(self.displayBuffer[self.currentLine])
|
|
|
|
|
|
2025-02-04 13:49:40 +03:00
|
|
|
|
def MoveRight(self) -> None:
|
2025-02-04 13:37:47 +03:00
|
|
|
|
if self.currentCol < len(self.displayBuffer[self.currentLine]):
|
|
|
|
|
self.currentCol += 1
|
|
|
|
|
elif self.currentLine < len(self.displayBuffer) - 1:
|
|
|
|
|
self.currentLine += 1
|
|
|
|
|
self.currentCol = 0
|
|
|
|
|
|
2025-02-04 13:49:40 +03:00
|
|
|
|
def MoveUp(self) -> None:
|
2025-02-04 13:37:47 +03:00
|
|
|
|
if self.currentLine > 0:
|
|
|
|
|
self.currentLine -= 1
|
|
|
|
|
self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine]))
|
|
|
|
|
|
2025-02-04 13:49:40 +03:00
|
|
|
|
def MoveDown(self) -> None:
|
2025-02-04 13:37:47 +03:00
|
|
|
|
if self.currentLine < len(self.displayBuffer) - 1:
|
|
|
|
|
self.currentLine += 1
|
|
|
|
|
self.currentCol = min(self.currentCol, len(self.displayBuffer[self.currentLine]))
|
|
|
|
|
|
2025-02-04 14:14:46 +03:00
|
|
|
|
def LoadFile(self, file_path) -> None:
|
2025-02-04 13:16:23 +03:00
|
|
|
|
"""Загрузка файла для редактирования"""
|
|
|
|
|
self.file_path = file_path
|
|
|
|
|
try:
|
|
|
|
|
with open(file_path, "r") as file:
|
|
|
|
|
self.displayBuffer = [list(line.rstrip('\n')) for line in file.readlines()]
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print(f"File {file_path} not found. Starting with empty buffer.")
|
|
|
|
|
self.displayBuffer = []
|
|
|
|
|
|
2025-02-04 14:14:46 +03:00
|
|
|
|
def SaveFile(self) -> None:
|
2025-02-04 13:16:23 +03:00
|
|
|
|
"""Сохранение файла"""
|
|
|
|
|
try:
|
|
|
|
|
with open(self.file_path, "w") as file:
|
|
|
|
|
for line in self.displayBuffer:
|
|
|
|
|
file.write(''.join(line) + '\n')
|
2025-02-04 14:14:46 +03:00
|
|
|
|
print(f"File {self.file_path} saved successfully.")
|
2025-02-04 13:16:23 +03:00
|
|
|
|
except Exception as e:
|
2025-02-04 14:14:46 +03:00
|
|
|
|
print(f"Error saving file: {str(e)}")
|