This repository has no description
1use indicatif::ProgressBar;
2use std::borrow::Cow;
3
4pub const PROGRESS_BARS_STYLE: &str = "\x1b]9;4;1;{percent}\x1b\\{prefix:>12.bold.cyan} {percent:03}% [{bar:25}] {msg:01} ({per_sec}, {elapsed} ago)";
5
6pub fn setup_progress_bar(total: u64, verb: &'static str) -> ProgressBar {
7 indicatif::ProgressBar::new(total)
8 .with_prefix(verb)
9 .with_style(
10 indicatif::ProgressStyle::with_template(PROGRESS_BARS_STYLE)
11 .unwrap()
12 .progress_chars("=> "),
13 )
14 .with_finish(indicatif::ProgressFinish::WithMessage(
15 "\x1b]9;4;0\x1b\\".into(),
16 ))
17}
18
19pub trait MaybeProgressBar<'a> {
20 fn set_message(&'a self, message: impl Into<Cow<'static, str>>);
21 fn set_length(&'a self, length: u64);
22 fn inc(&'a self, n: u64);
23 fn println(&'a self, message: impl AsRef<str>);
24}
25
26impl<'a> MaybeProgressBar<'a> for Option<&'a ProgressBar> {
27 fn set_message(&'a self, message: impl Into<Cow<'static, str>>) {
28 if let Some(pb) = self {
29 pb.set_message(message);
30 }
31 }
32
33 fn set_length(&'a self, length: u64) {
34 if let Some(pb) = self {
35 pb.set_length(length);
36 }
37 }
38
39 fn inc(&'a self, n: u64) {
40 if let Some(pb) = self {
41 pb.inc(n);
42 }
43 }
44
45 fn println(&'a self, message: impl AsRef<str>) {
46 if let Some(pb) = self {
47 pb.println(message);
48 } else {
49 println!("{}", message.as_ref());
50 }
51 }
52}