generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathcustom_spinner.rs
More file actions
67 lines (59 loc) · 1.68 KB
/
custom_spinner.rs
File metadata and controls
67 lines (59 loc) · 1.68 KB
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
use crossterm::{
cursor,
execute,
terminal,
};
use indicatif::{
ProgressBar,
ProgressStyle,
};
use tokio_util::sync::CancellationToken;
use crate::theme::StyledText;
const SPINNER_CHARS: &str = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏";
pub struct Spinners {
cancellation_token: CancellationToken,
}
impl Spinners {
pub fn new(message: String) -> Self {
// Hide the cursor when starting the spinner
let _ = execute!(std::io::stderr(), cursor::Hide);
let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.tick_chars(SPINNER_CHARS)
.template("{spinner} {msg}")
.unwrap(),
);
pb.set_message(message);
let token = CancellationToken::new();
let token_clone = token.clone();
tokio::spawn(async move {
loop {
tokio::select! {
_ = token_clone.cancelled() => {
break;
},
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {
pb.tick();
}
}
}
Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
});
Self {
cancellation_token: token,
}
}
}
impl Drop for Spinners {
fn drop(&mut self) {
self.cancellation_token.cancel();
let _ = execute!(
std::io::stderr(),
terminal::Clear(terminal::ClearType::CurrentLine),
cursor::MoveToColumn(0),
StyledText::reset_attributes(),
cursor::Show
);
}
}