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 delete -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 list_view;
15pub mod multi_select;
16pub mod raw_mode;
17pub mod style;
18
19// Re-export for backwards-compatible call sites that use `crate::tui::arrow_select(...)`.
20pub use arrow_select::arrow_select;
21
22use std::io::IsTerminal;
23use std::sync::atomic::{AtomicBool, Ordering};
24
25/// Whether stdout is attached to a terminal. Commands should fall back to
26/// static rendering when this returns false (pipes, redirects, CI).
27pub fn stdout_is_tty() -> bool {
28    std::io::stdout().is_terminal()
29}
30
31// #20/#4: tracks whether a ratatui terminal is currently active. The panic hook
32// checks this flag so `ratatui::restore()` is only called when it matters —
33// a non-ratatui panic must not clobber terminal state it never set up.
34//
35// Single-thread invariant: only the main thread creates ratatui terminals
36// in this codebase. Relaxed ordering is sufficient. If callers ever cross
37// threads, upgrade to Acquire/Release.
38static RATATUI_ACTIVE: AtomicBool = AtomicBool::new(false);
39
40/// Mark that a ratatui terminal is now active.
41///
42/// # Safety contract
43/// Must be called only from `TerminalGuard::new`. Direct callers can corrupt
44/// the panic-hook contract.
45pub(crate) fn mark_ratatui_active() {
46    RATATUI_ACTIVE.store(true, Ordering::Relaxed);
47}
48
49/// Mark that the ratatui terminal has been released.
50///
51/// # Safety contract
52/// Must be called only from `TerminalGuard::Drop`. Direct callers can corrupt
53/// the panic-hook contract.
54pub(crate) fn mark_ratatui_inactive() {
55    RATATUI_ACTIVE.store(false, Ordering::Relaxed);
56}
57
58/// Install a panic hook that restores the terminal state before the default
59/// panic handler prints. Safe to call once at process start.
60///
61/// The hook is gated on `RATATUI_ACTIVE` so it only calls `ratatui::restore()`
62/// when a ratatui terminal is actually in use — avoiding spurious restores for
63/// non-TTY panics (pipes, redirects, CI). `TerminalGuard` in `display.rs`
64/// sets and clears this flag.
65///
66/// `default(info)` chains to the original hook, which prints the panic message
67/// and respects `RUST_BACKTRACE` — so backtrace behaviour is preserved.
68pub fn install_panic_hook() {
69    let default = std::panic::take_hook();
70    std::panic::set_hook(Box::new(move |info| {
71        if RATATUI_ACTIVE.load(Ordering::Relaxed) {
72            // #6: catch_unwind guards against a second panic inside restore().
73            let _ = std::panic::catch_unwind(|| {
74                ratatui::restore();
75            });
76        }
77        default(info);
78    }));
79}