Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce the hold_max option for less jumpy Seek-implementing ProgressBarIters #642

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
pub struct ProgressBarIter<T> {
pub(crate) it: T,
pub progress: ProgressBar,
pub(crate) hold_max: bool,
}

impl<T> ProgressBarIter<T> {
Expand Down Expand Up @@ -192,6 +193,11 @@ impl<R: io::BufRead> io::BufRead for ProgressBarIter<R> {
impl<S: io::Seek> io::Seek for ProgressBarIter<S> {
fn seek(&mut self, f: io::SeekFrom) -> io::Result<u64> {
self.it.seek(f).map(|pos| {
let pos = if self.hold_max {
self.progress.position().max(pos)
} else {
pos
};
self.progress.set_position(pos);
pos
})
Expand All @@ -203,6 +209,21 @@ impl<S: io::Seek> io::Seek for ProgressBarIter<S> {
}
}

impl<S: io::Seek> ProgressBarIter<S> {
/// When random seeks are executed on the underlying io object the ProgressBar will jump harshly back and forth
/// setting hold_max to true keeps the progress to the maximum of the current and seeked-to position.
pub fn set_hold_max(&mut self, hold_max: bool) {
self.hold_max = hold_max
}
/// Builder-like function for holding the maximum reached position
///
/// See [`ProgressBarIter::set_hold_max()`].
pub fn with_hold_max(mut self, hold_max: bool) -> Self {
self.hold_max = hold_max;
self
}
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<W: tokio::io::AsyncWrite + Unpin> tokio::io::AsyncWrite for ProgressBarIter<W> {
Expand Down Expand Up @@ -323,7 +344,11 @@ impl<W: io::Write> io::Write for ProgressBarIter<W> {

impl<S, T: Iterator<Item = S>> ProgressIterator for T {
fn progress_with(self, progress: ProgressBar) -> ProgressBarIter<Self> {
ProgressBarIter { it: self, progress }
ProgressBarIter {
it: self,
progress,
hold_max: false,
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ impl ProgressBar {
ProgressBarIter {
progress: self.clone(),
it: read,
hold_max: false,
}
}

Expand All @@ -468,6 +469,7 @@ impl ProgressBar {
ProgressBarIter {
progress: self.clone(),
it: write,
hold_max: false,
}
}

Expand All @@ -494,6 +496,7 @@ impl ProgressBar {
ProgressBarIter {
progress: self.clone(),
it: write,
hold_max: false,
}
}

Expand All @@ -517,6 +520,7 @@ impl ProgressBar {
ProgressBarIter {
progress: self.clone(),
it: read,
hold_max: false,
}
}

Expand All @@ -539,6 +543,7 @@ impl ProgressBar {
ProgressBarIter {
progress: self.clone(),
it: stream,
hold_max: false,
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/rayon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ where

impl<S: Send, T: ParallelIterator<Item = S>> ParallelProgressIterator for T {
fn progress_with(self, progress: ProgressBar) -> ProgressBarIter<Self> {
ProgressBarIter { it: self, progress }
ProgressBarIter {
it: self,
progress,
hold_max: false,
}
}
}

Expand Down Expand Up @@ -99,6 +103,7 @@ impl<T, P: Producer<Item = T>> Producer for ProgressProducer<P> {
ProgressBarIter {
it: self.base.into_iter(),
progress: self.progress,
hold_max: false,
}
}

Expand Down