Rebuild the interactive configurator on opentui behind a JSON bridge

The TUI is now a TypeScript/opentui app in tui/ rather than Bubble Tea.
opentui is a Zig core with TypeScript bindings and no Go bindings, so this half
of the tool can't live in the Go binary; it compiles with Bun into a sibling
executable (rtsp-streamer-tui) that `rtsp-streamer tui` execs.

Everything that isn't presentation stays in Go, reached over three JSON
commands. The configurator holds no credentials and never writes the config
itself:

  config export           the config, plus limits like max_tiles
  config apply (stdin)    merge cameras/layouts/active_layout, validate, save
                          atomically, reload the daemon
  discover --json         Protect discovery, writing nothing

Two properties of that split are deliberate:

- The controller password never crosses the bridge. It's json:"-" on the way
  out, and apply only merges the three keys the TUI edits, so it can't be
  clobbered on the way back in either.
- apply re-reads the file before merging, so an editor left open for an hour can
  no longer overwrite a `views import`, a `layout set`, or a hand edit made in
  the meantime.

Discovery is previewable as a result: `discover --json` writes nothing, the
merge happens in the TUI, and nothing reaches disk until you save. Only
--enable-rtsp has a side effect, and it's on the controller.

Config structs gain json tags mirroring their yaml ones so the config
round-trips through the bridge under the same key names it has on disk, and
maxGridDim moves to config.MaxGridDim so the CLI and both configurators enforce
one ceiling. The write path is byte-for-byte identical to `layout set`, checked
against a copy of a live config.

Visible change: the grid editor draws real bordered boxes, so a spanning tile is
one box instead of an origin cell plus "·" continuation marks, and the
header-offset arithmetic in mouse.go is gone — the framework hit-tests list
rows. Keybindings, the lipgloss palette and the screen flow are carried over
unchanged; S now saves from anywhere.

The Bubble Tea version stays as `tui --legacy`. It's compiled into the Go binary
and needs no Bun, and on a headless Pi the TUI is the only config UI there is,
so a fallback is worth its weight. The cost of the new one is size: ~120 MB
against ~13 MB, since Bun embeds its runtime and opentui's native library.

Tests: 67 bun tests drive the real (in-memory) opentui renderer, including mouse
click and drag, plus tsc --noEmit. `make test-tui` runs both, and
scripts/preview.ts dumps every screen as text without needing a terminal.

Three bugs found during the port are documented in tui/README.md, since none are
apparent from the code: overlapping cell borders render as ┌ where a lattice
needs ┬; a drag dies after the first resize if the tree is rebuilt, because the
renderer captures the press-target renderable; and a rebuilt box has no computed
layout until the next frame, so its screenX reads 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Levi Woodard
2026-07-29 12:22:48 -06:00
parent 81193f524c
commit 1ea3a5ac0a
28 changed files with 3267 additions and 83 deletions

View File

@@ -59,10 +59,16 @@ func daemonCmd() *cobra.Command {
// discoverCmd queries UniFi Protect and merges cameras into the config.
func discoverCmd() *cobra.Command {
var enableRTSP string
var asJSON bool
c := &cobra.Command{
Use: "discover",
Short: "Discover cameras from UniFi Protect and update the config",
RunE: func(cmd *cobra.Command, _ []string) error {
// --json reports what discovery found and writes nothing, so the
// opentui configurator can preview the merge before saving.
if asJSON {
return discoverJSON(enableRTSP)
}
cfg, err := config.Load(cfgPath)
if err != nil {
return err
@@ -148,6 +154,7 @@ func discoverCmd() *cobra.Command {
},
}
c.Flags().StringVar(&enableRTSP, "enable-rtsp", "", "comma-separated channels to enable in Protect and record, e.g. high,low")
c.Flags().BoolVar(&asJSON, "json", false, "print results as JSON without saving the config")
return c
}
@@ -465,16 +472,37 @@ func statusCmd() *cobra.Command {
}
}
// tuiCmd launches the interactive configurator.
// tuiCmd launches the interactive configurator: the opentui one by default,
// or the previous Bubble Tea implementation with --legacy. The legacy path is
// kept because on a headless Pi the TUI is the only config UI there is, and it
// needs no Bun runtime — useful if the compiled TUI is missing or misbehaves.
func tuiCmd() *cobra.Command {
return &cobra.Command{
var legacy bool
c := &cobra.Command{
Use: "tui",
Aliases: []string{"config-ui"},
Short: "Interactive configurator (great over SSH)",
Long: `Interactive configurator: browse cameras, place them into layout slots, pick
the active layout, discover cameras from UniFi Protect, and save — which
signals a running daemon to reload.
This runs the opentui configurator, a separate binary (rtsp-streamer-tui) built
with Bun because opentui is a TypeScript library. It is looked for at
$RTSP_STREAMER_TUI, then next to this executable, then on $PATH. Build it with
'make tui' and install it with 'make install'.
--legacy runs the previous Bubble Tea configurator instead. It is compiled into
this binary, so it needs no Bun runtime and always works — useful if the
configurator is missing or misbehaving.`,
RunE: func(cmd *cobra.Command, _ []string) error {
return tui.Run(cfgPath)
if legacy {
return tui.Run(cfgPath)
}
return runTUI(cmd.Context())
},
}
c.Flags().BoolVar(&legacy, "legacy", false, "use the previous Bubble Tea configurator")
return c
}
// configCmd holds config-file utilities.
@@ -514,7 +542,7 @@ func configCmd() *cobra.Command {
},
}
c.AddCommand(initCmd, pathCmd, editCmd)
c.AddCommand(initCmd, pathCmd, editCmd, configExportCmd(), configApplyCmd())
return c
}