Skip to content

Commit

Permalink
Remove ProgressDrawState::is_finished to simplify code
Browse files Browse the repository at this point in the history
is_finished has always been ORed with force_draw, and removing it
allows the logic to be simplified.
  • Loading branch information
ishitatsuyuki committed Dec 15, 2021
1 parent 88f403b commit 0f8750a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
20 changes: 5 additions & 15 deletions src/draw_target.rs
Expand Up @@ -152,7 +152,7 @@ impl ProgressDrawTarget {
ref mut last_line_count,
leaky_bucket: Some(ref mut leaky_bucket),
} => {
if draw_state.finished || draw_state.force_draw || leaky_bucket.try_add_work() {
if draw_state.force_draw || leaky_bucket.try_add_work() {
(term, last_line_count)
} else {
// rate limited
Expand Down Expand Up @@ -205,8 +205,7 @@ impl ProgressDrawTarget {
ProgressDrawState {
lines: vec![],
orphan_lines: 0,
finished: true,
force_draw: false,
force_draw: true,
move_cursor: false,
alignment: Default::default(),
},
Expand Down Expand Up @@ -262,7 +261,7 @@ impl MultiProgressState {
}

pub(crate) fn draw(&mut self, idx: usize, draw_state: ProgressDrawState) -> io::Result<()> {
let force_draw = draw_state.finished || draw_state.force_draw;
let force_draw = draw_state.force_draw;
let mut orphan_lines = vec![];

// Split orphan lines out of the draw state, if any
Expand Down Expand Up @@ -301,18 +300,12 @@ impl MultiProgressState {
}
}

// !any(!done) is also true when iter() is empty, contrary to all(done)
let finished = !self
.draw_states
.iter()
.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,
force_draw: force_draw || orphan_lines_count > 0,
move_cursor: self.move_cursor,
alignment: self.alignment,
finished,
})
}

Expand Down Expand Up @@ -376,8 +369,6 @@ pub(crate) struct ProgressDrawState {
pub lines: Vec<String>,
/// The number of lines that shouldn't be reaped by the next tick.
pub orphan_lines: usize,
/// True if the bar no longer needs drawing.
pub finished: bool,
/// True if drawing should be forced.
pub force_draw: bool,
/// True if we should move the cursor up when possible instead of clearing lines.
Expand All @@ -387,12 +378,11 @@ pub(crate) struct ProgressDrawState {
}

impl ProgressDrawState {
pub(crate) fn new(lines: Vec<String>, finished: bool) -> Self {
pub(crate) fn new(lines: Vec<String>, force_draw: bool) -> Self {
Self {
lines,
orphan_lines: 0,
finished,
force_draw: false,
force_draw,
move_cursor: false,
alignment: Default::default(),
}
Expand Down
4 changes: 1 addition & 3 deletions src/progress_bar.rs
Expand Up @@ -138,7 +138,7 @@ impl ProgressBar {
}
ms = state.steady_tick;

state.draw().ok();
state.draw(false).ok();
} else {
break;
}
Expand Down Expand Up @@ -248,7 +248,6 @@ impl ProgressBar {
let draw_state = ProgressDrawState {
lines,
orphan_lines,
finished: state.is_finished(),
force_draw: true,
move_cursor: false,
alignment: Default::default(),
Expand Down Expand Up @@ -696,7 +695,6 @@ impl MultiProgress {
state.draw_target.apply_draw_state(ProgressDrawState {
lines: vec![],
orphan_lines: 0,
finished: true,
force_draw: true,
move_cursor,
alignment,
Expand Down
10 changes: 6 additions & 4 deletions src/state.rs
Expand Up @@ -124,7 +124,7 @@ impl ProgressState {
/// progress bar if the state has changed.
pub fn update_and_draw<F: FnOnce(&mut ProgressState)>(&mut self, f: F) {
if self.update(f) {
self.draw().ok();
self.draw(false).ok();
}
}

Expand All @@ -135,7 +135,7 @@ impl ProgressState {
state.draw_next = state.pos;
f(state);
});
self.draw().ok();
self.draw(true).ok();
}

/// Call the provided `FnOnce` to update the state. If a draw should be run, returns `true`.
Expand Down Expand Up @@ -228,7 +228,7 @@ impl ProgressState {
}
}

pub(crate) fn draw(&mut self) -> io::Result<()> {
pub(crate) fn draw(&mut self, force_draw: bool) -> io::Result<()> {
// we can bail early if the draw target is hidden.
if self.draw_target.is_hidden() {
return Ok(());
Expand All @@ -239,7 +239,9 @@ impl ProgressState {
false => Vec::new(),
};

let draw_state = ProgressDrawState::new(lines, self.is_finished());
// `|| self.is_finished()` should not be needed here, but we used to always for draw for
// finished progress bar, so it's kept as to not cause compatibility issues in weird cases.
let draw_state = ProgressDrawState::new(lines, force_draw || self.is_finished());
self.draw_target.apply_draw_state(draw_state)
}
}
Expand Down

0 comments on commit 0f8750a

Please sign in to comment.