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