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 authored and djc committed Jan 15, 2025
1 parent 6417492 commit 33a7843
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ impl Drawable<'_> {
} => draw_state.draw_to_term(term_like, last_line_count),
}
}

pub(crate) fn width(&self) -> Option<u16> {
match self {
Self::Term { term, .. } => Some(term.size().1),
Self::Multi { state, .. } => state.width(),
Self::TermLike { term_like, .. } => Some(term_like.width()),
}
}
}

pub(crate) enum LineAdjust {
Expand Down
5 changes: 3 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ 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();
Expand All @@ -193,6 +191,9 @@ impl BarState {
None => return Ok(()),
};

// Getting the width can be expensive; thus this should happen after checking drawable.
let width = drawable.width();

let mut draw_state = drawable.state();

if let Some(width) = width {
Expand Down

0 comments on commit 33a7843

Please sign in to comment.