PyExplorer/src/IO/UI.py

52 lines
1.7 KiB
Python

from __future__ import annotations
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
class UI:
def __init__(self, app: App):
self._app = app
def _get_attr(self, item: any, i: int) -> str:
attr = curses.color_pair(item.color_pair)
if i == self.app.cursor.pos:
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]
self._render_item(stdscr, item, i, width)
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)
attr = self._get_attr(item, i)
stdscr.addstr(i, 0, name.ljust(col_width), attr)
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.pos:
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:
return self._app