From 79116a9c1f047ffaa6c549a9d568d0fdea704367 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 30 Jun 2026 23:56:13 +0200 Subject: [PATCH] feat: add cursor and item rendering with colors --- src/App.py | 4 ++++ src/UI.py | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/App.py b/src/App.py index 7b35cc1..6342c4f 100644 --- a/src/App.py +++ b/src/App.py @@ -16,6 +16,7 @@ class App(): self._current = Folder(path) self._items = self.current.children() self._UI = UI(self) + self._cursor = 2 def _run(self, stdscr: curses.window) -> None: curses.curs_set(0) @@ -58,3 +59,6 @@ class App(): def UI(self) -> UI: return self._UI + @property + def cursor(self) -> int: + return self._cursor diff --git a/src/UI.py b/src/UI.py index 2b010b1..129e4cf 100644 --- a/src/UI.py +++ b/src/UI.py @@ -3,6 +3,7 @@ import stat from typing import TYPE_CHECKING import curses from .items.Symlink import Symlink +from .items.AItems import AItem if TYPE_CHECKING: from .App import App @@ -12,24 +13,39 @@ class UI: def __init__(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() for i in range(min(height, len(self.app.items))): 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)) - stdscr.addstr(i, 0, name) + ARROW = " -> " + 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: - stdscr.attron(curses.color_pair(0)) - 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)) + attr = self._get_attr(item, i) + stdscr.addstr(i, 0, name.ljust(col_width), attr) - stdscr.addstr(i, int(width / 3), str(size)) - stdscr.addstr(i, int(width / 3) * 2, stat.filemode(perm)) - stdscr.attroff(curses.color_pair(item.color_pair)) + if type(item) is Symlink: + attr_arrow = curses.color_pair(0) + 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 def app(self) -> App: