Skip to main content

git_worktree_manager/tui/
mod.rs

1//! TUI rendering layer built on ratatui + crossterm.
2//!
3//! Houses:
4//! - `arrow_select`: raw-mode arrow-key single-select
5//! - `multi_select`: raw-mode arrow + space multi-select (for `gw rm -i`)
6//! - `raw_mode`:    RAII `RawModeGuard` for termios + cursor state (Unix-only)
7//! - `list_view`:   Inline Viewport renderer for `gw list`
8//! - `style`:       shared ratatui `Style` palette mirroring `crate::console`
9//!
10//! Simple commands with pure text output continue to use `crate::console`.
11//! ratatui is reserved for commands that need declarative/progressive rendering.
12
13pub mod arrow_select;
14pub mod config_editor;
15pub mod list_view;
16pub mod multi_select;
17pub mod raw_mode;
18pub mod style;
19
20// Re-export for backwards-compatible call sites that use `crate::tui::arrow_select(...)`.
21pub use arrow_select::arrow_select;
22
23use std::io::IsTerminal;
24use std::sync::atomic::{AtomicBool, Ordering};
25
26/// Whether stdout is attached to a terminal. Commands should fall back to
27/// static rendering when this returns false (pipes, redirects, CI).
28pub fn stdout_is_tty() -> bool {
29    std::io::stdout().is_terminal()
30}
31
32// #20/#4: tracks whether a ratatui terminal is currently active. The panic hook
33// checks this flag so `ratatui::restore()` is only called when it matters —
34// a non-ratatui panic must not clobber terminal state it never set up.
35//
36// Single-thread invariant: only the main thread creates ratatui terminals
37// in this codebase. Relaxed ordering is sufficient. If callers ever cross
38// threads, upgrade to Acquire/Release.
39static RATATUI_ACTIVE: AtomicBool = AtomicBool::new(false);
40
41/// Mark that a ratatui terminal is now active.
42///
43/// # Safety contract
44/// Callers must pair every `mark_ratatui_active` with a `mark_ratatui_inactive`
45/// on all exit paths (Ok / Err / panic). The canonical wrapper is
46/// `display.rs::TerminalGuard`, which uses Drop to guarantee the pairing. The
47/// fullscreen-alt-screen editor in `tui::config_editor` calls these directly
48/// because its lifecycle differs (alt-screen + raw mode rather than inline
49/// viewport), and it accepts the burden of manual cleanup on every arm,
50/// including the `Terminal::new` failure path. New callers should prefer
51/// `TerminalGuard` unless they have the same justification.
52pub(crate) fn mark_ratatui_active() {
53    RATATUI_ACTIVE.store(true, Ordering::Relaxed);
54}
55
56/// Mark that the ratatui terminal has been released.
57///
58/// # Safety contract
59/// See [`mark_ratatui_active`]. Pair every `mark_ratatui_active` with exactly
60/// one `mark_ratatui_inactive`, including on error and panic paths. Skipping
61/// the clear leaves the panic hook armed to call `ratatui::restore()` on the
62/// next unrelated panic, which can scribble on a healthy terminal.
63pub(crate) fn mark_ratatui_inactive() {
64    RATATUI_ACTIVE.store(false, Ordering::Relaxed);
65}
66
67/// Install a panic hook that restores the terminal state before the default
68/// panic handler prints. Safe to call once at process start.
69///
70/// The hook is gated on `RATATUI_ACTIVE` so it only calls `ratatui::restore()`
71/// when a ratatui terminal is actually in use — avoiding spurious restores for
72/// non-TTY panics (pipes, redirects, CI). `TerminalGuard` in `display.rs`
73/// sets and clears this flag.
74///
75/// `default(info)` chains to the original hook, which prints the panic message
76/// and respects `RUST_BACKTRACE` — so backtrace behaviour is preserved.
77pub fn install_panic_hook() {
78    let default = std::panic::take_hook();
79    std::panic::set_hook(Box::new(move |info| {
80        if RATATUI_ACTIVE.load(Ordering::Relaxed) {
81            // #6: catch_unwind guards against a second panic inside restore().
82            let _ = std::panic::catch_unwind(|| {
83                ratatui::restore();
84            });
85        }
86        default(info);
87    }));
88}