From b19ff1bb67b72726aef398464930f5376787fe1c Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:28:00 +0200 Subject: [PATCH 1/7] Add ProgressIterator::progress_with_style() Style a ProgressIterator without manually handling the length of the bar --- src/iter.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/iter.rs b/src/iter.rs index a0d562ec..40df7778 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -33,6 +33,16 @@ where /// Wrap an iterator with a custom progress bar. fn progress_with(self, progress: ProgressBar) -> ProgressBarIter; + + /// Wrap an iterator with a progress bar and style it. + fn progress_with_style(self, style: crate::ProgressStyle) -> ProgressBarIter + 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. @@ -164,6 +174,7 @@ impl> ProgressIterator for T { #[cfg(test)] mod test { + use crate::ProgressStyle; use crate::iter::{ProgressBarIter, ProgressIterator}; use crate::progress_bar::ProgressBar; @@ -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) + }); } } From 73bc80d6fc2cbbaabbaa8cf3be25e4a158048944 Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:30:06 +0200 Subject: [PATCH 2/7] Fix typo Changed "more then" to "more than" --- src/style.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/style.rs b/src/style.rs index bb49949d..5e02d040 100644 --- a/src/style.rs +++ b/src/style.rs @@ -119,7 +119,7 @@ impl ProgressStyle { /// Sets the progress characters `(filled, current, to do)` /// - /// You can pass more then three for a more detailed display. + /// You can pass more than three for a more detailed display. /// All passed grapheme clusters need to be of equal width. pub fn progress_chars(mut self, s: &str) -> ProgressStyle { self.progress_chars = segment(s); From 7d10dbf30e1ea658fee58ebf0fee474e0fd27b1f Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:34:13 +0200 Subject: [PATCH 3/7] Taking care of clippy lints Removing two needless borrows --- src/draw_target.rs | 2 +- src/state.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/draw_target.rs b/src/draw_target.rs index 62bcab8c..8f475b5b 100644 --- a/src/draw_target.rs +++ b/src/draw_target.rs @@ -305,7 +305,7 @@ impl MultiProgressState { let finished = !self .draw_states .iter() - .any(|ref x| !x.as_ref().map(|s| s.finished).unwrap_or(false)); + .any(|x| !x.as_ref().map(|s| s.finished).unwrap_or(false)); self.draw_target.apply_draw_state(ProgressDrawState { lines, orphan_lines: orphan_lines_count, diff --git a/src/state.rs b/src/state.rs index cc547c24..34fb73a2 100644 --- a/src/state.rs +++ b/src/state.rs @@ -239,7 +239,7 @@ impl ProgressState { } let lines = match self.should_render() { - true => self.style.format_state(&self), + true => self.style.format_state(self), false => Vec::new(), }; From 9f292fb41bd9da0884a95fa78072f8d3e176ac37 Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Mon, 9 Aug 2021 18:16:05 +0200 Subject: [PATCH 4/7] Add progress_with_style() to ParallelProgressIterator Styling iterators should also be possible for parallel iterators --- src/rayon.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/rayon.rs b/src/rayon.rs index 771e28fc..a65a90f0 100644 --- a/src/rayon.rs +++ b/src/rayon.rs @@ -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 + where + Self: IndexedParallelIterator, + { + let len = u64::try_from(self.len()).unwrap(); + let bar = ProgressBar::new(len).with_style(style); + self.progress_with(bar) + } } impl> ParallelProgressIterator for T { @@ -199,6 +209,7 @@ impl> ParallelIterator for ProgressBarIte #[cfg(test)] mod test { + use crate::ProgressStyle; use crate::{ParallelProgressIterator, ProgressBar, ProgressBarIter}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; @@ -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) + }); } } From 5d409a5cb4927e46bd94caf8a861289789ba0c18 Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Mon, 9 Aug 2021 18:23:27 +0200 Subject: [PATCH 5/7] Fix documentation Change snippet about styling ParallelProgressIterator --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b6edacb8..3e484e6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,13 +107,13 @@ //! //! ```rust,ignore //! # extern crate rayon; -//! use indicatif::{ProgressBar, ParallelProgressIterator}; +//! use indicatif::{ProgressBar, ParallelProgressIterator, ProgressStyle}; //! use rayon::iter::{ParallelIterator, IntoParallelRefIterator}; //! -//! // Use `ProgressBar::with_style` to change the view -//! let pb = ProgressBar::new(); +//! // Alternatively, use `ProgressBar::new().with_style()` +//! let style = ProgressStyle::default_bar(); //! let v: Vec<_> = (0..100000).collect(); -//! let v2: Vec<_> = v.par_iter().progress_with(pb).map(|i| i + 1).collect(); +//! let v2: Vec<_> = v.par_iter().progress_with_style(style).map(|i| i + 1).collect(); //! assert_eq!(v2[0], 1); //! ``` //! From 2043e1873c9b79f31c11336ae9fcb34ab4339c97 Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Wed, 11 Aug 2021 10:29:34 +0200 Subject: [PATCH 6/7] Taking care of clippy lints in examples Removing needless borrows --- examples/multi-tree-ext.rs | 2 +- examples/multi-tree.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/multi-tree-ext.rs b/examples/multi-tree-ext.rs index a3ee72ce..df15a648 100644 --- a/examples/multi-tree-ext.rs +++ b/examples/multi-tree-ext.rs @@ -224,7 +224,7 @@ pub fn main() { let pb = mp2.insert(item.index, item.progress_bar.clone()); pb.set_prefix(" ".repeat(item.indent)); pb.set_message(&item.key); - items.insert(item.index, &item); + items.insert(item.index, item); } Elem::RemoveItem(Index(index)) => { let item = items.remove(*index); diff --git a/examples/multi-tree.rs b/examples/multi-tree.rs index 459e6b09..ea88dddf 100644 --- a/examples/multi-tree.rs +++ b/examples/multi-tree.rs @@ -115,7 +115,7 @@ fn main() { let elem = &ELEMENTS[el_idx]; let pb = mp2.insert(elem.index + 1, elem.progress_bar.clone()); pb.set_message(format!("{} {}", " ".repeat(elem.indent), elem.key)); - tree.lock().unwrap().insert(elem.index, &elem); + tree.lock().unwrap().insert(elem.index, elem); } Some(Action::IncProgressBar(el_idx)) => { let elem = &tree.lock().unwrap()[el_idx]; From a450c1be30a231c6d77e15447a66d49c8c47e58e Mon Sep 17 00:00:00 2001 From: LeCyberDucky <19520383+LeCyberDucky@users.noreply.github.com> Date: Wed, 11 Aug 2021 10:53:17 +0200 Subject: [PATCH 7/7] Running cargo fmt to fix formatting --- src/iter.rs | 4 ++-- src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/iter.rs b/src/iter.rs index 40df7778..cc038779 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -36,7 +36,7 @@ where /// Wrap an iterator with a progress bar and style it. fn progress_with_style(self, style: crate::ProgressStyle) -> ProgressBarIter - where + where Self: ExactSizeIterator, { let len = u64::try_from(self.len()).unwrap(); @@ -174,9 +174,9 @@ impl> ProgressIterator for T { #[cfg(test)] mod test { - use crate::ProgressStyle; use crate::iter::{ProgressBarIter, ProgressIterator}; use crate::progress_bar::ProgressBar; + use crate::ProgressStyle; #[test] fn it_can_wrap_an_iterator() { diff --git a/src/lib.rs b/src/lib.rs index 3e484e6d..a62185aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,7 @@ //! use indicatif::{ProgressBar, ParallelProgressIterator, ProgressStyle}; //! use rayon::iter::{ParallelIterator, IntoParallelRefIterator}; //! -//! // Alternatively, use `ProgressBar::new().with_style()` +//! // Alternatively, use `ProgressBar::new().with_style()` //! let style = ProgressStyle::default_bar(); //! let v: Vec<_> = (0..100000).collect(); //! let v2: Vec<_> = v.par_iter().progress_with_style(style).map(|i| i + 1).collect();