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
//! Commands that hand the terminal to something else and take it back:
//! running another program, suspending, releasing, and repainting after
//! the screen was written over.
use ;
use io;
use ;
/// Runs `command` in the terminal, as Bubble Tea's `tea.ExecProcess`
/// does, then sends `done(result)`: for an editor, a pager or a shell.
///
/// The program hands the terminal over on its own thread, in program
/// order: it stops reading input (bytes not yet read stay for the
/// child), turns every mode it set off, leaves the alternate screen,
/// erases an inline frame and puts the terminal's own mode back. The
/// child inherits the program's standard input, output and error. When
/// it exits, the program takes the terminal back: raw mode, every mode
/// on again, the whole frame drawn again, input read again; `done`'s
/// message is the next one the model sees. Keys typed while the child
/// runs are the child's; none are lost or read twice (Bubble Tea issues
/// #616 and #1424). Ctrl-Z while the child has the terminal stops both,
/// as in a shell.
///
/// ```
/// use petitcarnets as tea;
/// use std::process::Command;
///
/// enum Msg {
/// EditorClosed(std::io::Result<std::process::ExitStatus>),
/// }
///
/// struct Editor(Option<bool>);
///
/// impl tea::Model for Editor {
/// type Msg = Msg;
/// fn update(&mut self, msg: tea::Msg<Msg>) -> tea::Cmd<Msg> {
/// match msg {
/// // `vim` in a real program; `true` here, which ends at once.
/// tea::Msg::KeyPress(k) if k == "e" => tea::exec(Command::new("true"), Msg::EditorClosed),
/// tea::Msg::App(Msg::EditorClosed(status)) => {
/// self.0 = Some(status.is_ok_and(|s| s.success()));
/// tea::quit()
/// }
/// _ => tea::Cmd::none(),
/// }
/// }
/// fn view(&self) -> tea::View {
/// "e: edit".into()
/// }
/// }
///
/// let mut test = tea::testing::TestProgram::new(Editor(None), 20, 2);
/// test.type_text("e");
/// assert_eq!(test.final_model().0, Some(true));
/// ```
/// Something to run in the terminal with [`exec_command`]: Bubble Tea's
/// `tea.ExecCommand`. It runs on the program's thread while the program
/// has handed the terminal over, and reads and writes the terminal
/// through the process's standard streams.
/// A [`Command`] succeeds when it exits with status 0.
/// Runs `command` in the terminal as [`exec`] runs a [`Command`], then
/// sends `done(result)`: Bubble Tea's `tea.Exec`, for anything that
/// needs the terminal to itself.
///
/// ```
/// use petitcarnets as tea;
///
/// struct Task;
///
/// impl tea::ExecCommand for Task {
/// fn run(&mut self) -> std::io::Result<()> {
/// Ok(())
/// }
/// }
///
/// struct App(Option<bool>);
///
/// impl tea::Model for App {
/// type Msg = std::io::Result<()>;
/// fn update(&mut self, msg: tea::Msg<Self::Msg>) -> tea::Cmd<Self::Msg> {
/// match msg {
/// tea::Msg::KeyPress(k) if k == "x" => tea::exec_command(Task, |r| r),
/// tea::Msg::App(result) => {
/// self.0 = Some(result.is_ok());
/// tea::quit()
/// }
/// _ => tea::Cmd::none(),
/// }
/// }
/// fn view(&self) -> tea::View {
/// "x: run".into()
/// }
/// }
///
/// let mut test = tea::testing::TestProgram::new(App(None), 20, 2);
/// test.type_text("x");
/// assert_eq!(test.final_model().0, Some(true));
/// ```
+ Send + 'static,
)
/// Suspends the program as Ctrl-Z in a shell suspends one: the terminal
/// is handed back as for [`exec`], the process (and its process group)
/// stops, and when the shell continues it (`fg`), the program takes the
/// terminal back, draws the whole frame and sends
/// [`Msg::Resume`](crate::Msg::Resume). Bubble Tea's `tea.Suspend`.
///
/// Ctrl-Z does this by default; see
/// [`Program::with_suspend`](crate::Program::with_suspend).
pub const
/// Hands the terminal over as [`exec`] does and keeps it handed over,
/// while the program goes on updating the model, until
/// [`restore_terminal`]. Nothing is drawn meanwhile, and output from
/// [`println`](crate::println), [`raw`](crate::raw) and the like waits.
/// Bubble Tea's `Program.ReleaseTerminal`.
///
/// Go's is a method callable from any goroutine; here the terminal
/// belongs to the program's thread, so this is a command, run in program
/// order: `sequence([release_terminal(), Cmd::task(work),
/// restore_terminal()])` runs `work` with the terminal to itself.
pub const
/// Takes the terminal back after [`release_terminal`]: raw mode, every
/// mode on again, the whole frame drawn again. Bubble Tea's
/// `Program.RestoreTerminal`. Does nothing if the terminal is not
/// released.
pub const
/// Turns every mode the program set off and on again and draws the whole
/// frame, for when something else wrote over the terminal: Bubble Tea's
/// `tea.Repaint` (its issue #1808). Unlike [`clear_screen`](crate::clear_screen)
/// it enters the alternate screen again if something left it, and
/// redraws even when the view has not changed.
pub const