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

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.views import VimView
from mvc.views import VimView, CursesAdapter
from mvc.controllers import Controller, ReturnCode
from mvc.controllers import EditStrategy, CommandStrategy, NormalStrategy
def main():
model, view = VimModel(), VimView()
adapter = CursesAdapter()
model, view = VimModel(adapter.lines, adapter.cols), VimView(adapter)
# start mode - navigation mode
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 _: return ReturnCode.CONTINUE # Не обрабатываем, передаем дальше
self.model.Scroll(self.adapter.lines, self.adapter.cols)
self.model.Scroll()
return ReturnCode.GOOD
@abstractmethod
@ -59,7 +59,7 @@ class CommandStrategy(BaseStrategy):
if not isAscii(symbolCode): return ReturnCode.GOOD
self.model.InsertCommandSymbol(symbolCode)
self.model.Scroll(self.adapter.lines, self.adapter.cols)
self.model.Scroll()
return returnCode
class EditStrategy(BaseStrategy):
@ -80,7 +80,7 @@ class EditStrategy(BaseStrategy):
if not isAscii(symbolCode): return ReturnCode.GOOD
self.model.InsertSymbol(symbolCode)
self.model.Scroll(self.adapter.lines, self.adapter.cols)
self.model.Scroll()
return ReturnCode.GOOD
class NormalStrategy(BaseStrategy):
@ -96,5 +96,5 @@ class NormalStrategy(BaseStrategy):
match symbolCode:
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

View File

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

View File

@ -44,8 +44,8 @@ class CursesAdapter:
class VimView:
def __init__(self) -> None:
self.curses_adapter = CursesAdapter()
def __init__(self, adapter: CursesAdapter) -> None:
self.curses_adapter = adapter
def Render(self,
@ -83,4 +83,8 @@ class VimView:
def SetModeBar(self, modeBarData: str):
"""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)