Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 9, 2020
1 parent 6a4ae4e commit 9212de4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/line/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crosstermion::{
ansi_term::{ANSIString, ANSIStrings, Color, Style},
color,
};
use std::{collections::VecDeque, io, iter::FromIterator, ops::RangeInclusive, time::Duration};
use std::{collections::VecDeque, io, iter::FromIterator, ops::RangeInclusive};
use unicode_width::UnicodeWidthStr;

#[derive(Default)]
Expand All @@ -17,8 +17,6 @@ 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<Duration>,
pub throughput: Option<tree::Throughput>,
}

Expand Down Expand Up @@ -119,6 +117,9 @@ pub fn all(
)?;

if show_progress && config.output_is_terminal {
if let Some(tp) = state.throughput.as_mut() {
tp.update_elapsed();
}
let level_range = config
.level_filter
.clone()
Expand All @@ -133,7 +134,6 @@ pub fn all(
}
let mut tokens: Vec<ANSIString<'_>> = Vec::with_capacity(4);
let mut max_midpoint = 0;
let elapsed = state.elapsed;
for ((key, progress), ref mut blocks_in_last_iteration) in state
.tree
.iter()
Expand All @@ -150,7 +150,7 @@ pub fn all(
state
.throughput
.as_mut()
.and_then(|tp| elapsed.and_then(|elapsed| tp.update_and_get(key, progress, elapsed))),
.and_then(|tp| tp.update_and_get(key, progress)),
&mut tokens,
)
.unwrap_or(0),
Expand Down
6 changes: 1 addition & 5 deletions src/line/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
io,
ops::RangeInclusive,
sync::atomic::{AtomicBool, Ordering},
time::{Duration, SystemTime},
time::Duration,
};

#[derive(Clone)]
Expand Down Expand Up @@ -184,11 +184,7 @@ 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::<SystemTime>;
for event in event_recv {
let now = SystemTime::now();
state.elapsed = time_of_previous_draw_request.and_then(|then| now.duration_since(then).ok());
time_of_previous_draw_request = Some(now);
match event {
Event::Tick => {
draw::all(
Expand Down
41 changes: 22 additions & 19 deletions src/tree/throughput.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{progress, tree, unit};
use std::time::Duration;
use std::time::{Duration, SystemTime};

const THROTTLE_INTERVAL: Duration = Duration::from_secs(1);

Expand Down Expand Up @@ -57,27 +57,30 @@ impl State {
#[derive(Default)]
pub struct Throughput {
sorted_by_key: Vec<(tree::Key, State)>,
updated_at: Option<SystemTime>,
elapsed: Option<Duration>,
}

impl Throughput {
pub fn update_and_get(
&mut self,
key: &tree::Key,
value: &progress::Value,
elapsed: Duration,
) -> Option<unit::display::Throughput> {
value
.progress
.as_ref()
.and_then(|progress| match self.sorted_by_key.binary_search_by_key(key, |t| t.0) {
Ok(index) => self.sorted_by_key[index].1.update(progress.step, elapsed),
Err(index) => {
let state = State::new(progress.step, elapsed);
let tp = state.throughput();
self.sorted_by_key.insert(index, (*key, state));
tp
}
})
pub fn update_elapsed(&mut self) {
let now = SystemTime::now();
self.elapsed = self.updated_at.and_then(|then| now.duration_since(then).ok());
self.updated_at = Some(now);
}

pub fn update_and_get(&mut self, key: &tree::Key, value: &progress::Value) -> Option<unit::display::Throughput> {
value.progress.as_ref().and_then(|progress| {
self.elapsed
.and_then(|elapsed| match self.sorted_by_key.binary_search_by_key(key, |t| t.0) {
Ok(index) => self.sorted_by_key[index].1.update(progress.step, elapsed),
Err(index) => {
let state = State::new(progress.step, elapsed);
let tp = state.throughput();
self.sorted_by_key.insert(index, (*key, state));
tp
}
})
})
}
pub fn reconcile(&mut self, sorted_values: &[(tree::Key, progress::Value)]) {
self.sorted_by_key
Expand Down
10 changes: 10 additions & 0 deletions src/tui/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub struct Options {
///
/// *e.g.* 1.0/4.0 is one frame every 4 seconds.
pub frames_per_second: f32,

/// If true, (default false), we will keep track of the previous progress state to derive
/// continuous throughput information from. Throughput will only show for units which have
/// explicitly enabled it, it is opt-in.
///
/// This comes at the cost of additional memory and CPU time.
pub throughput: bool,

/// If set, recompute the column width of the task tree only every given frame. Otherwise the width will be recomputed every frame.
///
/// Use this if there are many short-running tasks with varying names paired with high refresh rates of multiple frames per second to
Expand All @@ -44,6 +52,7 @@ impl Default for Options {
Options {
title: "Progress Dashboard".into(),
frames_per_second: 10.0,
throughput: false,
recompute_column_width_every_nth_frame: None,
window_size: None,
stop_if_empty_progress: false,
Expand Down Expand Up @@ -129,6 +138,7 @@ pub fn render_with_input(
frames_per_second,
window_size,
recompute_column_width_every_nth_frame,
throughput,
stop_if_empty_progress,
} = options;
let mut terminal = new_terminal(AlternateRawScreen::try_from(out)?)?;
Expand Down

0 comments on commit 9212de4

Please sign in to comment.