Skip to content

Commit

Permalink
Add ProgressIterator::progress_with_style()
Browse files Browse the repository at this point in the history
Style a ProgressIterator without manually handling the length of the bar
  • Loading branch information
LeCyberDucky authored and djc committed Aug 11, 2021
1 parent df78bec commit 85d78ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ where

/// Wrap an iterator with a custom progress bar.
fn progress_with(self, progress: ProgressBar) -> ProgressBarIter<Self>;

/// Wrap an iterator with a progress bar and style it.
fn progress_with_style(self, style: crate::ProgressStyle) -> ProgressBarIter<Self>
where
Self: ExactSizeIterator,
{
let len = u64::try_from(self.len()).unwrap();
let bar = ProgressBar::new(len).with_style(style);
self.progress_with(bar)
}
}

/// Wraps an iterator to display its progress.
Expand Down Expand Up @@ -166,6 +176,7 @@ impl<S, T: Iterator<Item = S>> ProgressIterator for T {
mod test {
use crate::iter::{ProgressBarIter, ProgressIterator};
use crate::progress_bar::ProgressBar;
use crate::ProgressStyle;

#[test]
fn it_can_wrap_an_iterator() {
Expand All @@ -180,5 +191,9 @@ mod test {
let pb = ProgressBar::new(v.len() as u64);
v.iter().progress_with(pb)
});
wrap({
let style = ProgressStyle::default_bar().template("{wide_bar:.red} {percent}/100%");
v.iter().progress_with_style(style)
});
}
}
16 changes: 16 additions & 0 deletions src/rayon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ where
let len = u64::try_from(self.len()).unwrap();
self.progress_count(len)
}

/// Wrap an iterator with a progress bar and style it.
fn progress_with_style(self, style: crate::ProgressStyle) -> ProgressBarIter<Self>
where
Self: IndexedParallelIterator,
{
let len = u64::try_from(self.len()).unwrap();
let bar = ProgressBar::new(len).with_style(style);
self.progress_with(bar)
}
}

impl<S: Send, T: ParallelIterator<Item = S>> ParallelProgressIterator for T {
Expand Down Expand Up @@ -199,6 +209,7 @@ impl<S: Send, T: ParallelIterator<Item = S>> ParallelIterator for ProgressBarIte

#[cfg(test)]
mod test {
use crate::ProgressStyle;
use crate::{ParallelProgressIterator, ProgressBar, ProgressBarIter};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

Expand All @@ -214,5 +225,10 @@ mod test {
let pb = ProgressBar::new(v.len() as u64);
v.par_iter().progress_with(pb)
});

wrap({
let style = ProgressStyle::default_bar().template("{wide_bar:.red} {percent}/100%");
v.par_iter().progress_with_style(style)
});
}
}

0 comments on commit 85d78ac

Please sign in to comment.