This repository has no description
1use console::Style;
2use indicatif::{ProgressBar, ProgressStyle};
3use std::sync::{Arc, Mutex};
4use std::thread::{self, JoinHandle};
5use std::time;
6
7pub struct Spinner {
8 pub spinner: ProgressBar,
9 pub finished: Arc<Mutex<bool>>,
10 pub thread: JoinHandle<()>,
11}
12
13impl Spinner {
14 pub fn start(verb: &'static str, message: &str) -> Self {
15 let spinner = ProgressBar::new(0).with_style(
16 ProgressStyle::with_template(&super::format_log_msg(
17 Style::new().bold().cyan(),
18 verb,
19 &(message.to_owned() + " {spinner:.cyan}"),
20 ))
21 .unwrap(),
22 );
23 spinner.tick();
24
25 let thread_spinner = spinner.clone();
26 let finished = Arc::new(Mutex::new(false));
27 let thread_finished = Arc::clone(&finished);
28 let spinner_thread = thread::spawn(move || {
29 while !*thread_finished.lock().unwrap() {
30 thread_spinner.tick();
31 thread::sleep(time::Duration::from_millis(100));
32 }
33 thread_spinner.finish_and_clear();
34 });
35
36 Self {
37 spinner: spinner.clone(),
38 finished,
39 thread: spinner_thread,
40 }
41 }
42
43 pub fn end(self, message: &str) {
44 self.spinner.finish_and_clear();
45 *self.finished.lock().unwrap() = true;
46 self.thread.join().unwrap();
47 println!("{}", message);
48 }
49}