o filename -> открывает файл filename, не сохраняя изменения в текущем файле

master
serr 2025-02-05 01:02:33 +03:00
parent 7e5c5d3723
commit cffa12f07e
1 changed files with 21 additions and 0 deletions

View File

@ -139,6 +139,13 @@ class VimModel:
self.currentLine += self.displayLinesCount self.currentLine += self.displayLinesCount
else: else:
self.currentLine = len(self.displayBuffer) - 1 self.currentLine = len(self.displayBuffer) - 1
case _:
# Открывает файл
if len(cmd) > 2 and cmd[:2] == 'o ':
filename = cmd[2:]
self.LoadFile(filename)
return ReturnCode.SET_BASIC_MODE
return ReturnCode.GOOD return ReturnCode.GOOD
@ -201,10 +208,13 @@ class VimModel:
def LoadFile(self, file_path) -> None: def LoadFile(self, file_path) -> None:
"""Загрузка файла для редактирования""" """Загрузка файла для редактирования"""
self.Reset()
self.file_path = file_path self.file_path = file_path
self.mode = "NORMAL"
try: try:
with open(file_path, "r") as file: with open(file_path, "r") as file:
self.displayBuffer = [list(line.rstrip('\n')) for line in file.readlines()] self.displayBuffer = [list(line.rstrip('\n')) for line in file.readlines()]
except FileNotFoundError: except FileNotFoundError:
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 = []
@ -218,3 +228,14 @@ class VimModel:
print(f"File {self.file_path} saved successfully.") print(f"File {self.file_path} saved successfully.")
except Exception as e: except Exception as e:
print(f"Error saving file: {str(e)}") print(f"Error saving file: {str(e)}")
def Reset(self) -> None:
self.displayBuffer = []
self.currentLine = 0
self.currentCol = 0
self.scrollY = 0
self.scrollX = 0
self.file_path = ""
self.mode = ""
self.commandBuffer = []
self.exchangeBuffer = []