22glyph's foreground/background carries one pixel each, doubling vertical
33resolution relative to one-pixel-per-cell. Framed in a white border so the
44panel's edges are visible against an equally black terminal background,
5- with an optional stats line underneath.
5+ with a stats line and a static controls reference underneath.
66
77Each frame is composed entirely off-screen: one Text buffer, with adjacent
88same-color pixels merged into single runs rather than one styled character
2424Rgb = tuple [int , int , int ]
2525
2626_BORDER_STYLE = Style (color = "white" )
27- _STATS_STYLE = Style (color = "bright_black " )
27+ _STATS_STYLE = Style (color = "white " )
2828_TILE_BORDER_STYLE = Style (color = "grey50" )
29+ _HEADER_STYLE = Style (color = "white" , bold = True )
30+
31+ # Mirrors run_app._button_listener()'s own key handling — this is a
32+ # reference for a person watching the renderer, not something this
33+ # process itself listens for, so it has to be kept in sync by hand
34+ # rather than imported: the device process is a separate `matrixbox app`
35+ # invocation, very often in an entirely different terminal.
36+ _APP_CONTROLS = (
37+ ("n" , "toggle wifi on/off" ),
38+ ("s" , "short button press" ),
39+ ("l" , "long button press (usually exits the app)" ),
40+ ("r" , "reload (restarts)" ),
41+ ("+/-" , "refresh-fps" ),
42+ ("[/]" , "gamma" ),
43+ ("z" , "cycle panel size" ),
44+ )
2945
3046
3147class TerminalRenderer :
@@ -52,7 +68,13 @@ def toggle_tile_borders(self) -> bool:
5268
5369 return self .show_tile_borders
5470
55- def render (self , frame : Frame , stats : Stats | None ) -> None :
71+ def render (
72+ self ,
73+ frame : Frame ,
74+ stats : Stats | None ,
75+ * ,
76+ waiting_message : str = "waiting for a connected app..." ,
77+ ) -> None :
5678 text = Text ()
5779
5880 horizontal_border = "─" * frame .width
@@ -65,13 +87,26 @@ def render(self, frame: Frame, stats: Stats | None) -> None:
6587
6688 text .append ("└" + horizontal_border + "┘\n " , style = _BORDER_STYLE )
6789
68- # Padded to the box's own width: this is the last thing printed
69- # each frame, with no trailing newline, so a shorter line here
70- # than last frame's (app name changed length, fps gained a digit,
71- # the tile-border toggle flipped) would otherwise leave stray
72- # leftover characters on screen with nothing to overwrite them.
73- stats_line = _format_stats_line (stats , self .show_tile_borders )
74- text .append (stats_line .ljust (len (horizontal_border ) + 2 ), style = _STATS_STYLE )
90+ # Padded to the box's own width: content here (app name, fps
91+ # digits) varies in length frame to frame, and with no per-line
92+ # clear between redraws, a shorter line than last frame's would
93+ # otherwise leave stray leftover characters with nothing to
94+ # overwrite them.
95+ stats_line = _format_stats_line (stats , waiting_message )
96+ text .append (
97+ stats_line .ljust (len (horizontal_border ) + 2 ) + "\n " , style = _STATS_STYLE
98+ )
99+
100+ # Always rendered, not just printed once at startup: this is the
101+ # one screen region under this process's own full control every
102+ # frame, so it's the one place a controls reminder can stay
103+ # genuinely static instead of scrolling off under whatever this
104+ # window's arbitrary log/print output does next. Same reasoning
105+ # for including the *other* window's controls here too, not just
106+ # this window's own 't' — a person watching the panel shouldn't
107+ # have to go find that other terminal just to remember them.
108+ text .append ("\n " )
109+ text .append_text (_format_controls_block (self .show_tile_borders ))
75110
76111 self .console .control (Control .home ())
77112 self .console .print (text , end = "" )
@@ -136,15 +171,46 @@ def _rgb(rgb: Rgb) -> Color:
136171 return Color .from_rgb (r , g , b )
137172
138173
139- def _format_stats_line (stats : Stats | None , show_tile_borders : bool ) -> str :
140- borders = f"borders (t): { 'on' if show_tile_borders else 'off' } "
174+ def _format_stats_line (stats : Stats | None , waiting_message : str ) -> str :
141175 if stats is None :
142- return borders
176+ return waiting_message
143177
144178 cpu = f"{ stats .cpu_percent :.0f} %" if stats .cpu_percent is not None else "n/a"
145179 rss = f"{ stats .rss_kb / 1024 :.1f} MB" if stats .rss_kb is not None else "n/a"
146180
147- return (
148- f"app: { stats .app } fps: { stats .fps :.1f} cpu: { cpu } "
149- f"sim rss (peak): { rss } { borders } "
181+ return f"app: { stats .app } fps: { stats .fps :.1f} cpu: { cpu } sim rss (peak): { rss } "
182+
183+
184+ # Shared by both lists in _format_controls_block, so "t" and every app
185+ # control key line up in the same column instead of the single-entry
186+ # simulator list getting its own, different spacing.
187+ _KEY_WIDTH = max (len ("t" ), max (len (key ) for key , _ in _APP_CONTROLS ))
188+
189+
190+ def _format_controls_block (show_tile_borders : bool ) -> Text :
191+ text = Text ()
192+ text .append ("simulator controls (this window):\n " , style = _HEADER_STYLE )
193+ # Padded like the stats line above, for the same reason: "on" is
194+ # shorter than "off", and this is the one line in an otherwise fully
195+ # static block whose length can actually change between redraws.
196+ state = "on" if show_tile_borders else "off"
197+ border_line = f" { 't' .ljust (_KEY_WIDTH )} toggle borders [{ state } ]"
198+ text .append (
199+ border_line .ljust (len (f" { 't' .ljust (_KEY_WIDTH )} toggle borders [off]" ))
200+ + "\n " ,
201+ style = _STATS_STYLE ,
150202 )
203+
204+ text .append ("\n app controls (the `matrixbox app` window):\n " , style = _HEADER_STYLE )
205+ lines = [
206+ f" { key .ljust (_KEY_WIDTH )} { description } "
207+ for key , description in _APP_CONTROLS
208+ ]
209+ # No trailing newline on the last one: render() prints this whole
210+ # buffer with no newline of its own either, so the cursor is left at
211+ # the end of this text, matching every other status message that
212+ # follows it with its own leading "\n" rather than a blank one baked
213+ # in here.
214+ text .append ("\n " .join (lines ), style = _STATS_STYLE )
215+
216+ return text
0 commit comments