1use std::{sync::mpsc::{Receiver}, thread};
2
3use indicatif::{ProgressBar, ProgressStyle};
4use thread::JoinHandle;
5
6#[derive(Debug, Copy, Clone)]
7pub enum ProgressStatus {
8 Step(u64),
9 Finished,
10}
11
12pub fn new_progress_bar(data_count: u64) -> ProgressBar {
13 let progress_bar = ProgressBar::new(data_count);
14 progress_bar.set_style(
15 ProgressStyle::default_bar()
16 .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
17 .progress_chars("##-"),
18 );
19 progress_bar
20}
21
22pub fn spawn_progress_thread(bar: ProgressBar, rx: Receiver<ProgressStatus>) -> JoinHandle<()> {
23 thread::spawn(move || loop {
24 match rx.recv() {
25 Ok(ProgressStatus::Finished) => {
26 break;
27 }
28 Ok(ProgressStatus::Step(increment)) => {
29 if !bar.is_finished() {
30 bar.inc(increment);
31 }
32 }
33 Err(_) => {}
34 }
35 })
36}