okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""The look of the launch screens: the monitor's colours, rules and key hints, and the one list widget they share. 2 3Colours are the monitor's own (strata_cli/monitor.py: background #0e1113, panel #161a1d, text #eef1f3, muted #8e979f, accent 4#34d399, warning yellow); the launch mock-up adds a selection tint (#1f2a27), a warning amber and a soft red. Everything is plain 5text in a column of lines, as the monitor's text views are: a heading in the accent colour, a dim rule, the content, a dim 6explanation line and a dim line of key hints. 7""" 8 9from __future__import annotations 10 11import os 12import sys 13from dataclasses import dataclass 14from typing import Any 15 16from rich .text import Text 17from textual .widgets import Static 18 19BG ,PANEL ,FG ,MUTE = "#0e1113" ,"#161a1d" ,"#eef1f3" ,"#8e979f" 20ACCENT ,WARN ,BAD ,SEL ,RULE = "#34d399" ,"#f5c56b" ,"#f28b82" ,"#1f2a27" ,"#3a4146" 21 22 23@dataclass( frozen = True ) 24class Palette : 25"""The colours of one theme. The launch screens are dark; the monitor's Connect view and its pop-ups follow the monitor's light theme.""" 26 27bg :str 28panel :str 29fg :str 30mute :str 31accent :str 32warn :str 33bad :str 34sel :str 35rule :str 36 37 38DARK = Palette (BG ,PANEL ,FG ,MUTE ,ACCENT ,WARN ,BAD ,SEL ,RULE ) 39# Light: every text colour reads at 4.5:1 or better on the white panel (tests/terminal/test_tuiqa_monitor.py computes it). 40LIGHT = Palette ("#f9fafb" ,"#ffffff" ,"#221e1f" ,"#5b5f66" ,"#0a6b4a" ,"#8a5a00" ,"#b01a2e" ,"#cfeadf" ,"#c9ced3" ) 41 42 43def unicode_ok ()-> bool : 44if os .environ .get ("TERM" )== "dumb" : 45return False 46from strata_cli import consoleinfo 47if consoleinfo .legacy_console ():# the classic Windows console's font has no arrows, check marks or block glyphs ... 48if os .environ .get ("STRATA_GLYPHS" ,"" ).strip ().lower ()in ("ascii" ,"legacy" ): 49return False 50if consoleinfo .classic_style ()["mode" ]!= "full" :# ... unless the window's font could be switched to Cascadia Mono 51return False 52enc = getattr (sys .stdout ,"encoding" ,None )or "utf-8" 53try : 54"›─↑↓•█░" .encode (enc ) 55return True 56except (LookupError ,UnicodeEncodeError ): 57return False 58 59 60class Glyphs : 61def __init__ (self ,unicode :bool | None = None ): 62u = unicode_ok ()if unicode is None else unicode 63self .unicode = u 64self .cursor = "›" if u else ">" # (U+203A is in Consolas too, but one rule for the whole screen is simpler) 65self .rule = "─" if u else "-" 66self .updown = "↑↓" if u else "Up/Down" 67self .dot = "·" if u else "-" 68self .full = "█" if u else "#" 69self .empty = "░" if u else "." 70self .tick = "✓" if u else "ok" 71self .mask = "•" if u else "*" 72self .arrow = "→" if u else "->" 73 74 75CSS = f""" 76Screen {{ background: { BG } ; color: { FG } ; }} 77#shell {{ width: 100%; height: 100%; background: { PANEL } ; padding: 0 1; }} 78#head {{ height: 1; }} 79#rule1, #rule2 {{ height: 1; color: { RULE } ; }} 80#body {{ height: 1fr; overflow-y: auto; }} 81#explain {{ height: auto; max-height: 4; color: { MUTE } ; }} 82#hints {{ height: auto; max-height: 3; color: { MUTE } ; }} 83#message {{ height: auto; max-height: 3; color: { MUTE } ; }} 84Menu {{ height: auto; max-height: 100%; }} 85.field {{ height: 3; border: solid { ACCENT } ; display: none; }} 86.field.slim {{ height: 1; border: none; padding: 0 1; background: { SEL } ; }} 87#cmdbox {{ height: 6; border-top: solid { RULE } ; scrollbar-size-vertical: 1; }} 88.field.open {{ display: block; }} 89Input {{ background: { PANEL } ; color: { FG } ; }} 90""" 91 92 93@dataclass 94class Cell : 95text :str 96style :str = "" 97 98 99@dataclass 100class Item : 101cells :list [Cell | str ] 102key :Any = None 103enabled :bool = True 104style :str = "" # whole-row style when not selected 105 106 107def cell (c :Cell | str )-> Cell : 108return c if isinstance (c ,Cell )else Cell (str (c )) 109 110 111class Menu (Static ): 112"""A column list with a cursor: `items` is a list of `Item`, `columns` a list of (title, width) (width 0 = the rest).""" 113 114DEFAULT_CSS = "Menu { height: auto; text-wrap: nowrap; text-overflow: clip; }" 115 116def __init__ (self ,columns :list [tuple [str ,int ]]| None = None ,* ,glyphs :Glyphs | None = None ,id :str | None = None , 117window :int = 0 ,palette :Palette | None = None )-> None : 118super ().__init__ ("" ,id = id ,markup = False ) 119self .palette = palette or DARK 120self .columns = columns or [] 121self .items :list [Item ]= [] 122self .index = 0 123self .top = 0 124self .window = window # 0 = show all rows 125self .g = glyphs or Glyphs () 126self .show_header = bool (self .columns and any (t for t ,_ in self .columns )) 127 128def set_items (self ,items :list [Item ],index :int | None = None )-> None : 129self .items = items 130if index is not None : 131self .index = index 132self .index = max (0 ,min (self .index ,len (items )- 1 ))if items else 0 133self ._refresh () 134 135@property 136def current (self )-> Item | None : 137return self .items [self .index ]if self .items else None 138 139def move (self ,step :int )-> None : 140if not self .items : 141return 142n = len (self .items ) 143i = self .index 144for _ in range (n ): 145i = (i + step )% n 146if self .items [i ].enabled or not any (it .enabled for it in self .items ): 147break 148self .index = i 149self ._refresh () 150 151def select (self ,key :Any )-> None : 152for i ,it in enumerate (self .items ): 153if it .key == key : 154self .index = i 155self ._refresh () 156 157def page (self ,step :int )-> None : 158self .index = max (0 ,min (len (self .items )- 1 ,self .index + step * max (1 , (self .window or 8 )- 1 ))) 159self ._refresh () 160 161def _refresh (self )-> None : 162win = self .window or len (self .items ) 163if self .index < self .top : 164self .top = self .index 165if self .index >= self .top + win : 166self .top = self .index - win + 1 167self .top = max (0 ,min (self .top ,max (0 ,len (self .items )- win ))) 168self .update (self .build ()) 169 170def build (self )-> Text : 171out = Text (no_wrap = True ,overflow = "crop" ) 172pad = " " 173widths = [w for _t ,w in self .columns ] 174if self .show_header : 175out .append (pad ) 176for (title ,w )in self .columns : 177out .append (title .ljust (w )if w else title ,style = "bold" ) 178out .append ("\n" ) 179win = self .window or len (self .items ) 180rows = self .items [self .top :self .top + win ] 181for n ,it in enumerate (rows ): 182selected = (self .top + n )== self .index 183line = Text (no_wrap = True ,overflow = "crop" ) 184line .append ((self .g .cursor + " " )if selected else pad ,style = f"bold { self . palette . accent } " if selected else "" ) 185for k ,c in enumerate (it .cells ): 186c = cell (c ) 187w = widths [k ]if k < len (widths )else 0 188txt = c .text .ljust (w )if w else c .text 189style = c .style if not selected else (c .style if c .style and not selected else "" ) 190if selected : 191style = f"bold { self . palette . accent } " if c .style in ("" ,None )else c .style 192elif not c .style and it .style : 193style = it .style 194if not it .enabled and not selected : 195style = self .palette .mute 196line .append (txt ,style = style ) 197if selected : 198line .stylize (f"on { self . palette . sel } " ) 199line .pad_right (max (0 , (self .size .width or 100 )- len (line .plain ))) 200line .stylize (f"on { self . palette . sel } " ) 201out .append_text (line ) 202if n < len (rows )- 1 : 203out .append ("\n" ) 204if self .window and len (self .items )> win : 205out .append (f"\n { self . top + 1 } - { min ( len ( self . items ), self . top + win ) } of { len ( self . items ) } " ,style = self .palette .mute ) 206return out 207 208def on_resize (self ,_event )-> None : 209self ._refresh () 210 211 212def heading (text :str )-> Text : 213return Text (text ,style = f"bold { ACCENT } " ) 214 215 216def muted (text :str )-> Text : 217return Text (text ,style = MUTE ) 218 219 220def warn (text :str )-> Text : 221return Text (text ,style = WARN ) 222 223 224def bad (text :str )-> Text : 225return Text (text ,style = BAD ) 226 227 228def bar (fraction :float ,width :int ,g :Glyphs )-> str : 229fraction = max (0.0 ,min (1.0 ,fraction )) 230full = int (round (fraction * width )) 231return g .full * full + g .empty * (width - full )