feat: update dependencies, optimize
This commit is contained in:
@@ -1,28 +1,46 @@
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use rust_util::{debugging, util_msg};
|
||||
|
||||
use crate::util_env;
|
||||
|
||||
const PB_PROGRESS: &str = "#-";
|
||||
const PB_TEMPLATE: &str = "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} {bytes_per_sec} ({eta})";
|
||||
|
||||
|
||||
pub struct Progress {
|
||||
progress_bar: ProgressBar,
|
||||
pub enum Progress {
|
||||
NoProgress,
|
||||
Progress(ProgressBar),
|
||||
}
|
||||
|
||||
impl Progress {
|
||||
pub fn new(total: u64) -> Self {
|
||||
let progress_bar = ProgressBar::new(total);
|
||||
progress_bar.set_style(ProgressStyle::default_bar()
|
||||
.template(PB_TEMPLATE).expect("SHOULD NOT FAIL")
|
||||
.progress_chars(PB_PROGRESS)
|
||||
);
|
||||
Self { progress_bar }
|
||||
let no_progress = util_env::get_no_progress();
|
||||
let is_atty = util_msg::is_atty();
|
||||
|
||||
if no_progress || !is_atty {
|
||||
debugging!("No progress: [{}, {}]", no_progress, is_atty);
|
||||
Self::NoProgress
|
||||
} else {
|
||||
let progress_bar = ProgressBar::new(total);
|
||||
progress_bar.set_style(ProgressStyle::default_bar()
|
||||
.template(PB_TEMPLATE).expect("SHOULD NOT FAIL")
|
||||
.progress_chars(PB_PROGRESS)
|
||||
);
|
||||
Self::Progress(progress_bar)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn position(&self, position: u64) {
|
||||
self.progress_bar.set_position(position)
|
||||
match self {
|
||||
Progress::NoProgress => {}
|
||||
Progress::Progress(progress_bar) => progress_bar.set_position(position),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(&self) {
|
||||
self.progress_bar.finish_and_clear()
|
||||
match self {
|
||||
Progress::NoProgress => {}
|
||||
Progress::Progress(progress_bar) => progress_bar.finish_and_clear(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user