okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd0e3766a
main
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 : 18return 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.""" 23return "1-7" if connect else "1-6" 24 25 26def title (index :int )-> str : 27return NAMES [max (0 ,min (index ,len (NAMES )- 1 ))] 28 29 30@dataclass 31class Tabs : 32pieces :list [tuple [str ,bool ]]# (text, selected) 33sep :str 34left :bool = False # tabs hidden on the left 35right :bool = False # tabs hidden on the right 36 37def width (self )-> int : 38return (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 ]: 42return [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.""" 47n = count (connect ) 48current = max (0 ,min (current ,n - 1 )) 49for labels ,sep in ((NAMES ," " ), (SHORT ," " )): 50tabs = Tabs ([(t ,i == current )for i ,t in enumerate (_items (labels ,current ,n ))],sep ) 51if tabs .width ()<= width : 52return tabs 53items = _items (SHORT ,current ,n ) 54lo = hi = current 55while True : 56grew = False 57for nxt ,is_hi in ((hi + 1 ,True ), (lo - 1 ,False )): 58if not 0 <= nxt < n : 59continue 60new_lo ,new_hi = (lo ,nxt )if is_hi else (nxt ,hi ) 61trial = Tabs ([(items [i ],i == current )for i in range (new_lo ,new_hi + 1 )]," " ,new_lo > 0 ,new_hi < n - 1 ) 62if trial .width ()<= width : 63lo ,hi ,grew = new_lo ,new_hi ,True 64if not grew : 65break 66return 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.""" 71tabs = layout (current ,width ,connect ) 72line = ("< " if tabs .left else "" )+ tabs .sep .join (t for t ,_ in tabs .pieces )+ (" >" if tabs .right else "" ) 73return 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.)""" 79from rich .text import Text 80tabs = layout (current ,width ,connect ) 81out = Text (no_wrap = True ,overflow = "crop" ) 82if tabs .left : 83out .append ("< " ,style = "dim" ) 84for i , (text ,selected )in enumerate (tabs .pieces ): 85if i : 86out .append (tabs .sep ) 87out .append (text ,style = (f"bold { accent } " if color and accent else "bold" )if selected else "" ) 88if tabs .right : 89out .append (" >" ,style = "dim" ) 90return out