mirror of
https://github.com/rr-/pq-cli.git
synced 2026-09-29 06:44:00 -06:00
ui/curses: highlight changed and added items
This commit is contained in:
@@ -2,6 +2,8 @@ import curses
|
||||
import enum
|
||||
import typing as T
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_HIGHLIGHT, has_colors
|
||||
|
||||
from .scrollable import Scrollable
|
||||
|
||||
|
||||
@@ -19,6 +21,7 @@ class DataTable(Scrollable):
|
||||
|
||||
self._align_right = align_right
|
||||
self._items: T.List[T.Tuple[str, str]]
|
||||
self._selected: T.Optional[int] = None
|
||||
|
||||
def add(self, text: str, value: str) -> None:
|
||||
self._items.append((text, value))
|
||||
@@ -38,6 +41,9 @@ class DataTable(Scrollable):
|
||||
del self._items[idx]
|
||||
self.scroll_to_item(idx)
|
||||
|
||||
def select(self, text: T.Optional[str]) -> None:
|
||||
self._selected = None if text is None else self.get_idx(text)
|
||||
|
||||
def get_idx(self, text: str) -> T.Optional[int]:
|
||||
for idx, row in enumerate(self._items):
|
||||
if row[0].strip() == text.strip():
|
||||
@@ -48,20 +54,28 @@ class DataTable(Scrollable):
|
||||
assert self._pad
|
||||
if self._align_right:
|
||||
for y, row in enumerate(self._items):
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attron(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
self._pad.move(y, 0)
|
||||
self._pad.addnstr(row[0], min(len(row[0]), w))
|
||||
col2_x = max(0, w - len(row[1]))
|
||||
if col2_x < w:
|
||||
self._pad.move(y, col2_x)
|
||||
self._pad.addstr(row[1])
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attroff(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
else:
|
||||
col2_x = self._get_col_width(0)
|
||||
for y, row in enumerate(self._items):
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attron(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
self._pad.addnstr(y, 0, row[0], min(len(row[0]), w))
|
||||
if col2_x < w:
|
||||
self._pad.addnstr(
|
||||
y, col2_x, row[1], min(len(row[1]), w - col2_x)
|
||||
)
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attroff(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
|
||||
def _get_col_width(self, x: int) -> int:
|
||||
return max([len(row[0]) for row in self._items] + [0])
|
||||
|
||||
@@ -2,12 +2,18 @@ import curses
|
||||
import enum
|
||||
import typing as T
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_HIGHLIGHT, has_colors
|
||||
|
||||
from .scrollable import Scrollable
|
||||
|
||||
|
||||
class ListBox(Scrollable):
|
||||
_items: T.List[str]
|
||||
|
||||
def __init__(self, parent: T.Any, h: int, w: int, y: int, x: int) -> None:
|
||||
super().__init__(parent, h, w, y, x)
|
||||
self._selected: T.Optional[int] = None
|
||||
|
||||
def add(self, text: str) -> None:
|
||||
self._items.append(text)
|
||||
self.scroll_to_item(-1)
|
||||
@@ -30,7 +36,16 @@ class ListBox(Scrollable):
|
||||
def delete(self, idx: int, count: int = 1) -> None:
|
||||
del self._items[idx : idx + count]
|
||||
|
||||
def select(self, idx: T.Optional[int]) -> None:
|
||||
if idx < 0:
|
||||
idx %= len(self._items)
|
||||
self._selected = idx
|
||||
|
||||
def _render_impl(self, h: int, w: int) -> None:
|
||||
assert self._pad
|
||||
for y, item in enumerate(self._items):
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attron(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
self._pad.addnstr(y, 0, item, min(len(item), w))
|
||||
if y == self._selected and has_colors():
|
||||
self._pad.attroff(curses.color_pair(COLOR_HIGHLIGHT))
|
||||
|
||||
Reference in New Issue
Block a user