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
use std::io::Write;
use std::sync::mpsc::{self, Receiver, Sender, TryRecvError};
use std::thread::{self, JoinHandle};
use std::time::Duration;
pub const DEFAULT_F: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
pub const CIRCLE_F: [&str; 4] = ["◐", "◓", "◑", "◒"];
pub const ROTATE_F: [&str; 4] = ["|", "/", "-", "\\"];
pub const MOVE_EQ_F: [&str; 4] = ["[= ]", "[ = ]", "[ =]", "[ = ]"];
pub const MOVE_MIN_F: [&str; 4] = ["[- ]", "[ - ]", "[ -]", "[ - ]"];
pub const MOVE_EQ_LONG_F: [&str; 10] = [
"[= ]", "[== ]", "[ == ]", "[ == ]", "[ ==]", "[ =]", "[ ==]", "[ == ]",
"[ == ]", "[== ]",
];
pub const MOVE_MIN_LONG_F: [&str; 10] = [
"[- ]", "[-- ]", "[ -- ]", "[ -- ]", "[ --]", "[ -]", "[ --]", "[ -- ]",
"[ -- ]", "[-- ]",
];
pub struct Throbber {
anim: Option<ThrobberAnim>,
message: String,
interval: Duration,
frames: &'static [&'static str],
}
struct ThrobberAnim {
thread: JoinHandle<()>,
sender: Sender<ThrobberSignal>,
}
enum ThrobberSignal {
Start,
Finish,
Succ(String),
Fail(String),
ChMsg(String),
ChInt(Duration),
ChFrames(&'static [&'static str]),
End,
}
impl Throbber {
pub fn new() -> Self {
Self {
anim: None,
message: "".to_string(),
interval: Duration::from_millis(200),
frames: &DEFAULT_F,
}
}
pub fn message(mut self, msg: String) -> Self {
self.message = msg;
self
}
pub fn interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
pub fn frames(mut self, frames: &'static [&'static str]) -> Self {
self.frames = frames;
self
}
pub fn change_message(&mut self, msg: String) {
if let Some(ref anim) = self.anim {
anim.sender
.send(ThrobberSignal::ChMsg(msg.clone()))
.unwrap();
anim.thread.thread().unpark();
}
self.message = msg;
}
pub fn change_interval(&mut self, interval: Duration) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::ChInt(interval)).unwrap();
anim.thread.thread().unpark();
}
self.interval = interval;
}
pub fn change_frames(&mut self, frames: &'static [&'static str]) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::ChFrames(frames)).unwrap();
anim.thread.thread().unpark();
}
self.frames = frames;
}
pub fn start(&mut self) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::Start).unwrap();
anim.thread.thread().unpark();
return;
}
let (sender, receiver): (Sender<ThrobberSignal>, Receiver<ThrobberSignal>) =
mpsc::channel();
let msg = self.message.clone();
let frames = self.frames;
let interval = self.interval;
let thread = thread::spawn(move || animation_thread(receiver, msg, frames, interval));
self.anim = Some(ThrobberAnim { thread, sender });
}
pub fn start_with_msg(&mut self, msg: String) {
self.change_message(msg);
self.start();
}
pub fn finish(&mut self) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::Finish).unwrap();
anim.thread.thread().unpark();
}
}
pub fn success(&mut self, msg: String) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::Succ(msg)).unwrap();
anim.thread.thread().unpark();
} else {
println!("\x1B[2K\r✔ {}", msg);
}
}
pub fn fail(&mut self, msg: String) {
if let Some(ref anim) = self.anim {
anim.sender.send(ThrobberSignal::Fail(msg)).unwrap();
anim.thread.thread().unpark();
} else {
println!("\x1B[2K\r✖ {}", msg);
}
}
pub fn end(mut self) {
if let Some(anim) = self.anim.take() {
anim.sender.send(ThrobberSignal::End).unwrap();
anim.thread.thread().unpark();
anim.thread.join().unwrap();
}
}
}
fn animation_thread(
receiver: Receiver<ThrobberSignal>,
mut msg: String,
mut frames: &'static [&'static str],
mut interval: Duration,
) {
let mut play_anim = true;
let mut frame = 0;
loop {
match receiver.try_recv() {
Ok(ThrobberSignal::Start) => {
play_anim = true;
continue;
}
Ok(ThrobberSignal::Finish) => {
print!("\x1B[2K\r");
std::io::stdout().flush().unwrap();
play_anim = false;
continue;
}
Ok(ThrobberSignal::Succ(succ_msg)) => {
println!("\x1B[2K\r✔ {}", succ_msg);
play_anim = false;
continue;
}
Ok(ThrobberSignal::Fail(fail_msg)) => {
println!("\x1B[2K\r✖ {}", fail_msg);
play_anim = false;
continue;
}
Ok(ThrobberSignal::ChMsg(new_msg)) => {
msg = new_msg;
continue;
}
Ok(ThrobberSignal::ChInt(new_dur)) => {
interval = new_dur;
continue;
}
Ok(ThrobberSignal::ChFrames(new_frames)) => {
frames = new_frames;
frame = 0;
continue;
}
Ok(ThrobberSignal::End) => {
break;
}
Err(TryRecvError::Disconnected) => {
break;
}
Err(TryRecvError::Empty) => {
if play_anim == false {
thread::park();
continue;
}
}
}
print!("\x1B[2K\r");
print!("{} {}", frames[frame], msg);
std::io::stdout().flush().unwrap();
thread::sleep(interval);
frame = (frame + 1) % frames.len();
}
}