lazy_pbar/lib.rs
1use indicatif::{ProgressIterator, ProgressStyle, ProgressBarIter};
2
3const PBAR_TEMPLATE: &str = "{msg} |{wide_bar}| {pos}/{len} [{elapsed_precise}>{eta_precise}]";
4
5/// A very lazy wrapper for any ProgressIterator.
6///
7/// Looks nice enough as a default.
8///
9/// Style heavily borrowing from [tqdm](https://github.com/tqdm/tqdm).
10///
11/// Example:
12/// ```
13/// use lazy_pbar::pbar_sized;
14///
15/// for i in pbar_sized(0..1_000_000, 1_000_000) {
16/// // Do whatever
17/// }
18/// ```
19pub fn pbar_sized<I: ProgressIterator>(it: I, len: usize) -> ProgressBarIter<I>{
20 let pbar = indicatif::ProgressBar::new(len as u64)
21 .with_style(
22 ProgressStyle::default_bar()
23 .template(PBAR_TEMPLATE)
24 );
25 it.progress_with(pbar)
26}
27
28/// A very lazy wrapper for a ProgressIterator that is also an ExactSizeIterator.
29///
30/// Looks nice enough as a default.
31///
32/// Style heavily borrowing from [tqdm](https://github.com/tqdm/tqdm).
33///
34/// Example:
35/// ```
36/// use lazy_pbar::pbar;
37///
38/// for i in pbar(0..1_000_000) {
39/// // Do whatever
40/// }
41/// ```
42pub fn pbar<I: ProgressIterator + ExactSizeIterator>(it: I) -> ProgressBarIter<I>{
43 let pbar = indicatif::ProgressBar::new(it.len() as u64)
44 .with_style(
45 ProgressStyle::default_bar()
46 .template(PBAR_TEMPLATE)
47 );
48 it.progress_with(pbar)
49}