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