From 75f32e4b73ea2a55e73e8ab4fcb025272a5858bc Mon Sep 17 00:00:00 2001 From: Jasper Zschiegner Date: Mon, 13 Jan 2025 11:13:20 +0100 Subject: [PATCH] Only get draw_target-width when we actually draw --- src/state.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/state.rs b/src/state.rs index 8b68e1ea..0ce25b7d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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() } }