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
use Infallible;
use ;
use crate*;
/// Short-hand for the type of error that can occur in a [`State`].
///
/// This is parameterised over the state `S` and the value type `T` (corresponding to the `Ok` type of a
/// result).
type Error<S, T> = Error;
/// Dictates when and what to return from a running [`State`].
/// Defines the event loop of an application state.
///
///
/// # Usage
///
/// For most applications, implementing [`State::draw`] and [`State::input`] will suffice.
/// - [`State::draw`] draws the user interface using [Ratatui](ratatui).
/// - [`State::input`] handles key press events.
///
/// Afterward, [`State::run`] may be called to enter the event loop.
///
/// If [events](Event) other than key press events are required in an application, it may implement
/// [`State::event`], which handles any and all events read from the backend. Its default implementation
/// simply delegates key press events to [`State::input`] and discards the rest.
///
/// The interface provided by [`State::run`] is fairly low-level. In most cases, a wrapper function should be
/// used to provide a more bespoke interface.
///
///
/// # Error Handling
///
/// Arbitrary application-defined errors are supported through the [`State::Result`] type. Errors can be
/// returned from [`State::input`] or [`State::event`], and are propogated through [`State::run`].
///
/// Requiring a result type as opposed to an error type (which is generally standard practice) allows states
/// to accept types that aren't results, but can *behave* like results. Most prominently: `Option<T>` and
/// `T` itself. The latter case is especially interesting since it allows states for which no error can occur
/// to be implemented without any mention of [`Result`] or [`Infallible`] trickery --- all return values are
/// implicitly `Ok`.
///
///
/// # Signals
///
/// The event handler [`State::event`] (and [`State::input`] by extension) communicates when and what to
/// return from [`State::run`] using [`Signal`]. A value of [`Signal::Continue`] indicates that the state
/// should continue running, whereas [`Signal::Return`] indicates that the state should stop running, and
/// contains the value that should be returned.
///
/// The return value can be whatever makes sense for the state, and the type of the value is defined by
/// [`State::Out`].
///
/// To allow the return value to be moved from the state (e.g., when the return value is a field of the state
/// struct), [`State::event`] consumes `self`. The consumed `self` is then yielded back to [`State::run`] via
/// [`Signal::Continue`], representing the "continuation" of the state.
///
///
/// # Dummy state
///
/// A dummy (or no-nop) state is implemented through `()`. This is useful when a state is expected but not
/// used; e.g. to display a [`dialog`] without a background.
///
/// The dummy state draws nothing and exits as soon as a key is pressed.
///
///
/// # Examples
///
/// A state with a tally that increases when the user presses `up`:
///
/// ```no_run
/// use ratatui::widgets::Paragraph;
/// use tundra::prelude::*;
///
/// struct Tally {
/// value: u32,
/// }
///
/// impl State for Tally {
/// type Result<T> = T;
/// type Out = u32;
/// type Global = ();
///
/// fn draw(&self, frame: &mut Frame) {
/// let widget = Paragraph::new(self.value.to_string());
/// frame.render_widget(widget, frame.size());
/// }
///
/// fn input(mut self, key: KeyEvent, ctx: &mut Context) -> Signal<Self> {
/// match key.code {
/// KeyCode::Up => self.value += 1,
/// KeyCode::Tab => self.value *= tally(ctx),
/// KeyCode::Enter => return Signal::Return(self.value),
/// _ => (),
/// }
/// Signal::Continue(self)
/// }
/// }
///
/// // a wrapper for the state that constructs the tally and runs it -- a recommended pattern!
/// pub fn tally(ctx: &mut Context) -> u32 {
/// Tally{ value: 0 }.run(ctx)
/// }
/// ```
/// Implements a dummy (or no-op) [`State`] through `()`. It draws nothing and exits as soon as a key is
/// pressed.
///
/// This is useful when a state is expected but not used; e.g. if you want to display a [`dialog`] without a
/// background.
/// Generalisation over data-carrying [`Result`]-like types.
///
/// There are three significant implementors of this trait:
/// - `Result<T, E>` itself, which has error type `E`.
/// - `Option<T>`, which has error type `()`.
/// - `T`, which has error type [`Infallible`] (or `!` once stabilised).
///
/// Either three of these can be used in place of an explicit [`Result`] where a [`ResultLike`] type is
/// expected. This allows [`State`] to accept not only any error type (through `Result<T, E>`), but also the
/// absence of an error type (through `Option<T>`), and the absence of an error altogether (through `T`).
///
///
/// # Limitations
///
/// There are limitations to this approach. Namely, it is very difficult to assert that [`State::Result`] has
/// the same error type regardless of its value type `T` (as is true for all three implementors listed above)
/// This means that to propogate an error from `State::Result<T>` to `State::Result<U>`, an explicit bound to
/// assert that the conversion between the two (ostensibly distinct) error types exists must be added. This
/// is cumbersome for generic code (like the default implementation of [`State::run`]), but has no bearing on
/// the concrete implementations of the states themselves.