feat: add cursor and item rendering with colors
This commit is contained in:
parent
25ec2bde43
commit
79116a9c1f
2 changed files with 32 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
40
src/UI.py
40
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:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue