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
//! What a model shows: its text, and everything else about the terminal
//! a frame decides (screen, cursor, title, colours, progress bar, input
//! modes).
use crate::color::Rgb;
use crate::mode::KeyboardEnhancements;
use crate::mouse::MouseMode;
/// What a model shows: Bubble Tea's `tea.View`. Make one from a string
/// with `.into()` or [`View::new`], and set the rest with the `with_`
/// methods. Everything is declared per frame: after each frame the
/// program changes whatever differs from the frame before, and puts the
/// terminal back when it ends.
///
/// A view is drawn inline, below the shell prompt, unless it sets
/// [`alt_screen`](View::alt_screen), as in Bubble Tea v2.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct View {
/// The text to draw, one line per row; ANSI styles (SGR) and
/// hyperlinks (OSC 8) are kept, other escape sequences dropped, and
/// tabs expanded to the next multiple of 8 columns.
pub content: String,
/// Draw on the alternate screen, the whole window, instead of
/// inline. Bubble Tea's `AltScreen`.
pub alt_screen: bool,
/// Where to show the cursor, relative to the frame's top left, and
/// how; `None` hides it. Bubble Tea's `Cursor`.
pub cursor: Option<Cursor>,
/// The window title (OSC 2); empty leaves it alone. The title the
/// terminal had is put back at the end where the terminal keeps a
/// title stack. Bubble Tea's `WindowTitle`.
pub window_title: String,
/// The terminal's default text colour (OSC 10); `None` keeps or
/// resets the terminal's own. Bubble Tea's `ForegroundColor`.
pub foreground_color: Option<Rgb>,
/// The terminal's default background colour (OSC 11); `None` keeps or
/// resets the terminal's own. Bubble Tea's `BackgroundColor`.
pub background_color: Option<Rgb>,
/// A progress bar in the terminal's tab or title bar (OSC 9;4), where
/// the terminal has one. Bubble Tea's `ProgressBar`.
pub progress_bar: Option<ProgressBar>,
/// Which mouse events to receive; none by default.
pub mouse_mode: MouseMode,
/// Whether to receive [`Msg::Focus`](crate::Msg::Focus) and
/// [`Msg::Blur`](crate::Msg::Blur).
pub report_focus: bool,
/// Turns bracketed paste off, so pasted text arrives as keys. It is
/// on by default, as in Bubble Tea.
pub disable_bracketed_paste_mode: bool,
/// Keyboard enhancements to ask for. Key disambiguation (the kitty
/// protocol's first flag) is always asked for, as in Bubble Tea.
pub keyboard_enhancements: KeyboardEnhancements,
}
impl View {
/// A view of `content`. Bubble Tea's `tea.NewView`.
#[must_use]
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
..Self::default()
}
}
/// Replaces the content. Bubble Tea's `View.SetContent`.
pub fn set_content(&mut self, content: impl Into<String>) {
self.content = content.into();
}
/// The same view on the alternate screen (`true`) or inline.
#[must_use]
pub fn with_alt_screen(mut self, on: bool) -> Self {
self.alt_screen = on;
self
}
/// The same view with the cursor shown as `cursor`.
#[must_use]
pub fn with_cursor(mut self, cursor: Cursor) -> Self {
self.cursor = Some(cursor);
self
}
/// The same view with this window title.
#[must_use]
pub fn with_window_title(mut self, title: impl Into<String>) -> Self {
self.window_title = title.into();
self
}
/// The same view with this default text colour.
#[must_use]
pub fn with_foreground_color(mut self, color: Rgb) -> Self {
self.foreground_color = Some(color);
self
}
/// The same view with this default background colour.
#[must_use]
pub fn with_background_color(mut self, color: Rgb) -> Self {
self.background_color = Some(color);
self
}
/// The same view with this progress bar.
#[must_use]
pub fn with_progress_bar(mut self, bar: ProgressBar) -> Self {
self.progress_bar = Some(bar);
self
}
/// The same view with mouse events of `mode`.
#[must_use]
pub fn with_mouse_mode(mut self, mode: MouseMode) -> Self {
self.mouse_mode = mode;
self
}
/// The same view with focus reports on or off.
#[must_use]
pub fn with_report_focus(mut self, on: bool) -> Self {
self.report_focus = on;
self
}
/// The same view with bracketed paste on (the default) or off.
#[must_use]
pub fn with_bracketed_paste(mut self, on: bool) -> Self {
self.disable_bracketed_paste_mode = !on;
self
}
/// The same view with these keyboard enhancements.
#[must_use]
pub fn with_keyboard_enhancements(mut self, enhancements: KeyboardEnhancements) -> Self {
self.keyboard_enhancements = enhancements;
self
}
}
impl From<String> for View {
fn from(content: String) -> Self {
Self::new(content)
}
}
impl From<&str> for View {
fn from(content: &str) -> Self {
Self::new(content)
}
}
/// The cursor's shape. Bubble Tea's `CursorShape`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum CursorShape {
/// A block.
#[default]
Block,
/// An underline.
Underline,
/// A vertical bar.
Bar,
}
/// The cursor: where it is, relative to the frame's top left, and how
/// it looks. Bubble Tea's `tea.Cursor`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cursor {
/// The column, from 0.
pub x: u16,
/// The row, from 0.
pub y: u16,
/// Its shape.
pub shape: CursorShape,
/// Whether it blinks.
pub blink: bool,
/// Its colour (OSC 12); `None` keeps the terminal's.
pub color: Option<Rgb>,
}
impl Cursor {
/// A blinking block at column `x` of row `y`, in the terminal's
/// colour. Bubble Tea's `tea.NewCursor`.
#[must_use]
pub const fn new(x: u16, y: u16) -> Self {
Self {
x,
y,
shape: CursorShape::Block,
blink: true,
color: None,
}
}
/// The same cursor with this shape.
#[must_use]
pub const fn with_shape(mut self, shape: CursorShape) -> Self {
self.shape = shape;
self
}
/// The same cursor, blinking or steady.
#[must_use]
pub const fn with_blink(mut self, blink: bool) -> Self {
self.blink = blink;
self
}
/// The same cursor in this colour.
#[must_use]
pub const fn with_color(mut self, color: Rgb) -> Self {
self.color = Some(color);
self
}
/// The DECSCUSR value for the shape: 1 and 2 a blinking or steady
/// block, 3 and 4 an underline, 5 and 6 a bar. Go's
/// `encodeCursorStyle`.
#[must_use]
pub const fn style(&self) -> u8 {
let base = match self.shape {
CursorShape::Block => 1,
CursorShape::Underline => 3,
CursorShape::Bar => 5,
};
if self.blink { base } else { base + 1 }
}
}
/// What a progress bar shows. Bubble Tea's `ProgressBarState`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ProgressBarState {
/// No progress bar.
#[default]
None,
/// Progress, as a percentage.
Default,
/// An error, at a percentage.
Error,
/// Busy, with no percentage.
Indeterminate,
/// A warning, at a percentage.
Warning,
}
/// A progress bar the terminal shows in its tab or title bar (OSC 9;4),
/// where it has one. Bubble Tea's `tea.ProgressBar`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ProgressBar {
/// What it shows.
pub state: ProgressBarState,
/// The percentage, 0 to 100.
pub value: u8,
}
impl ProgressBar {
/// A bar in `state` at `value` percent, cut to 100. Bubble Tea's
/// `tea.NewProgressBar`.
#[must_use]
pub fn new(state: ProgressBarState, value: u8) -> Self {
Self {
state,
value: value.min(100),
}
}
/// The OSC 9;4 sequence that shows the bar.
pub(crate) fn sequence(self) -> String {
let v = self.value.min(100);
match self.state {
ProgressBarState::None => "\x1b]9;4;0\x07".to_string(),
ProgressBarState::Default => format!("\x1b]9;4;1;{v}\x07"),
ProgressBarState::Error => format!("\x1b]9;4;2;{v}\x07"),
ProgressBarState::Indeterminate => "\x1b]9;4;3\x07".to_string(),
ProgressBarState::Warning => format!("\x1b]9;4;4;{v}\x07"),
}
}
}
#[cfg(test)]
mod tests;