Skip to content

Commit 214ca1a

Browse files
committed
fix: update legend for controls
1 parent 92c94dc commit 214ca1a

3 files changed

Lines changed: 92 additions & 37 deletions

File tree

‎matrixbox_simulator/device/run_app.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,6 @@ def restart_process(reason: str = "to apply the new panel geometry") -> NoReturn
499499
)
500500

501501

502-
def _controls_hint() -> str:
503-
return (
504-
"'s'/'l' button, '+'/'-' refresh-fps, '['/']' gamma, 'z' cycle size, "
505-
"'n' toggle wifi, 'r' reload (restarts)"
506-
)
507-
508-
509502
def _cycle_size(settings_path: Path) -> NoReturn:
510503
# Matches by width/height rather than a saved "size" name, since real
511504
# settings have no such field either — just width/height/tiles. An
@@ -764,7 +757,6 @@ def listen() -> None:
764757

765758
thread = threading.Thread(target=listen, daemon=True)
766759
thread.start()
767-
print(f"matrixbox-simulator: controls: {_controls_hint()}")
768760

769761
try:
770762
yield

‎matrixbox_simulator/term/render.py‎

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
glyph's foreground/background carries one pixel each, doubling vertical
33
resolution relative to one-pixel-per-cell. Framed in a white border so the
44
panel'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
77
Each frame is composed entirely off-screen: one Text buffer, with adjacent
88
same-color pixels merged into single runs rather than one styled character
@@ -24,8 +24,24 @@
2424
Rgb = 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

3147
class 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("\napp 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

‎matrixbox_simulator/term/run_simulator.py‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,13 @@ def run_connected(
210210
) -> None:
211211
# Shown before any real frame has arrived, including while there's no
212212
# sim running at all yet, so the panel's bounds are always visible
213-
# rather than depending on server round-trip timing.
213+
# rather than depending on server round-trip timing. The stats line's
214+
# own "waiting for frame server..." text covers the same ground a
215+
# separate stderr print used to, so there's only ever one such
216+
# message on screen instead of both stacked underneath each other.
214217
placeholder = Frame.blank(width, height, tiles=tiles)
215-
renderer.render(placeholder, None)
218+
waiting_message = f"waiting for frame server at {url}..."
219+
renderer.render(placeholder, None, waiting_message=waiting_message)
216220

217221
while is_running():
218222
source = connect_with_retry(url, is_running)
@@ -247,25 +251,18 @@ def run_connected(
247251

248252
if is_running():
249253
renderer.clear()
250-
renderer.render(placeholder, None)
254+
renderer.render(placeholder, None, waiting_message=waiting_message)
251255

252256

253257
def connect_with_retry(url: str, is_running: Callable[[], bool]) -> WsSource | None:
254258
"""Retries WsSource(url) until it succeeds or is_running() goes false.
255-
Covers both a server that hasn't started yet and one that restarted."""
256-
announced = False
257-
259+
Covers both a server that hasn't started yet and one that restarted.
260+
No status print of its own: the placeholder box's own stats line
261+
already says as much for as long as this is retrying."""
258262
while is_running():
259263
try:
260264
return WsSource(url)
261265
except Exception:
262-
if not announced:
263-
print(
264-
f"\nmatrixbox-simulator: waiting for frame server at {url}...",
265-
file=sys.stderr,
266-
)
267-
announced = True
268-
269266
time.sleep(RECONNECT_DELAY_SECONDS)
270267

271268
return None

0 commit comments

Comments
 (0)