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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//! Where a program reads input and writes frames: the real terminal, or a
//! scripted one for tests.
use crate::color::Rgb;
use crate::input::{ESC_TIMEOUT, Event, Reader};
use crate::key::Key;
use crate::mode::{KeyboardEnhancementsReport, ModeSetting, ReportedMode, TerminalMode};
use crate::mouse::Mouse;
use crate::msg::{ClipboardSelection, ColorScheme, Size};
use std::io::{self, Write};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};
/// What a backend reports to the running program: what the terminal
/// sent, decoded, and signals. Each terminal variant becomes the
/// [`Msg`](crate::Msg) of the same name.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Input {
/// A key was pressed.
KeyPress(Key),
/// A key was released.
KeyRelease(Key),
/// A mouse button was pressed.
MouseClick(Mouse),
/// A mouse button was released.
MouseRelease(Mouse),
/// The mouse wheel turned.
MouseWheel(Mouse),
/// The mouse moved.
MouseMotion(Mouse),
/// Text was pasted.
Paste(String),
/// A paste begins.
PasteStart,
/// A paste ended.
PasteEnd,
/// The terminal gained focus.
Focus,
/// The terminal lost focus.
Blur,
/// The cursor's position, from 0.
CursorPosition {
/// The column.
x: i32,
/// The row.
y: i32,
},
/// The terminal's keyboard enhancements.
KeyboardEnhancements(KeyboardEnhancementsReport),
/// The terminal's reply about a mode.
ModeReport {
/// The mode.
mode: ReportedMode,
/// Its setting.
value: ModeSetting,
},
/// The terminal's name and version.
TerminalVersion(String),
/// The terminal's foreground colour.
ForegroundColor(Rgb),
/// The terminal's background colour.
BackgroundColor(Rgb),
/// The terminal's cursor colour.
CursorColor(Rgb),
/// The window changed to this size, or the terminal reported it.
Resize(Size),
/// The window's size in pixels.
PixelSize(Size),
/// The size of one cell in pixels.
CellSize(Size),
/// The clipboard's content (OSC 52).
Clipboard {
/// The text.
content: String,
/// Which clipboard.
selection: ClipboardSelection,
},
/// A capability, as `name=value` or a name alone (XTGETTCAP).
Capability(String),
/// The terminal's primary device attributes (DA1).
PrimaryDeviceAttributes(Vec<i64>),
/// The terminal's secondary device attributes (DA2).
SecondaryDeviceAttributes(Vec<i64>),
/// The terminal's unit id (DA3).
TertiaryDeviceAttributes(String),
/// The terminal's colour scheme.
ColorScheme(ColorScheme),
/// `SIGINT`: stop with [`Error::Interrupted`](crate::Error::Interrupted).
Interrupt,
/// `SIGTERM`: stop as if the model had quit.
Terminate,
/// `SIGTSTP` (`kill -TSTP`, or Ctrl-Z while another program has the
/// terminal): suspend the program, as [`suspend`](crate::suspend)
/// does.
Suspend,
/// `SIGCONT` without a suspend of the program's own: it was stopped
/// from outside, so the terminal is set up and drawn again.
Continue,
}
/// Delivers input to the running program; `false` once it has stopped.
pub type InputSink = Box<dyn Fn(Input) -> bool + Send + 'static>;
/// A terminal a [`Program`](crate::Program) can run in.
pub trait Backend {
/// Takes over the terminal (raw mode, hidden cursor). The program
/// switches to the alternate screen itself, when a view asks.
///
/// # Errors
///
/// If the terminal cannot be set up.
fn enter(&mut self) -> io::Result<()>;
/// Stops input and gives the screen back as it was.
///
/// # Errors
///
/// If the terminal cannot be restored.
fn leave(&mut self) -> io::Result<()>;
/// The window's size.
///
/// # Errors
///
/// If the size cannot be read.
fn size(&mut self) -> io::Result<Size>;
/// Starts delivering input to `sink`, usually from a thread, until
/// [`Backend::leave`].
fn start_input(&mut self, sink: InputSink);
/// Where frames are written.
fn output(&mut self) -> &mut dyn Write;
/// Turns a terminal mode on or off. By default this writes the
/// mode's [`sequence`](TerminalMode::sequence) to
/// [`output`](Backend::output); the real terminal also records it,
/// so it is turned off however the program ends.
///
/// # Errors
///
/// If the sequence cannot be written.
fn set_mode(&mut self, mode: TerminalMode, on: bool) -> io::Result<()> {
self.output().write_all(&mode.sequence(on))
}
/// Whether this is a real terminal. A program then asks it, when it
/// starts, whether it has synchronized output (mode 2026) and
/// grapheme widths (mode 2027), unless Bubble Tea would not (over
/// SSH, or in Apple's Terminal); detects its colour profile from the
/// environment; and before handing it back, reads the replies to
/// queries still on their way. No by default: a backend that is not
/// a terminal shows every colour as it is, unless
/// [`Program::with_color_profile`](crate::Program::with_color_profile)
/// says otherwise.
fn is_terminal(&self) -> bool {
false
}
/// Hands the terminal to another program, for
/// [`exec`](crate::exec), [`suspend`](crate::suspend) and
/// [`release_terminal`](crate::release_terminal): stops reading
/// input (bytes not yet read stay for that program), shows the
/// cursor and puts the terminal's own mode back. The runtime has
/// already turned its modes off. Nothing to do by default.
///
/// # Errors
///
/// If the terminal cannot be handed over.
fn release(&mut self) -> io::Result<()> {
Ok(())
}
/// Takes the terminal back after [`Backend::release`]: raw mode and
/// the hidden cursor again, and input read again. The runtime then
/// turns its modes on again and draws the whole frame. Nothing to do
/// by default.
///
/// # Errors
///
/// If the terminal cannot be set up again.
fn restore(&mut self) -> io::Result<()> {
Ok(())
}
/// Stops the process, as Ctrl-Z in a shell does, and returns once it
/// is continued; called between [`Backend::release`] and
/// [`Backend::restore`]. Nothing to do by default.
///
/// # Errors
///
/// If the process cannot be stopped.
fn suspend(&mut self) -> io::Result<()> {
Ok(())
}
}
/// A scripted terminal for testing models: a fixed size, input sent in
/// order, and everything written captured. Input is either decoded
/// already ([`TestBackend::keys`], [`TestBackend::inputs`]) or raw bytes
/// that go through the same decoder as a real terminal's
/// ([`TestBackend::bytes`]).
///
/// ```
/// use petitcarnets::testing::TestBackend;
/// use petitcarnets::Key;
///
/// let backend = TestBackend::new(40, 10)
/// .bytes(b"\x1b[A")
/// .keys([Key::char('q')]);
/// let screen = backend.screen();
/// // ... run a Program with `backend`, then read `screen.text()`.
/// # let _ = screen;
/// ```
#[derive(Debug, Clone)]
pub struct TestBackend {
size: Size,
steps: Vec<Step>,
gap: Duration,
screen: Screen,
terminal: bool,
}
/// One scripted step.
#[derive(Debug, Clone)]
enum Step {
Input(Input),
Bytes(Vec<u8>),
}
/// What a [`TestBackend`] captured; it stays readable after the run.
#[derive(Debug, Clone, Default)]
pub struct Screen(Arc<Mutex<Vec<u8>>>);
impl Screen {
/// Everything written, as text, escape sequences included.
#[must_use]
pub fn raw(&self) -> String {
String::from_utf8_lossy(&self.0.lock().unwrap_or_else(PoisonError::into_inner)).into_owned()
}
/// Everything written, without escape sequences.
#[must_use]
pub fn text(&self) -> String {
crate::style::strip_ansi(&self.raw())
}
}
impl Write for Screen {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl TestBackend {
/// A `columns` by `rows` terminal with no input.
#[must_use]
pub fn new(columns: u16, rows: u16) -> Self {
Self {
size: Size::new(columns, rows),
steps: Vec::new(),
gap: Duration::from_millis(5),
screen: Screen::default(),
terminal: false,
}
}
/// Acts as a real terminal ([`Backend::is_terminal`]): the program
/// asks it about its output modes, detects the colour profile from
/// its environment, and waits a little for replies to queries before
/// it ends. A scripted terminal answers only what its input says.
#[must_use]
pub const fn terminal(mut self) -> Self {
self.terminal = true;
self
}
/// Adds key presses, in order.
#[must_use]
pub fn keys(self, keys: impl IntoIterator<Item = Key>) -> Self {
self.inputs(keys.into_iter().map(Input::KeyPress))
}
/// Adds input to send, in order: keys, mouse events, resizes,
/// signals.
#[must_use]
pub fn inputs(mut self, inputs: impl IntoIterator<Item = Input>) -> Self {
self.steps.extend(inputs.into_iter().map(Step::Input));
self
}
/// Adds bytes as the terminal would send them, in one read. They go
/// through the real decoder: a sequence cut off at the end waits for
/// the next `bytes` step, unless the gap reaches the 50 ms escape
/// timeout or another kind of step comes next, when it is read as it
/// is.
#[must_use]
pub fn bytes(mut self, bytes: impl AsRef<[u8]>) -> Self {
self.steps.push(Step::Bytes(bytes.as_ref().to_vec()));
self
}
/// The pause before each input (5 ms by default), so each update's
/// commands can run in between.
#[must_use]
pub const fn gap(mut self, gap: Duration) -> Self {
self.gap = gap;
self
}
/// A handle on what the program writes.
#[must_use]
pub fn screen(&self) -> Screen {
self.screen.clone()
}
}
/// Sends the input among `events`; `false` once the program has stopped.
fn send(events: Vec<Event>, sink: &InputSink) -> bool {
crate::input::inputs(events).all(sink)
}
/// Plays `steps` into `sink`, one every `gap`.
fn play(steps: &[Step], gap: Duration, sink: &InputSink) {
let mut reader = Reader::default();
for (i, step) in steps.iter().enumerate() {
std::thread::sleep(gap);
let delivered = match step {
Step::Input(input) => sink(input.clone()),
Step::Bytes(bytes) => {
let now = Instant::now();
let more = matches!(steps.get(i + 1), Some(Step::Bytes(_))) && gap < ESC_TIMEOUT;
send(reader.feed(bytes, now), sink) && (more || send(reader.flush(now), sink))
}
};
if !delivered {
return;
}
}
}
impl Backend for TestBackend {
fn enter(&mut self) -> io::Result<()> {
Ok(())
}
fn leave(&mut self) -> io::Result<()> {
Ok(())
}
fn size(&mut self) -> io::Result<Size> {
Ok(self.size)
}
fn start_input(&mut self, sink: InputSink) {
let (steps, gap) = (self.steps.clone(), self.gap);
drop(std::thread::spawn(move || play(&steps, gap, &sink)));
}
fn output(&mut self) -> &mut dyn Write {
&mut self.screen
}
fn is_terminal(&self) -> bool {
self.terminal
}
}
#[cfg(test)]
mod tests;