Skip to content

Commit

Permalink
Implement width on Drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
jaheba committed Jan 13, 2025
1 parent b9137fd commit 42ba810
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 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 {
Drawable::Term { term, .. } => Some(term.size().1),
Drawable::Multi { state, .. } => state.width(),
Drawable::TermLike { term_like, .. } => Some(term_like.width()),
}
}
}

pub(crate) enum LineAdjust {
Expand Down
17 changes: 7 additions & 10 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,14 @@ impl BarState {
// finished progress bars, so it's kept as to not cause compatibility issues in weird cases.
force_draw |= self.state.is_finished();

// 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();
// let width = self.draw_target.width();

let mut drawable = match self.draw_target.drawable(force_draw, now) {
Some(drawable) => drawable,
None => return Ok(()),
};

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

let mut draw_state = drawable.state();

Expand All @@ -206,7 +204,6 @@ impl BarState {
.format_state(&self.state, &mut draw_state.lines, width);
}
}

drop(draw_state);

drawable.draw()
Expand Down

0 comments on commit 42ba810

Please sign in to comment.