diff --git a/architecture.md b/architecture.md index 949a06b..ce09f46 100644 --- a/architecture.md +++ b/architecture.md @@ -340,6 +340,11 @@ backend IDs/errors. The compact mixer status line distinguishes connected, connecting, missing, ambiguous, and failed devices, adding a connect/select action where the user can resolve the state. +Terminal restoration is guarded around all fallible client/run work after +terminal initialization. The restore action is injectable for failure-path +tests; client and transport diagnostics stay in the rendered status area rather +than being printed over the alternate screen. + Terminal colors come from semantic theme palettes rather than renderer-local literals. The built-ins are Default, Dracula, Nord, Gruvbox Dark, Catppuccin Mocha, and Solarized Dark. `tui --theme NAME` selects the initial palette and diff --git a/task.md b/task.md index 3e1f0d2..62d1ebb 100644 --- a/task.md +++ b/task.md @@ -280,10 +280,11 @@ Dependencies: M7, M4 for real use. ambiguity selection guidance, and transport errors in its compact status line. Keep this item open until editor-managed assignment persistence can report save/conflict failures from M9.3. -- [ ] **M8.3 Terminal lifecycle:** restoration on normal/error exit, nonblocking refresh, logging away from display. Tests: injected IO/client failure releases terminal guard; polling/refresh does not block server operation. - Blocking IPC now runs on a joined worker and a delayed request cannot block - redraw access; keep this item open for explicit terminal-restoration failure - injection and log routing. +- [x] **M8.3 Terminal lifecycle:** restoration on normal/error exit, nonblocking refresh, logging away from display. Tests: injected IO/client failure releases terminal guard; polling/refresh does not block server operation. + Blocking IPC runs on a joined worker and a delayed request cannot block redraw + access. The restoration guard is independently injected and proven to run + when client initialization returns an error; client/transport diagnostics are + rendered inside the TUI rather than printed into the active terminal. - [ ] **M8.4 Relative-group activation (reserved):** wire the prepared per-member maximum state into origin-aware runtime observations, lifecycle, snapshots, pickup, and editor help before allowing `volume_mode = "relative"` diff --git a/tui/src/lib.rs b/tui/src/lib.rs index ef65572..6c6d0b8 100644 --- a/tui/src/lib.rs +++ b/tui/src/lib.rs @@ -404,8 +404,9 @@ pub fn run_live_with_theme( ) -> io::Result<()> { let client = BufferedMixerClient::spawn(client)?; let mut terminal = ratatui::init(); - let _guard = RestoreTerminal; - LiveTui::with_theme(client, theme)?.run(&mut terminal) + with_terminal_restore(ratatui::restore, || { + LiveTui::with_theme(client, theme)?.run(&mut terminal) + }) } /// Retains the original preview example without opening audio or IPC. @@ -413,11 +414,21 @@ pub fn run() -> io::Result<()> { run_live(DemoClient::default()) } -struct RestoreTerminal; +fn with_terminal_restore( + restore: impl FnOnce(), + operation: impl FnOnce() -> io::Result, +) -> io::Result { + let _guard = RestoreTerminal(Some(restore)); + operation() +} -impl Drop for RestoreTerminal { +struct RestoreTerminal(Option); + +impl Drop for RestoreTerminal { fn drop(&mut self) { - ratatui::restore(); + if let Some(restore) = self.0.take() { + restore(); + } } } @@ -857,6 +868,44 @@ mod tests { assert!(app.exit); } + #[test] + fn terminal_restore_runs_when_client_initialization_fails() { + struct FailingClient; + + impl MixerClient for FailingClient { + fn snapshot(&mut self) -> io::Result { + Err(io::Error::other("injected client failure")) + } + + fn set_volume( + &mut self, + _id: MixerObjectId, + _gain: f64, + _expected_revision: u64, + ) -> io::Result { + unreachable!() + } + + fn toggle_mute( + &mut self, + _id: MixerObjectId, + _expected_revision: u64, + ) -> io::Result { + unreachable!() + } + } + + let restored = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let restore_flag = Arc::clone(&restored); + let result = with_terminal_restore( + move || restore_flag.store(true, std::sync::atomic::Ordering::Release), + || LiveTui::new(FailingClient).map(|_| ()), + ); + + assert_eq!(result.unwrap_err().to_string(), "injected client failure"); + assert!(restored.load(std::sync::atomic::Ordering::Acquire)); + } + #[test] fn theme_key_cycles_known_palettes_and_wraps() { let mut app = LiveTui::new(DemoClient::default()).unwrap();