Skip to content

Commit

Permalink
Only get draw_target-width when we actually draw
Browse files Browse the repository at this point in the history
  • Loading branch information
jaheba committed Jan 13, 2025
1 parent 6417492 commit 75f32e4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,28 @@ impl BarState {
}

pub(crate) fn draw(&mut self, mut force_draw: bool, now: Instant) -> io::Result<()> {
let width = self.draw_target.width();

// `|= self.is_finished()` should not be needed here, but we used to always draw for
// finished progress bars, so it's kept as to not cause compatibility issues in weird cases.
force_draw |= self.state.is_finished();
let mut drawable = match self.draw_target.drawable(force_draw, now) {
Some(drawable) => drawable,
None => return Ok(()),
};

let mut draw_state = drawable.state();
// First check whether there is something to be drawn.
if self.draw_target.drawable(force_draw, now).is_none() {
return Ok(());
}
// Then, get the target width.
// This is an expensive operation, and should only happen when we actually do some work.
let width = self.draw_target.width();

// Now actually get the drawable.
let mut drawable = self.draw_target.drawable(force_draw, now).unwrap();

if let Some(width) = width {
if !matches!(self.state.status, Status::DoneHidden) {
self.style
.format_state(&self.state, &mut draw_state.lines, width);
.format_state(&self.state, &mut drawable.state().lines, width);
}
}

drop(draw_state);
drawable.draw()
}
}
Expand Down

0 comments on commit 75f32e4

Please sign in to comment.