From b4e91f5f5ecd3ac6f304cc980df4f4cd2bc134ab Mon Sep 17 00:00:00 2001 From: djugei Date: Thu, 2 May 2024 11:28:36 +0200 Subject: [PATCH 1/2] introduce the hold_max option --- src/iter.rs | 27 ++++++++++++++++++++++++++- src/progress_bar.rs | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/iter.rs b/src/iter.rs index 8861db52..2e0ba38b 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -62,6 +62,7 @@ where pub struct ProgressBarIter { pub(crate) it: T, pub progress: ProgressBar, + pub(crate) hold_max: bool, } impl ProgressBarIter { @@ -192,6 +193,11 @@ impl io::BufRead for ProgressBarIter { impl io::Seek for ProgressBarIter { fn seek(&mut self, f: io::SeekFrom) -> io::Result { 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 }) @@ -203,6 +209,21 @@ impl io::Seek for ProgressBarIter { } } +impl ProgressBarIter { + /// 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 tokio::io::AsyncWrite for ProgressBarIter { @@ -323,7 +344,11 @@ impl io::Write for ProgressBarIter { impl> ProgressIterator for T { fn progress_with(self, progress: ProgressBar) -> ProgressBarIter { - ProgressBarIter { it: self, progress } + ProgressBarIter { + it: self, + progress, + hold_max: false, + } } } diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 036ab1ef..f13b06c0 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -447,6 +447,7 @@ impl ProgressBar { ProgressBarIter { progress: self.clone(), it: read, + hold_max: false, } } @@ -468,6 +469,7 @@ impl ProgressBar { ProgressBarIter { progress: self.clone(), it: write, + hold_max: false, } } From ddd70b092e37222daf80c43f7dbf11382f2e521d Mon Sep 17 00:00:00 2001 From: djugei Date: Thu, 2 May 2024 11:51:46 +0200 Subject: [PATCH 2/2] fix failing tests in non-default features --- src/progress_bar.rs | 3 +++ src/rayon.rs | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/progress_bar.rs b/src/progress_bar.rs index f13b06c0..5231b790 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -496,6 +496,7 @@ impl ProgressBar { ProgressBarIter { progress: self.clone(), it: write, + hold_max: false, } } @@ -519,6 +520,7 @@ impl ProgressBar { ProgressBarIter { progress: self.clone(), it: read, + hold_max: false, } } @@ -541,6 +543,7 @@ impl ProgressBar { ProgressBarIter { progress: self.clone(), it: stream, + hold_max: false, } } diff --git a/src/rayon.rs b/src/rayon.rs index 1c8e844f..737438fd 100644 --- a/src/rayon.rs +++ b/src/rayon.rs @@ -41,7 +41,11 @@ where impl> ParallelProgressIterator for T { fn progress_with(self, progress: ProgressBar) -> ProgressBarIter { - ProgressBarIter { it: self, progress } + ProgressBarIter { + it: self, + progress, + hold_max: false, + } } } @@ -99,6 +103,7 @@ impl> Producer for ProgressProducer

{ ProgressBarIter { it: self.base.into_iter(), progress: self.progress, + hold_max: false, } }