okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-tuiqa (95f380769) into f-split-int: TUI QA fixes (launch with flags, discovery, monitor menu, connect checks, layouts, no browser, warnings), picture reader CPU under 24 GB / GPU from 24 GBfd0e3766a

main
3.8 KiB90 linesraw
1"""The monitor's view tabs: ONE model for every view and every width.
2
3There are six views (Overview, Serving, Requests, Engine, Hardware, Logs) and, when `strata launch` opened the monitor, a seventh, Connect.
4Every screen draws the same tab line from here, so the selected tab is always marked (in square brackets, and in colour when there is
5colour), the hints say 1-6 or 1-7 to match, and a narrow terminal keeps the tab names: shorter names first, then only the tabs around the
6selected one with < and > for the ones out of sight.
7"""
8
9from __future__ import annotations
10
11from dataclasses import dataclass
12
13NAMES = ("Overview", "Serving", "Requests", "Engine", "Hardware", "Logs", "Connect")
14SHORT = ("Over", "Serve", "Reqs", "Engine", "GPU", "Logs", "Conn")
15
16
17def count(connect: bool) -> int:
18    return 7 if connect else 6
19
20
21def keys(connect: bool) -> str:
22    """The number keys that switch views: "1-6", or "1-7" with the Connect view."""
23    return "1-7" if connect else "1-6"
24
25
26def title(index: int) -> str:
27    return NAMES[max(0, min(index, len(NAMES) - 1))]
28
29
30@dataclass
31class Tabs:
32    pieces: list[tuple[str, bool]]      # (text, selected)
33    sep: str
34    left: bool = False                  # tabs hidden on the left
35    right: bool = False                 # tabs hidden on the right
36
37    def width(self) -> int:
38        return (2 if self.left else 0) + (2 if self.right else 0) + sum(len(t) for t, _ in self.pieces) + len(self.sep) * max(0, len(self.pieces) - 1)
39
40
41def _items(labels: tuple[str, ...], current: int, n: int) -> list[str]:
42    return [f"[{i + 1} {labels[i]}]" if i == current else f"{i + 1} {labels[i]}" for i in range(n)]
43
44
45def layout(current: int, width: int, connect: bool = False) -> Tabs:
46    """The tabs for a line `width` columns wide: all names, else short names, else only the tabs around the selected one."""
47    n = count(connect)
48    current = max(0, min(current, n - 1))
49    for labels, sep in ((NAMES, "  "), (SHORT, " ")):
50        tabs = Tabs([(t, i == current) for i, t in enumerate(_items(labels, current, n))], sep)
51        if tabs.width() <= width:
52            return tabs
53    items = _items(SHORT, current, n)
54    lo = hi = current
55    while True:
56        grew = False
57        for nxt, is_hi in ((hi + 1, True), (lo - 1, False)):
58            if not 0 <= nxt < n:
59                continue
60            new_lo, new_hi = (lo, nxt) if is_hi else (nxt, hi)
61            trial = Tabs([(items[i], i == current) for i in range(new_lo, new_hi + 1)], " ", new_lo > 0, new_hi < n - 1)
62            if trial.width() <= width:
63                lo, hi, grew = new_lo, new_hi, True
64        if not grew:
65            break
66    return Tabs([(items[i], i == current) for i in range(lo, hi + 1)], " ", lo > 0, hi < n - 1)
67
68
69def plain(current: int, width: int, connect: bool = False) -> str:
70    """The tab line as plain text, at most `width` columns."""
71    tabs = layout(current, width, connect)
72    line = ("< " if tabs.left else "") + tabs.sep.join(t for t, _ in tabs.pieces) + (" >" if tabs.right else "")
73    return line[:max(1, width)]
74
75
76def styled(current: int, width: int, connect: bool = False, *, accent: str = "", color: bool = True):
77    """The same line as rich Text: the selected tab in bold and the accent colour (just bold, in brackets, without colour).
78    (rich is imported here: the plain-text parts of this module also run in the thin stdlib-only monitor bundle.)"""
79    from rich.text import Text
80    tabs = layout(current, width, connect)
81    out = Text(no_wrap=True, overflow="crop")
82    if tabs.left:
83        out.append("< ", style="dim")
84    for i, (text, selected) in enumerate(tabs.pieces):
85        if i:
86            out.append(tabs.sep)
87        out.append(text, style=(f"bold {accent}" if color and accent else "bold") if selected else "")
88    if tabs.right:
89        out.append(" >", style="dim")
90    return out