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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! # AltuiInit
//!
//! `AltuiInit` is a small helper around a Crossterm-based terminal that provides
//! a safe and ergonomic way to:
//!
//! - initialize the terminal (raw mode, alternate screen, optional mouse capture)
//! - restore the original terminal state on exit
//! - recover the terminal state even if the application panics
//!
//! The goal of `AltuiInit` is **not** to hide Crossterm or `Terminal`, but to
//! eliminate repetitive and error-prone boilerplate code commonly found in
//! TUI applications.
//!
//! ## Basic usage
//!
//! ```rust,no_run
//! use altui_core::{AltuiInit, widgets::{Block, Borders}};
//!
//! fn main() -> std::io::Result<()> {
//! AltuiInit::new(true)?
//! .set_panic_hook()
//! .run(|terminal| {
//! terminal.draw(|f| {
//! let size = f.size();
//! let mut block = Block::default()
//! .title("Block")
//! .borders(Borders::ALL);
//! f.render_widget(&mut block, size);
//! })?;
//!
//! Ok(())
//! })
//! }
//! ```
//!
//! This example is functionally equivalent to a much more verbose setup using
//! raw Crossterm primitives (see below), but guarantees that the terminal will
//! be restored correctly even in the presence of errors or panics.
//!
//! ## What does `AltuiInit` replace?
//!
//! The example above replaces the following boilerplate code:
//!
//! ```rust,no_run
//! use std::{io, thread, time::Duration};
//! use altui_core::{
//! backend::CrosstermBackend,
//! widgets::{Widget, Block, Borders},
//! Terminal,
//! };
//! use crossterm::{
//! event::{DisableMouseCapture, EnableMouseCapture},
//! execute,
//! terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
//! };
//!
//! fn main() -> Result<(), io::Error> {
//! enable_raw_mode()?;
//! let mut stdout = io::stdout();
//! execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
//!
//! let backend = CrosstermBackend::new(stdout);
//! let mut terminal = Terminal::new(backend)?;
//!
//! terminal.draw(|f| {
//! let size = f.size();
//! let mut block = Block::default()
//! .title("Block")
//! .borders(Borders::ALL);
//! f.render_widget(&mut block, size);
//! })?;
//!
//! thread::sleep(Duration::from_millis(5000));
//!
//! disable_raw_mode()?;
//! execute!(
//! terminal.backend_mut(),
//! LeaveAlternateScreen,
//! DisableMouseCapture
//! )?;
//! terminal.show_cursor()?;
//!
//! Ok(())
//! }
//! ```
//!
//! `AltuiInit` encapsulates this setup and teardown logic in a single RAII type,
//! reducing the chance of leaving the terminal in a broken state.
//!
//! ## Panic handling
//!
//! Terminal-based applications modify global terminal state
//! (raw mode, alternate screen, cursor visibility, mouse capture).
//! If a panic occurs, this state must still be restored.
//!
//! `AltuiInit` addresses this problem on two levels:
//!
//! 1. **RAII cleanup**
//! The terminal state is restored in `Drop`, which guarantees cleanup
//! when execution leaves the `AltuiInit` scope normally or due to unwinding.
//!
//! 2. **Optional panic hook**
//! Calling [`AltuiInit::set_panic_hook`] installs a default panic hook that
//! performs a best-effort terminal reset before delegating to the original
//! panic handler.
//!
//! ```rust,ignore
//! use altui_core::AltuiInit;
//!
//! AltuiInit::new(true)?
//! .set_panic_hook()
//! .run(|terminal| {
//! /* application code */
//! Ok(())
//! });
//! ```
//!
//! This hook is global and will also trigger if a panic occurs in another
//! thread or outside the immediate control flow of `AltuiInit`.
use Stdout;
use crate::;
/// Installs the default panic hook used by [`AltuiInit::set_panic_hook`].
///
/// This hook performs a best-effort restoration of the terminal state
/// before delegating to the previously installed panic hook.
///
/// Specifically, it attempts to:
///
/// - disable raw mode
/// - leave the alternate screen
/// - disable mouse capture
///
/// The hook is **global** and applies to panics from all threads.
///
/// # Notes
///
/// - This function does **not** replace RAII-based cleanup.
/// It is intended as a safety net for panics occurring outside the normal
/// control flow (e.g. in background threads).
///
/// - Calling this function is optional. Advanced users may prefer to install
/// their own panic hook or handle panics manually.
///
/// - The hook does not suppress the panic; it only ensures terminal recovery.
///
/// # See also
///
/// - [`AltuiInit::set_panic_hook`]
/// Crossterm terminal initialization helper which restores the original
/// terminal state on drop.
///
/// `AltuiInit` provides a minimal RAII wrapper around a Crossterm-based
/// [`Terminal`]. It is designed to eliminate repetitive setup and teardown
/// code while keeping full control over rendering and event handling.
///
/// ## Responsibilities
///
/// - enable raw mode
/// - enter the alternate screen
/// - optionally enable mouse capture
/// - restore the terminal state on drop
///
/// ## What `AltuiInit` does *not* do
///
/// - manage an event loop
/// - handle input
/// - hide the underlying `Terminal` API
///
/// ## Full control
///
/// If you need full control over terminal initialization or teardown
/// (for example, skipping `LeaveAlternateScreen` or managing cursor state
/// manually), you can always bypass `AltuiInit` and use Crossterm directly.
/// `AltuiInit` is a convenience layer, not a restriction.