From 33be555f1e6c5141c5ee756a1d42b7ea0762f975 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 9 Aug 2020 10:37:21 +0800 Subject: [PATCH] pass elapsed time on in line renderer --- src/line/draw.rs | 6 +++++- src/line/engine.rs | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/line/draw.rs b/src/line/draw.rs index e4720bf..dc8fc48 100644 --- a/src/line/draw.rs +++ b/src/line/draw.rs @@ -17,6 +17,8 @@ pub struct State { last_progress_midpoint: Option, /// The amount of blocks per line we have written last time. blocks_per_line: VecDeque, + /// The elapsed time between draw requests. None on the first draw + pub elapsed: Option, } pub struct Options { @@ -143,6 +145,7 @@ pub fn all( config.terminal_dimensions.0, config.colored, state.last_progress_midpoint, + state.elapsed, &mut tokens, ) .unwrap_or(0), @@ -261,6 +264,7 @@ fn format_progress<'a>( column_count: u16, colored: bool, midpoint: Option, + elapsed: Option, buf: &mut Vec>, ) -> Option { let mut brush = color::Brush::new(colored); @@ -277,7 +281,7 @@ fn format_progress<'a>( let values_brush = brush.style(Style::new().bold().dimmed()); match progress.unit.as_ref() { Some(unit) => { - let mut display = unit.display(progress.step, progress.done_at, None); + let mut display = unit.display(progress.step, progress.done_at, elapsed); buf.push(values_brush.paint(format!("{}", display.values()))); buf.push(" ".into()); buf.push(display.unit().to_string().into()); diff --git a/src/line/engine.rs b/src/line/engine.rs index d071340..7467672 100644 --- a/src/line/engine.rs +++ b/src/line/engine.rs @@ -172,7 +172,9 @@ pub fn render(mut out: impl io::Write + Send + 'static, progress: tree::Root, co std::thread::sleep(Duration::from_secs_f32(secs)); }); + let mut time_of_previous_draw_request = None::; for event in event_recv { + state.elapsed = time_of_previous_draw_request.as_ref().and_then(|t| t.elapsed().ok()); match event { Event::Tick => { draw::all( @@ -185,6 +187,7 @@ pub fn render(mut out: impl io::Write + Send + 'static, progress: tree::Root, co } Event::Quit => break, } + time_of_previous_draw_request = Some(std::time::SystemTime::now()) } if show_cursor {