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
//! Components: [Bubbles](https://github.com/charmbracelet/bubbles) v2,
//! with the same names, default key bindings and behaviour, behind the
//! `bubbles` feature (on by default, no dependencies).
//!
//! | Go (`charm.land/bubbles/v2`) | Rust |
//! |------------------------------|----------------------------------------|
//! | `key` | [`key`]: bindings and their help |
//! | `help` | [`help`]: short and full help views |
//! | `cursor` | [`cursor`]: a blinking text cursor |
//! | `spinner` | [`spinner`]: every Bubbles spinner |
//! | `paginator` | [`paginator`]: pages, dots or numbers |
//! | `textinput` | [`textinput`]: one line of text |
//! | `textarea` | [`textarea`]: many lines, soft wrapped |
//! | `viewport` | [`viewport`]: a scrolling pane |
//! | `list` | [`list`]: items, filtered and paged |
//! | `table` | [`table`]: columns and rows |
//! | `progress` | [`progress`]: a bar, animated by spring |
//! | `timer` | [`timer`]: a countdown |
//! | `stopwatch` | [`stopwatch`]: a count up |
//!
//! # How components compose
//!
//! A component is a plain struct, made with `new()` (Go's `New()`), with
//! `update(&mut self, msg: &tea::Msg<X>) -> tea::Cmd<X>` and
//! `view(&self) -> String`. Go's `Update(Msg) (Model, Cmd)` returns a
//! changed copy; here the component changes in place, which is what a
//! parent does with the copy anyway, and the message is borrowed, so a
//! parent can hand the same key to several children.
//!
//! `X` is the component's own message type, always a typed enum or
//! struct of its own (such as [`spinner::TickMsg`]), never a
//! `Box<dyn Any>`. A component with no messages of its own (such as
//! [`viewport`] and [`table`]) is generic over `X`, so it takes the
//! parent's message as it is. A parent holds its children's messages in
//! its own enum, maps a child's commands up with
//! [`Cmd::map`](crate::Cmd::map) and routes messages down with
//! [`Msg::route`](crate::Msg::route):
//!
//! ```
//! use petitcarnets as tea;
//! use tea::bubbles::{spinner, textinput};
//!
//! enum Msg {
//! Spinner(spinner::TickMsg),
//! Input(textinput::Msg),
//! }
//!
//! struct Form {
//! spinner: spinner::Model,
//! input: textinput::Model,
//! }
//!
//! impl tea::Model for Form {
//! type Msg = Msg;
//!
//! fn init(&mut self) -> tea::Cmd<Msg> {
//! tea::batch([
//! self.spinner.tick().map(Msg::Spinner),
//! self.input.focus().map(Msg::Input),
//! ])
//! }
//!
//! fn update(&mut self, msg: tea::Msg<Msg>) -> tea::Cmd<Msg> {
//! if matches!(&msg, tea::Msg::KeyPress(k) if *k == "ctrl+c") {
//! return tea::quit();
//! }
//! let mut cmds = Vec::new();
//! if let Some(m) = msg.route(|m| match m {
//! Msg::Spinner(t) => Some(*t),
//! Msg::Input(_) => None,
//! }) {
//! cmds.push(self.spinner.update(&m).map(Msg::Spinner));
//! }
//! if let Some(m) = msg.route(|m| match m {
//! Msg::Input(i) => Some(*i),
//! Msg::Spinner(_) => None,
//! }) {
//! cmds.push(self.input.update(&m).map(Msg::Input));
//! }
//! tea::batch(cmds)
//! }
//!
//! fn view(&self) -> tea::View {
//! format!("{} {}", self.spinner.view(), self.input.view()).into()
//! }
//! }
//!
//! let form = Form { spinner: spinner::Model::new(), input: textinput::Model::new() };
//! let mut test = tea::testing::TestProgram::new(form, 30, 1);
//! test.type_text("hi");
//! assert!(test.screen().text().ends_with(" > hi"), "the spinner, then the input");
//! ```
//!
//! Components that tick (the spinner, the cursor's blink, the progress
//! bar's animation, the timer and stopwatch) carry an id, as Bubbles
//! does, so two of them in one parent each take only their own ticks.
//!
//! Text is measured with [`style::width`](crate::style::width), which
//! follows the running program's width method; no component splits a
//! grapheme cluster or a wide character.
use crateCmd;
use ;
use Duration;
/// A component's on or off settings, a bit each, so its struct holds
/// one field rather than many booleans.
;
/// The last id given to a component; ids start at 1.
static LAST_ID: AtomicU64 = new;
/// A new id for a component that ticks, unique in the process.
/// A command that sends `msg` now, on the program's clock, without a
/// thread: a Bubbles command that returns a message at once.