petitcarnets 0.0.7

Terminal user interfaces in the Elm architecture (model, update, view), with no dependencies.
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Commands that ask the terminal something, and the clipboard. Replies
//! arrive as messages whenever the terminal sends them; nothing waits
//! for one, and a terminal that never answers costs nothing but, at the
//! end, a short bounded wait (see [`Program::run`](crate::Program::run)).

use super::{Cmd, Inner, Output};
use std::fmt::Write as _;

/// A query: bytes for the terminal whose reply is read before the
/// terminal is handed back.
fn query<M>(bytes: impl Into<Vec<u8>>) -> Cmd<M> {
    Cmd(Inner::Output(Output::Query(bytes.into())))
}

/// Sends the window's size as [`Msg::WindowSize`](crate::Msg::WindowSize),
/// read from the terminal now. Bubble Tea's `tea.RequestWindowSize`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::TestProgram;
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_window_size()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let test = TestProgram::new(Log(Vec::new()), 20, 1);
/// let size = tea::Msg::WindowSize(tea::Size::new(20, 1));
/// // Once at the start, and once more when asked.
/// assert_eq!(test.model().0.iter().filter(|m| **m == size).count(), 2);
/// ```
#[must_use]
pub const fn request_window_size<M>() -> Cmd<M> {
    Cmd(Inner::Output(Output::WindowSize))
}

/// Asks where the cursor is; the reply is
/// [`Msg::CursorPosition`](crate::Msg::CursorPosition). Bubble Tea's
/// `tea.RequestCursorPosition`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_cursor_position()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b[6n", "\x1b[3;7R");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let position = tea::Msg::CursorPosition { x: 6, y: 2 };
/// assert!(test.model().0.contains(&position));
/// ```
#[must_use]
pub fn request_cursor_position<M>() -> Cmd<M> {
    query(b"\x1b[6n".as_slice())
}

/// Asks for the terminal's background colour; the reply is
/// [`Msg::BackgroundColor`](crate::Msg::BackgroundColor). Bubble Tea's
/// `tea.RequestBackgroundColor`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_background_color()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b]11;?\x07", "\x1b]11;rgb:1a1a/1b1b/2c2c\x07");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let color = tea::Msg::BackgroundColor(tea::Rgb::new(0x1a, 0x1b, 0x2c));
/// assert!(test.model().0.contains(&color));
/// ```
#[must_use]
pub fn request_background_color<M>() -> Cmd<M> {
    query(b"\x1b]11;?\x07".as_slice())
}

/// Asks for the terminal's text colour; the reply is
/// [`Msg::ForegroundColor`](crate::Msg::ForegroundColor). Bubble Tea's
/// `tea.RequestForegroundColor`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_foreground_color()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b]10;?\x07", "\x1b]10;rgb:ffff/ffff/ffff\x07");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let color = tea::Msg::ForegroundColor(tea::Rgb::new(255, 255, 255));
/// assert!(test.model().0.contains(&color));
/// ```
#[must_use]
pub fn request_foreground_color<M>() -> Cmd<M> {
    query(b"\x1b]10;?\x07".as_slice())
}

/// Asks for the cursor's colour; the reply is
/// [`Msg::CursorColor`](crate::Msg::CursorColor). Bubble Tea's
/// `tea.RequestCursorColor`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_cursor_color()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b]12;?\x07", "\x1b]12;rgb:ff00/8000/0000\x07");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let color = tea::Msg::CursorColor(tea::Rgb::new(255, 128, 0));
/// assert!(test.model().0.contains(&color));
/// ```
#[must_use]
pub fn request_cursor_color<M>() -> Cmd<M> {
    query(b"\x1b]12;?\x07".as_slice())
}

/// Asks for the terminal's name and version (`XTVERSION`); the reply is
/// [`Msg::TerminalVersion`](crate::Msg::TerminalVersion). Bubble Tea's
/// `tea.RequestTerminalVersion`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_terminal_version()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b[>q", "\x1bP>|kitty(0.40.0)\x1b\\");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let version = tea::Msg::TerminalVersion("kitty(0.40.0)".into());
/// assert!(test.model().0.contains(&version));
/// ```
#[must_use]
pub fn request_terminal_version<M>() -> Cmd<M> {
    query(b"\x1b[>q".as_slice())
}

/// Asks for a terminfo capability by name (`XTGETTCAP`), such as `RGB`,
/// `Tc` or `Ms`; the reply is [`Msg::Capability`](crate::Msg::Capability),
/// and none comes for a capability the terminal does not know. Bubble
/// Tea's `tea.RequestCapability`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::request_capability("RGB")
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1bP+q524742\x1b\\", "\x1bP1+r524742\x1b\\");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// assert!(test.model().0.contains(&tea::Msg::Capability("RGB".into())));
/// let profile = tea::Msg::ColorProfile(tea::ColorProfile::TrueColor);
/// assert!(test.model().0.contains(&profile));
/// ```
#[must_use]
pub fn request_capability<M>(name: &str) -> Cmd<M> {
    let mut bytes = String::from("\x1bP+q");
    for b in name.bytes() {
        let _ = write!(bytes, "{b:02X}");
    }
    bytes.push_str("\x1b\\");
    query(bytes)
}

/// Copies `text` to the system clipboard (OSC 52), where the terminal
/// allows it. Bubble Tea's `tea.SetClipboard`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::TestProgram;
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::set_clipboard("hi")
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let test = TestProgram::new(Log(Vec::new()), 20, 1);
/// let output = String::from_utf8_lossy(test.output());
/// assert!(output.contains("\x1b]52;c;aGk=\x07"));
/// ```
#[must_use]
pub fn set_clipboard<M>(text: &str) -> Cmd<M> {
    Cmd(Inner::Output(Output::Raw(osc52(b'c', text))))
}

/// Copies `text` to the primary selection (OSC 52). Bubble Tea's
/// `tea.SetPrimaryClipboard`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::TestProgram;
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::set_primary_clipboard("hi")
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let test = TestProgram::new(Log(Vec::new()), 20, 1);
/// let output = String::from_utf8_lossy(test.output());
/// assert!(output.contains("\x1b]52;p;aGk=\x07"));
/// ```
#[must_use]
pub fn set_primary_clipboard<M>(text: &str) -> Cmd<M> {
    Cmd(Inner::Output(Output::Raw(osc52(b'p', text))))
}

/// Asks for the system clipboard's text; the reply, where the terminal
/// allows it, is [`Msg::Clipboard`](crate::Msg::Clipboard). Bubble Tea's
/// `tea.ReadClipboard`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::read_clipboard()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b]52;c;?\x07", "\x1b]52;c;aGk=\x07");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let content = "hi".to_string();
/// let selection = tea::ClipboardSelection::System;
/// let clipboard = tea::Msg::Clipboard { content, selection };
/// assert!(test.model().0.contains(&clipboard));
/// ```
#[must_use]
pub fn read_clipboard<M>() -> Cmd<M> {
    query(b"\x1b]52;c;?\x07".as_slice())
}

/// Asks for the primary selection's text; the reply is
/// [`Msg::Clipboard`](crate::Msg::Clipboard). Bubble Tea's
/// `tea.ReadPrimaryClipboard`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::testing::{TestProgram, TestTerminal};
///
/// /// Keeps every message it gets.
/// struct Log(Vec<tea::Msg<()>>);
///
/// impl tea::Model for Log {
///     type Msg = ();
///
///     fn init(&mut self) -> tea::Cmd<()> {
///         tea::read_primary_clipboard()
///     }
///
///     fn update(&mut self, msg: tea::Msg<()>) -> tea::Cmd<()> {
///         self.0.push(msg);
///         tea::Cmd::none()
///     }
/// #
/// #     fn view(&self) -> tea::View {
/// #         tea::View::default()
/// #     }
/// }
///
/// let terminal = TestTerminal::new(20, 1).reply("\x1b]52;p;?\x07", "\x1b]52;p;aGk=\x07");
/// let test = TestProgram::with_terminal(tea::Program::new(Log(Vec::new())), terminal);
/// let content = "hi".to_string();
/// let selection = tea::ClipboardSelection::Primary;
/// let clipboard = tea::Msg::Clipboard { content, selection };
/// assert!(test.model().0.contains(&clipboard));
/// ```
#[must_use]
pub fn read_primary_clipboard<M>() -> Cmd<M> {
    query(b"\x1b]52;p;?\x07".as_slice())
}

/// OSC 52 setting `selection` to `text`, as Go's `ansi.SetClipboard`
/// writes it: empty text clears it.
fn osc52(selection: u8, text: &str) -> Vec<u8> {
    let data = crate::base64::encode(text.as_bytes());
    let mut out = format!("\x1b]52;{};", char::from(selection)).into_bytes();
    out.extend_from_slice(data.as_bytes());
    out.push(0x07);
    out
}