diff --git a/src/handlers/substitute_status.rs b/src/handlers/substitute_status.rs index b63e3e1..e5f48cf 100644 --- a/src/handlers/substitute_status.rs +++ b/src/handlers/substitute_status.rs @@ -5,7 +5,7 @@ use owo_colors::{OwoColorize, Style}; use crate::{ action::{Action, ActionType, BuildStepId, ResultFields, StartFields}, - download_display::{StyledString, style_bar}, + download_display::{DOWNLOAD_STYLE, EXTRACT_STYLE, StyledString, style_bar}, estimator::Estimator, handlers::{Handler, fetch::FetchHandler}, multibar::{BarSegment, MultiBar}, @@ -50,6 +50,8 @@ impl Handler for CopyPathsHandler { pub struct SubstitutionStatusHandler { id: BuildStepId, progress: ProgressBar, + download_estimator: Estimator, + extract_estimator: Estimator, state_copy: HashMap, state_transfer: HashMap, state_self: [u64; 2], @@ -62,6 +64,8 @@ impl SubstitutionStatusHandler { let mut new = Self { id: *id, progress, + download_estimator: Estimator::new(Instant::now()), + extract_estimator: Estimator::new(Instant::now()), state_copy: HashMap::new(), state_transfer: HashMap::new(), state_self: [0, 0], @@ -93,6 +97,7 @@ impl SubstitutionStatusHandler { } let dl_percent = ((self.get_done() as f64 / self.max_transfer as f64) * 100.0) as u64; let ex_percent = ((self.get_unpacked() as f64 / self.max_copy as f64) * 100.0) as u64; + let expected = ((self.get_running() as f64 / self.get_done() as f64) * 100.0) as u64; let min = dl_percent.min(ex_percent); let dl = dl_percent.saturating_sub(min); let mbar = MultiBar([ @@ -100,13 +105,34 @@ impl SubstitutionStatusHandler { BarSegment::Dynamic(&DOWNLOAD_CHAR, dl), BarSegment::Dynamic(" ", 100 - min - dl), ]); + + let work_per_sec = if self.get_done() < self.max_transfer { + StyledString::new( + Cow::Owned(format!( + "{}", + HumanBytes(self.download_estimator.steps_per_second(Instant::now()) as u64) + )), + DOWNLOAD_STYLE, + ) + } else { + StyledString::new( + Cow::Owned(format!( + "{}", + HumanBytes(self.extract_estimator.steps_per_second(Instant::now()) as u64) + )), + EXTRACT_STYLE, + ) + }; let msg = style_bar( StyledString::new(Cow::Owned("Downloading".to_string()), Style::new()), StyledString::new( - Cow::Owned(format!("{}/{}", self.state_self[0], self.state_self[1])), + Cow::Owned(format!( + "{}/{} {}", + self.state_self[0], self.state_self[1], expected + )), Style::new().purple(), ), - StyledString::new(Cow::Owned(format!("awa")), Style::new().yellow()), + work_per_sec, self.get_done() as usize, self.get_unpacked() as usize, mbar, @@ -176,11 +202,16 @@ impl Handler for SubstitutionStatusHandler { } if let Some(copy) = self.state_copy.get_mut(id) { *copy = [*done, *expected]; + self.extract_estimator + .record(self.get_unpacked(), Instant::now()); self.draw_bar(state.term_width); } if let Some(transfer) = self.state_transfer.get_mut(id) { *transfer = [*done, *expected]; + + self.download_estimator + .record(self.get_done(), Instant::now()); self.draw_bar(state.term_width); } Ok(true) diff --git a/src/main.rs b/src/main.rs index ac6393a..a4528f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ fn main() -> Result<(), color_eyre::Report> { for line in lines.lines() { let line = line.strip_prefix("@nix ").unwrap_or(line); - sleep(Duration::from_nanos(500)); + sleep(Duration::from_millis(1)); let action = action::Action::parse(line)?; state.handle(&action)?; // match action {