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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use ;
use crateState;
use Wrapper;
pub type Backend = CrosstermBackend;
pub type Terminal = Terminal;
/// Stores the [`Terminal`] and represents the terminal environment as a whole.
/// Manages the terminal environment.
///
/// Serves as a wrapper around [Ratatui's terminal](ratatui::Terminal) with added RAII to automatically
/// initialise and reset the terminal environment. The initialisation of the terminal environment consists
/// of:
/// - Installing a panic handler to make sure the terminal environment is reset before the program exits and
/// a panic message is printed.
/// - Enabling raw mode.
/// - Hiding the cursor.
/// - Entering an alternate terminal buffer.
///
///
/// # Basic usage
///
/// Construct the context using [`Context::new`] and give a mutable reference to it when running states with
/// [`State::run`].
///
///
/// # Application-defined global
///
/// In addition to managing the terminal environment, the context also provides the utility of a global
/// value, which can be whatever makes sense in the application. Suitable examples include configuration
/// data or user information. The global will then be available via the [`global`](Context::global) field of
/// the context for all states ran with it.
///
/// Note that this is purely opt-in; for applications where no global data is necessary, `()` may be used,
/// which is the default.
///
/// To use a context global, construct the context using [`Context::with_global`] and set the
/// [`Global`](crate::State::Global) type of all states ran with the context equal to the type of the global.
///
///
/// # Chaining with new globals
///
/// Though globals should generally persist across an entire application, there is support for creating a
/// "new" context with a new global value, while reusing the same internal [`Terminal`] handle. This is
/// achieved through _chaining_ using [`Context::chain_with_global`] or [`Context::chain_without_global`].
///
/// Chaining may be useful where there are distinct clusters of states in an application, with each cluster
/// having its own associated global.
///
/// ⚠️ Creating several context instances using [`Context::new`] or [`Context::with_global`] should generally
/// be avoided.
///
///
/// # Custom panic handler
///
/// The installed panic handler will delegate to the previous one after resetting the terminal. If a custom
/// panic handler is used in the application, it should be installed *before* creating the context to ensure
/// compatability.
///
///
/// # Unmanaged terminal environment
///
/// The automatic initialisation and resetting of the terminal environment can be opted out from by using
/// [`Context::new_unmanaged`] or [`Context::with_global_unmanaged`] to construct the context. Note that in
/// these cases, the [`Terminal`] instance must be constructed manually by application code. See
/// [Ratatui's documentation](ratatui) on how to do this.
///
///
/// # Examples
///
/// Creating a context without global data and using it to run a [`State`]:
/// ```no_run
/// # use tundra::prelude::*;
/// let mut ctx = Context::new()?;
/// # let some_state = ();
/// // let some_state: impl State<Global = ()>
/// some_state.run(&mut ctx)
/// # ; Ok::<(), std::io::Error>(())
/// ```
///
/// Creating a context with global user data:
/// ```no_run
/// # use tundra::prelude::*;
/// struct User {
/// name: String,
/// id: u32,
/// }
///
/// let user = User {
/// name: "Don Hertzfeldt".into(),
/// id: 2012,
/// };
/// let mut ctx = Context::with_global(user)?;
///
/// // the global can then be retrieved as:
/// let user: &User = &ctx.global;
///
/// // and the context can be used to run states that have `State::Global = User`.
/// # struct SomeState;
/// # impl SomeState{ fn run<T>(self, _: T) {} }
/// # let some_state = SomeState;
/// // let some_state: impl State<Global = User>
/// some_state.run(&mut ctx)
/// # ; Ok::<(), std::io::Error>(())
/// ```
///
/// "Removing" the global from an existing context:
/// ```no_run
/// # use std::path::PathBuf;
/// # use tundra::prelude::*;
/// let cache_dir = "~/.cache/svalbard/".into();
///
/// let mut old: Context<PathBuf> = Context::with_global(cache_dir)?;
/// let new: Context = old.chain_without_global();
///
/// // old context is still available!
/// # struct SomeState;
/// # impl SomeState{ fn run<T>(self, _: T) {} }
/// # let some_state = SomeState;
/// // let some_state: impl State<Global = PathBuf>
/// some_state.run(&mut old)
/// # ; Ok::<(), std::io::Error>(())
/// ```
///
/// Constructing a context without automatic management of the terminal environment (this requires adding
/// [`crossterm`] to the application's dependencies):
/// ```no_run
/// use std::io;
/// use crossterm::{
/// terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
/// cursor::{Hide, Show},
/// };
/// use ratatui::prelude::*;
/// use tundra::{Terminal, Backend};
/// # use tundra::prelude::*;
///
/// // construct and initialize terminal
/// let backend = Backend::new(io::stdout());
/// let terminal = Terminal::new(backend)?;
/// terminal::enable_raw_mode()?;
/// crossterm::execute!(io::stdout(), Hide, EnterAlternateScreen)?;
///
/// // construct context and run some state with it
/// let mut ctx = Context::new_unmanaged(terminal);
/// # let some_state = ();
/// // let some_state: impl State<Global = ()>
/// some_state.run(&mut ctx);
///
/// // reset terminal
/// terminal::disable_raw_mode();
/// crossterm::execute!(io::stdout(), Show, LeaveAlternateScreen);
/// # Ok::<(), std::io::Error>(())
/// ```