start adding more status lines

This commit is contained in:
2025-11-14 15:23:04 +04:00
parent 632fdeb867
commit 79f5e52a73
4 changed files with 168 additions and 9 deletions

View File

@@ -0,0 +1,161 @@
use std::{collections::HashMap, sync::LazyLock, time::Instant};
use indicatif::{HumanBytes, ProgressBar};
use owo_colors::OwoColorize;
use crate::{
action::{Action, ActionType, BuildStepId, ResultFields, StartFields},
estimator::Estimator,
handlers::{Handler, fetch::FetchHandler},
multibar::{BarSegment, MultiBar},
nix_path, pad_string,
};
static DOWNLOAD_CHAR: LazyLock<String> = LazyLock::new(|| "-".blue().bold().to_string());
static DONE_CHAR: LazyLock<String> = LazyLock::new(|| "#".green().bold().to_string());
const INFO_WIDTH: usize = 13;
pub struct CopyPathsHandler;
impl Handler for CopyPathsHandler {
fn handle(
&mut self,
state: &mut crate::state::State,
action: &crate::action::Action,
) -> color_eyre::Result<bool> {
match action {
Action::Start {
start_type: StartFields::CopyPaths,
id,
..
} => {
state.println(format!("CopyPaths start"))?;
let progress = state.add_pb(ProgressBar::new(1));
state.plug(SubstitutionStatusHandler::new(
id,
state.term_width,
progress,
));
// Handle global start action
Ok(true)
}
_ => Ok(true),
}
}
}
pub struct SubstitutionStatusHandler {
id: BuildStepId,
progress: ProgressBar,
state_copy: HashMap<BuildStepId, [u64; 2]>,
state_transfer: HashMap<BuildStepId, [u64; 2]>,
state_self: [u64; 2],
max_copy: u64,
max_transfer: u64,
}
impl SubstitutionStatusHandler {
fn new(id: &BuildStepId, width: u16, progress: ProgressBar) -> Self {
let mut new = Self {
id: *id,
progress,
state_copy: HashMap::new(),
state_transfer: HashMap::new(),
state_self: [0, 0],
max_copy: 0,
max_transfer: 0,
};
new.draw_bar(width);
new
}
fn get_done(&self) -> u64 {
self.state_transfer.values().map(|&[done, ..]| done).sum()
}
fn get_running(&self) -> u64 {
self.state_transfer
.values()
.map(|&[_, expected, ..]| expected)
.sum()
}
fn get_unpacked(&self) -> u64 {
self.state_copy.values().map(|&[done, ..]| done).sum()
}
fn draw_bar(&mut self, width: u16) {}
}
impl Handler for SubstitutionStatusHandler {
fn handle(
&mut self,
state: &mut crate::state::State,
action: &crate::action::Action,
) -> color_eyre::Result<bool> {
match action {
Action::Start {
start_type: StartFields::CopyPath { path, .. },
id,
..
} => {
self.state_copy.insert(*id, [0; 2]);
Ok(true)
}
Action::Start {
start_type: StartFields::FileTransfer { .. },
id,
..
} => {
self.state_transfer.insert(*id, [0; 2]);
Ok(true)
}
Action::Result {
id,
fields:
ResultFields::SetExpected {
action: ActionType::CopyPaths,
expected,
},
} => {
self.max_copy = *expected;
self.draw_bar(state.term_width);
Ok(true)
}
Action::Result {
id,
fields:
ResultFields::SetExpected {
action: ActionType::CopyPaths,
expected,
},
} => {
self.max_copy = *expected;
self.draw_bar(state.term_width);
Ok(true)
}
Action::Result {
id,
fields: ResultFields::Progress { done, expected, .. },
} => {
if id == &self.id {
self.state_self = [*done, *expected];
self.draw_bar(state.term_width);
}
if let Some(copy) = self.state_copy.get_mut(id) {
*copy = [*done, *expected];
self.draw_bar(state.term_width);
}
if let Some(transfer) = self.state_transfer.get_mut(id) {
*transfer = [*done, *expected];
self.draw_bar(state.term_width);
}
Ok(true)
}
_ => Ok(true),
}
}
}