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
//! A spinner: frames shown one after another while something runs.
//! Bubbles' `spinner` package, with every one of its spinners.
//!
//! ```
//! use petitcarnets as tea;
//! use tea::bubbles::spinner::{self, Spinner};
//!
//! let mut s = spinner::Model::new().with_spinner(Spinner::DOT);
//! assert_eq!(s.view(), "⣾ ");
//! let start: tea::Cmd<spinner::TickMsg> = s.tick();
//! # let _ = start;
//! ```
use super::next_id;
use crate::style::Style;
use crate::{Cmd, Msg as TeaMsg};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// A spinner's frames and how fast they turn: Bubbles' `spinner.Spinner`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Spinner {
/// The frames, in order.
pub frames: &'static [&'static str],
/// How long each frame shows (Go's `FPS`, which is a duration too).
pub fps: Duration,
}
/// `n` frames a second.
const fn per_second(n: u64) -> Duration {
Duration::from_nanos(1_000_000_000 / n)
}
impl Spinner {
/// `| / - \`.
pub const LINE: Self = Self {
frames: &["|", "/", "-", "\\"],
fps: per_second(10),
};
/// A braille dot going round, with a space after.
pub const DOT: Self = Self {
frames: &["⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "],
fps: per_second(10),
};
/// A small braille dot going round.
pub const MINI_DOT: Self = Self {
frames: &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
fps: per_second(12),
};
/// A braille dot jumping.
pub const JUMP: Self = Self {
frames: &["⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"],
fps: per_second(10),
};
/// A block fading.
pub const PULSE: Self = Self {
frames: &["█", "▓", "▒", "░"],
fps: per_second(8),
};
/// Three points, one lit.
pub const POINTS: Self = Self {
frames: &["∙∙∙", "●∙∙", "∙●∙", "∙∙●"],
fps: per_second(7),
};
/// The globe turning.
pub const GLOBE: Self = Self {
frames: &["🌍", "🌎", "🌏"],
fps: per_second(4),
};
/// The moon's phases.
pub const MOON: Self = Self {
frames: &["🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"],
fps: per_second(8),
};
/// The three wise monkeys.
pub const MONKEY: Self = Self {
frames: &["🙈", "🙉", "🙊"],
fps: per_second(3),
};
/// A meter filling and emptying.
pub const METER: Self = Self {
frames: &["▱▱▱", "▰▱▱", "▰▰▱", "▰▰▰", "▰▰▱", "▰▱▱", "▱▱▱"],
fps: per_second(7),
};
/// A trigram turning.
pub const HAMBURGER: Self = Self {
frames: &["☱", "☲", "☴", "☲"],
fps: per_second(3),
};
/// Dots after a word.
pub const ELLIPSIS: Self = Self {
frames: &["", ".", "..", "..."],
fps: per_second(3),
};
}
/// Time for a spinner to show its next frame: Bubbles' `spinner.TickMsg`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TickMsg {
/// When the tick fired, on the program's clock.
pub time: SystemTime,
/// The id of the spinner it is for; 0 is for every spinner.
pub id: u64,
tag: u64,
}
/// A spinner's messages: only ticks.
pub type Msg = TickMsg;
/// A spinner: Bubbles' `spinner.Model`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
/// The frames and speed.
pub spinner: Spinner,
/// The style frames are drawn in.
pub style: Style,
frame: usize,
id: u64,
tag: u64,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
/// A [`Spinner::LINE`] spinner with a new id: `spinner.New`.
#[must_use]
pub fn new() -> Self {
Self {
spinner: Spinner::LINE,
style: Style::new(),
frame: 0,
id: next_id(),
tag: 0,
}
}
/// With these frames: `spinner.WithSpinner`.
#[must_use]
pub const fn with_spinner(mut self, spinner: Spinner) -> Self {
self.spinner = spinner;
self
}
/// With this style: `spinner.WithStyle`.
#[must_use]
pub const fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
/// The spinner's id, which its ticks carry.
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
/// Shows the next frame on its own tick, and asks for the one after;
/// ticks for other spinners, and its own older ones, are ignored.
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
match msg {
TeaMsg::App(tick) if self.is_mine(tick) => {
self.frame = (self.frame + 1) % self.spinner.frames.len().max(1);
self.tag += 1;
let (id, tag) = (self.id, self.tag);
crate::tick(self.spinner.fps, move |time| TickMsg { time, id, tag })
}
_ => Cmd::none(),
}
}
/// Whether `tick` is this spinner's latest (an id or tag of 0 is
/// anyone's).
const fn is_mine(&self, tick: &TickMsg) -> bool {
(tick.id == 0 || tick.id == self.id) && (tick.tag == 0 || tick.tag == self.tag)
}
/// The current frame in the style, or `(error)` if there is none.
#[must_use]
pub fn view(&self) -> String {
self.spinner
.frames
.get(self.frame)
.map_or_else(|| "(error)".to_string(), |f| self.style.paint(f))
}
/// A command that starts the spinner now: Bubbles' `Model.Tick`.
#[must_use]
pub fn tick(&self) -> Cmd<Msg> {
let (id, tag) = (self.id, self.tag);
crate::tick(Duration::ZERO, move |time| TickMsg { time, id, tag })
}
/// A tick for this spinner at `time`, as the runtime would send.
#[must_use]
pub const fn tick_msg(&self, time: SystemTime) -> TickMsg {
TickMsg {
time,
id: self.id,
tag: self.tag,
}
}
}
impl Default for TickMsg {
/// A tick for every spinner, at the epoch.
fn default() -> Self {
Self {
time: UNIX_EPOCH,
id: 0,
tag: 0,
}
}
}
#[cfg(test)]
mod tests;