mirror of
https://github.com/rr-/pq-cli.git
synced 2026-09-28 22:33:59 -06:00
ui/curses: add colors
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from .base import Widget, WindowWrapper
|
||||
from .data_table import DataTable
|
||||
from .focusable import Focusable
|
||||
from .label import Label
|
||||
from .list_box import ListBox
|
||||
from .menu import Menu
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import contextlib
|
||||
import curses
|
||||
import typing as T
|
||||
|
||||
from pqcli.ui.curses.colors import COLOR_FOCUSED, has_colors
|
||||
from pqcli.ui.curses.event_handler import EventHandler
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def focus_standout(focused: bool, win: T.Any) -> T.Generator:
|
||||
if focused:
|
||||
if has_colors():
|
||||
win.attron(curses.color_pair(COLOR_FOCUSED))
|
||||
else:
|
||||
win.standout()
|
||||
yield
|
||||
if focused:
|
||||
if has_colors():
|
||||
win.attroff(curses.color_pair(COLOR_FOCUSED))
|
||||
else:
|
||||
win.standend()
|
||||
|
||||
|
||||
class Focusable:
|
||||
def __init__(self, *args: T.Any, **kwargs: T.Any) -> None:
|
||||
self._focused = False
|
||||
self._on_focus_change = EventHandler()
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def focused(self) -> bool:
|
||||
return self._focused
|
||||
|
||||
@focused.setter
|
||||
def focused(self, focused: bool) -> None:
|
||||
self._focused = focused
|
||||
self._on_focus_change()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def focus_standout(self, win: T.Any) -> T.Generator:
|
||||
with focus_standout(self._focused, win):
|
||||
yield
|
||||
@@ -3,6 +3,7 @@ import curses.ascii
|
||||
import typing as T
|
||||
|
||||
from pqcli.ui.curses.util import KEYS_DOWN, KEYS_UP
|
||||
from pqcli.ui.curses.widgets.focusable import focus_standout
|
||||
|
||||
from ..util import Choice
|
||||
from .base import Widget
|
||||
@@ -73,11 +74,8 @@ class Menu(Widget):
|
||||
|
||||
for y, choice in enumerate(self._choices):
|
||||
self._pad.move(len(self._header_lines) + 1 + y, 0)
|
||||
if y == self._active_choice:
|
||||
self._pad.standout()
|
||||
self._pad.addstr(choice.desc)
|
||||
if y == self._active_choice:
|
||||
self._pad.standend()
|
||||
with focus_standout(y == self._active_choice, self._pad):
|
||||
self._pad.addstr(choice.desc)
|
||||
|
||||
self._pad.refresh(
|
||||
0,
|
||||
|
||||
@@ -3,6 +3,7 @@ import datetime
|
||||
import typing as T
|
||||
|
||||
from pqcli.lingo import format_timespan
|
||||
from pqcli.ui.curses.colors import COLOR_PROGRESSBAR, has_colors
|
||||
from pqcli.ui.curses.widgets.base import WindowWrapper
|
||||
|
||||
|
||||
@@ -47,7 +48,13 @@ class ProgressBar(WindowWrapper):
|
||||
self._win.addnstr(0, x, text, min(len(text), self.getmaxyx()[1] - 1))
|
||||
x = int(cur_pos * self.getmaxyx()[1] // max_pos)
|
||||
if x > 0:
|
||||
self._win.chgat(0, 0, curses.A_REVERSE)
|
||||
self._win.chgat(
|
||||
0,
|
||||
0,
|
||||
curses.color_pair(COLOR_PROGRESSBAR)
|
||||
if has_colors()
|
||||
else curses.A_REVERSE,
|
||||
)
|
||||
if x < self.getmaxyx()[1]:
|
||||
self._win.chgat(0, x, curses.A_NORMAL)
|
||||
self._win.noutrefresh()
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import curses
|
||||
import typing as T
|
||||
|
||||
from pqcli.ui.curses.colors import (
|
||||
COLOR_SCROLLBAR_THUMB,
|
||||
COLOR_SCROLLBAR_TRACK,
|
||||
has_colors,
|
||||
)
|
||||
|
||||
from .base import WindowWrapper
|
||||
|
||||
|
||||
@@ -69,13 +75,25 @@ class Scrollable(WindowWrapper):
|
||||
y2 = self._scroll_pos + h
|
||||
thumb_y1 = int(y1 * h // len(self._items))
|
||||
thumb_y2 = int(y2 * h // len(self._items))
|
||||
|
||||
attr_thumb = (
|
||||
curses.color_pair(COLOR_SCROLLBAR_THUMB)
|
||||
if has_colors()
|
||||
else curses.A_REVERSE
|
||||
)
|
||||
attr_track = (
|
||||
curses.color_pair(COLOR_SCROLLBAR_TRACK)
|
||||
if has_colors()
|
||||
else curses.A_NORMAL
|
||||
)
|
||||
|
||||
for win_y in range(h):
|
||||
self._win.chgat(
|
||||
win_y,
|
||||
w - 1,
|
||||
curses.A_REVERSE
|
||||
attr_thumb
|
||||
if thumb_y1 <= win_y <= thumb_y2
|
||||
else curses.A_NORMAL,
|
||||
else attr_track,
|
||||
)
|
||||
self._win.noutrefresh()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user