Skip to content

Commit

Permalink
pass elapsed time on in line renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 9, 2020
1 parent a4b4ab7 commit 33be555
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/line/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct State {
last_progress_midpoint: Option<u16>,
/// The amount of blocks per line we have written last time.
blocks_per_line: VecDeque<u16>,
/// The elapsed time between draw requests. None on the first draw
pub elapsed: Option<std::time::Duration>,
}

pub struct Options {
Expand Down Expand Up @@ -143,6 +145,7 @@ pub fn all(
config.terminal_dimensions.0,
config.colored,
state.last_progress_midpoint,
state.elapsed,
&mut tokens,
)
.unwrap_or(0),
Expand Down Expand Up @@ -261,6 +264,7 @@ fn format_progress<'a>(
column_count: u16,
colored: bool,
midpoint: Option<u16>,
elapsed: Option<std::time::Duration>,
buf: &mut Vec<ANSIString<'a>>,
) -> Option<u16> {
let mut brush = color::Brush::new(colored);
Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/line/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<std::time::SystemTime>;
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(
Expand 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 {
Expand Down

0 comments on commit 33be555

Please sign in to comment.