Skip to content

Commit

Permalink
Make sure with_style() and set_style() behave the same
Browse files Browse the repository at this point in the history
In #395, we made it so that message and prefix are carried over
when calling ProgressBar::with_style(), but this did not change
ProgressBar::set_style(). Reuse the same logic by having
with_style() call set_style(), so that these cannot fall out of
sync again.
  • Loading branch information
djc committed Mar 21, 2022
1 parent 4bcd612 commit 4dfb8d4
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/progress_bar.rs
Expand Up @@ -60,13 +60,8 @@ impl ProgressBar {
}

/// A convenience builder-like function for a progress bar with a given style
pub fn with_style(self, mut style: ProgressStyle) -> ProgressBar {
let mut state = self.state();
mem::swap(&mut state.style.message, &mut style.message);
mem::swap(&mut state.style.prefix, &mut style.prefix);
state.style = style;
drop(state);

pub fn with_style(self, style: ProgressStyle) -> ProgressBar {
self.set_style(style);
self
}

Expand Down Expand Up @@ -122,8 +117,11 @@ impl ProgressBar {
/// Overrides the stored style
///
/// This does not redraw the bar. Call [`ProgressBar::tick()`] to force it.
pub fn set_style(&self, style: ProgressStyle) {
self.state().style = style;
pub fn set_style(&self, mut style: ProgressStyle) {
let mut state = self.state();
mem::swap(&mut state.style.message, &mut style.message);
mem::swap(&mut state.style.prefix, &mut style.prefix);
state.style = style;
}

/// Spawns a background thread to tick the progress bar
Expand Down

0 comments on commit 4dfb8d4

Please sign in to comment.