74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
import curses
|
|
|
|
class CursesAdapter:
|
|
def __init__(self) -> None:
|
|
self.screen = curses.initscr()
|
|
self.screen.keypad(True)
|
|
self.cols = curses.COLS
|
|
self.lines = curses.LINES
|
|
# init color system
|
|
curses.start_color()
|
|
# green color pair
|
|
curses.init_pair(1, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
|
|
curses.curs_set(1) # Make cursor visible
|
|
|
|
def Refresh(self) -> None:
|
|
"""Apply changes"""
|
|
self.screen.refresh()
|
|
|
|
def Cleanup(self) -> None:
|
|
curses.endwin()
|
|
|
|
def SetCursor(self, x: int, y: int):
|
|
"""set cursor position (x, y)"""
|
|
self.screen.move(x, y)
|
|
|
|
def SetChar(self, x: int, y: int, code: int):
|
|
"""set char position (x, y)"""
|
|
self.screen.addch(x, y, code)
|
|
|
|
def SetString(self, x: int, y: int, data: str):
|
|
"""set string begin position (x, y)"""
|
|
self.screen.addstr(x, y, data)
|
|
|
|
def GetChar(self) -> int:
|
|
"""Wait users input"""
|
|
return self.screen.getch()
|
|
|
|
def Render(self,
|
|
displayBuffer: list[list[str]],
|
|
currentLine: int, currentCol: int,
|
|
scrollX: int, scrollY: int,
|
|
file_path: str):
|
|
"""Отрисовка текущего состояния"""
|
|
|
|
self.screen.clear()
|
|
|
|
# display visible part of text
|
|
for i in range(self.lines - 1):
|
|
if i + scrollY < len(displayBuffer):
|
|
line = ''.join(displayBuffer[i + scrollY])
|
|
if scrollX < len(line):
|
|
self.SetString(i, 0, line[scrollX:scrollX + self.cols])
|
|
else:
|
|
self.SetString(i, 0, '')
|
|
else:
|
|
self.SetString(i, 0, '')
|
|
|
|
# update mode bar
|
|
self.EditModeBar(currentLine, len(displayBuffer), file_path)
|
|
# update cursor
|
|
self.SetCursor(currentLine - scrollY, currentCol - scrollX)
|
|
self.Refresh()
|
|
|
|
def EditModeBar(self, currentLine, totalLines, fileName):
|
|
"""Print edit mode information panel"""
|
|
self.screen.addstr(self.lines - 1, 0, ' ' * (self.cols - 1)) # Очистка строки
|
|
self.screen.addstr(self.lines - 1, 0, "FILE: ")
|
|
self.screen.addstr(fileName, curses.color_pair(1)) # Имя файла
|
|
self.screen.addstr(" | MODE:")
|
|
self.screen.addstr(" EDIT", curses.color_pair(1))
|
|
self.screen.addstr(" | LINE: ")
|
|
self.screen.addstr(str(currentLine + 1), curses.color_pair(1))
|
|
self.screen.addstr("/")
|
|
self.screen.addstr(str(totalLines), curses.color_pair(1)) # Общее количество строк |