feat: add cursor and item rendering with colors

This commit is contained in:
lohhiiccc 2026-06-30 23:56:13 +02:00
parent 25ec2bde43
commit 79116a9c1f
2 changed files with 32 additions and 12 deletions

View file

@ -16,6 +16,7 @@ class App():
self._current = Folder(path) self._current = Folder(path)
self._items = self.current.children() self._items = self.current.children()
self._UI = UI(self) self._UI = UI(self)
self._cursor = 2
def _run(self, stdscr: curses.window) -> None: def _run(self, stdscr: curses.window) -> None:
curses.curs_set(0) curses.curs_set(0)
@ -58,3 +59,6 @@ class App():
def UI(self) -> UI: def UI(self) -> UI:
return self._UI return self._UI
@property
def cursor(self) -> int:
return self._cursor

View file

@ -3,6 +3,7 @@ import stat
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import curses import curses
from .items.Symlink import Symlink from .items.Symlink import Symlink
from .items.AItems import AItem
if TYPE_CHECKING: if TYPE_CHECKING:
from .App import App from .App import App
@ -12,24 +13,39 @@ class UI:
def __init__(self, app: App): def __init__(self, app: App):
self._app = app self._app = app
def render(self, stdscr: curses.window): def _get_attr(self, item: any, i: int) -> str:
attr = curses.color_pair(item.color_pair)
if i == self.app.cursor:
attr |= curses.A_REVERSE
return attr
def render(self, stdscr: curses.window) -> None:
height, width = stdscr.getmaxyx() height, width = stdscr.getmaxyx()
for i in range(min(height, len(self.app.items))): for i in range(min(height, len(self.app.items))):
item = self.app.items[i] item = self.app.items[i]
name, size, perm = item.fields() self._render_item(stdscr, item, i, width)
stdscr.attron(curses.color_pair(item.color_pair)) ARROW = " -> "
stdscr.addstr(i, 0, name) ARROW_LEN = len(ARROW)
def _render_item(self, stdscr: curses.window, item: AItem, i: int, width: int) -> None:
name, size, perm = item.fields()
name_len = len(name)
col_width = int(width / 3)
if type(item) is Symlink: attr = self._get_attr(item, i)
stdscr.attron(curses.color_pair(0)) stdscr.addstr(i, 0, name.ljust(col_width), attr)
stdscr.addstr(i, len(name), " -> ")
stdscr.attron(curses.color_pair(Symlink.target_color(item.point_to)))
stdscr.addstr(i, len(name) + 4, str(item.point_to))
stdscr.addstr(i, int(width / 3), str(size)) if type(item) is Symlink:
stdscr.addstr(i, int(width / 3) * 2, stat.filemode(perm)) attr_arrow = curses.color_pair(0)
stdscr.attroff(curses.color_pair(item.color_pair)) attr_target = curses.color_pair(Symlink.target_color(item.point_to))
if i == self.app.cursor:
attr_arrow |= curses.A_REVERSE
attr_target |= curses.A_REVERSE
stdscr.addstr(i, name_len, self.ARROW, attr_arrow)
stdscr.addstr(i, name_len + self.ARROW_LEN, str(item.point_to), attr_target)
stdscr.addstr(i, col_width, str(size).ljust(col_width * 2), attr)
stdscr.addstr(i, col_width * 2, stat.filemode(perm), attr)
@property @property
def app(self) -> App: def app(self) -> App: