добавил скролл для поля ввода команды

master
serr 2025-02-05 00:24:14 +03:00
parent f67703719b
commit d200d36aee
4 changed files with 26 additions and 23 deletions

View File

@ -1,10 +1,12 @@
from mvc.models import VimModel from mvc.models import VimModel
from mvc.views import VimView from mvc.views import VimView, CursesAdapter
from mvc.controllers import Controller, ReturnCode from mvc.controllers import Controller, ReturnCode
from mvc.controllers import EditStrategy, CommandStrategy, NormalStrategy from mvc.controllers import EditStrategy, CommandStrategy, NormalStrategy
def main(): def main():
model, view = VimModel(), VimView() adapter = CursesAdapter()
model, view = VimModel(adapter.lines, adapter.cols), VimView(adapter)
# start mode - navigation mode # start mode - navigation mode
strategy = NormalStrategy(model, view.curses_adapter) strategy = NormalStrategy(model, view.curses_adapter)

View File

@ -22,7 +22,7 @@ class BaseStrategy(ABC):
case self.adapter.KEY_CTRL_S: self.model.SaveFile() case self.adapter.KEY_CTRL_S: self.model.SaveFile()
case _: return ReturnCode.CONTINUE # Не обрабатываем, передаем дальше case _: return ReturnCode.CONTINUE # Не обрабатываем, передаем дальше
self.model.Scroll(self.adapter.lines, self.adapter.cols) self.model.Scroll()
return ReturnCode.GOOD return ReturnCode.GOOD
@abstractmethod @abstractmethod
@ -59,7 +59,7 @@ class CommandStrategy(BaseStrategy):
if not isAscii(symbolCode): return ReturnCode.GOOD if not isAscii(symbolCode): return ReturnCode.GOOD
self.model.InsertCommandSymbol(symbolCode) self.model.InsertCommandSymbol(symbolCode)
self.model.Scroll(self.adapter.lines, self.adapter.cols) self.model.Scroll()
return returnCode return returnCode
class EditStrategy(BaseStrategy): class EditStrategy(BaseStrategy):
@ -80,7 +80,7 @@ class EditStrategy(BaseStrategy):
if not isAscii(symbolCode): return ReturnCode.GOOD if not isAscii(symbolCode): return ReturnCode.GOOD
self.model.InsertSymbol(symbolCode) self.model.InsertSymbol(symbolCode)
self.model.Scroll(self.adapter.lines, self.adapter.cols) self.model.Scroll()
return ReturnCode.GOOD return ReturnCode.GOOD
class NormalStrategy(BaseStrategy): class NormalStrategy(BaseStrategy):
@ -96,5 +96,5 @@ class NormalStrategy(BaseStrategy):
match symbolCode: match symbolCode:
case self.adapter.KEY_TWO_DOTS: return ReturnCode.SET_COMMAND_MODE case self.adapter.KEY_TWO_DOTS: return ReturnCode.SET_COMMAND_MODE
self.model.Scroll(self.adapter.lines, self.adapter.cols) self.model.Scroll()
return ReturnCode.GOOD return ReturnCode.GOOD

View File

@ -9,7 +9,9 @@ class ReturnCode(Enum):
CONTINUE = -96 CONTINUE = -96
class VimModel: class VimModel:
def __init__(self): def __init__(self, displayLinesCount: int, displayColsCount: int):
self.displayLinesCount = displayLinesCount
self.displayColsCount = displayColsCount
self.displayBuffer = [] # буфер для хранения всех строк self.displayBuffer = [] # буфер для хранения всех строк
self.currentLine = 0 # текущий индекс строки self.currentLine = 0 # текущий индекс строки
self.currentCol = 0 # текущий индекс колонки self.currentCol = 0 # текущий индекс колонки
@ -26,16 +28,16 @@ class VimModel:
modeBar += f" | COMMAND BUFFER: {''.join(self.commandBuffer)}" modeBar += f" | COMMAND BUFFER: {''.join(self.commandBuffer)}"
return modeBar return modeBar
def Scroll(self, displayLines: int, displayCols: int) -> None: def Scroll(self) -> None:
if self.currentLine < self.scrollY: if self.currentLine < self.scrollY:
self.scrollY = self.currentLine self.scrollY = self.currentLine
elif self.currentLine >= self.scrollY + displayLines - 1: elif self.currentLine >= self.scrollY + self.displayLinesCount - 1:
self.scrollY = self.currentLine - displayLines + 2 self.scrollY = self.currentLine - self.displayLinesCount + 2
if self.currentCol < self.scrollX: if self.currentCol < self.scrollX:
self.scrollX = self.currentCol self.scrollX = self.currentCol
elif self.currentCol >= self.scrollX + displayCols: elif self.currentCol >= self.scrollX + self.displayColsCount:
self.scrollX = self.currentCol - displayCols + 1 self.scrollX = self.currentCol - self.displayColsCount + 1
def InsertCommandSymbol(self, symbolCode: int) -> None: def InsertCommandSymbol(self, symbolCode: int) -> None:
self.commandBuffer.append(chr(symbolCode)) self.commandBuffer.append(chr(symbolCode))
@ -90,19 +92,13 @@ class VimModel:
if non_space_index is not None: if non_space_index is not None:
non_space_index += self.currentCol non_space_index += self.currentCol
right_space_index = line.find(' ', non_space_index) right_space_index = line.find(' ', non_space_index)
if right_space_index != -1: self.currentCol = right_space_index if right_space_index != -1 else len(line)
self.currentCol = right_space_index
else:
self.currentCol = len(self.displayBuffer[self.currentLine])
case "b": # Перемещает курсор в начало слова слева от курсора case "b": # Перемещает курсор в начало слова слева от курсора
line = ''.join(self.displayBuffer[self.currentLine]) line = ''.join(self.displayBuffer[self.currentLine])
non_space_index = next((i for i in range(self.currentCol - 1, -1, -1) if line[i] != ' '), None) non_space_index = next((i for i in range(self.currentCol - 1, -1, -1) if line[i] != ' '), None)
if non_space_index is not None: if non_space_index is not None:
left_space_index = line.rfind(' ', 0, non_space_index) left_space_index = line.rfind(' ', 0, non_space_index)
if left_space_index != -1: self.currentCol = left_space_index + 1 if left_space_index != -1 else 0
self.currentCol = left_space_index + 1
else:
self.currentCol = 0
case "gg": # Переход в начало файла case "gg": # Переход в начало файла
self.currentLine = 0 self.currentLine = 0
self.currentCol = 0 self.currentCol = 0
@ -135,6 +131,7 @@ class VimModel:
return ReturnCode.GOOD return ReturnCode.GOOD
def WordUnderCursor(self)-> tuple[int, int]: def WordUnderCursor(self)-> tuple[int, int]:
"""Возвращает индекс начала и индекс конца слова под курсором"""
line = ''.join(self.displayBuffer[self.currentLine]) line = ''.join(self.displayBuffer[self.currentLine])
start_index = line.rfind(' ', 0, self.currentCol) start_index = line.rfind(' ', 0, self.currentCol)
start_index = 0 if start_index == -1 else start_index + 1 start_index = 0 if start_index == -1 else start_index + 1

View File

@ -44,8 +44,8 @@ class CursesAdapter:
class VimView: class VimView:
def __init__(self) -> None: def __init__(self, adapter: CursesAdapter) -> None:
self.curses_adapter = CursesAdapter() self.curses_adapter = adapter
def Render(self, def Render(self,
@ -83,4 +83,8 @@ class VimView:
def SetModeBar(self, modeBarData: str): def SetModeBar(self, modeBarData: str):
"""Print edit mode information panel""" """Print edit mode information panel"""
self.curses_adapter.SetString(self.curses_adapter.lines - 1, 0, modeBarData) if len(modeBarData) > self.curses_adapter.cols - 1:
scrollX = len(modeBarData)-self.curses_adapter.cols
self.curses_adapter.SetString(self.curses_adapter.lines - 1, 0, modeBarData[scrollX+1:])
else:
self.curses_adapter.SetString(self.curses_adapter.lines - 1, 0, modeBarData)