use std::thread::sleep; use std::time::Duration; use console::Term; use crate::state::State; pub mod action; pub mod action_raw; pub mod download_display; pub mod download_pb; pub mod estimator; pub mod handlers; pub mod multibar; pub mod nix_path; pub mod state; pub mod state_manager; pub fn pad_string(s: &str, width: usize) -> String { if s.len() >= width { s.to_string() } else { let mut padded = s.to_string(); padded.push_str(&" ".repeat(width - s.len())); padded } } fn main() -> Result<(), color_eyre::Report> { color_eyre::install().unwrap(); let lines = std::fs::read_to_string("build.log")?; let mut state = State::new(Term::stderr().size().1); // let lines = lines.lines().take(0).collect::>().join("\n"); // let term = Term::stdout(); // let mut state = State { // multi_progress: MultiProgress::new(), // manager: StateManager::new(), // separator: None, // width: term.size().1, // current_width: term.size().1, // term, // }; for line in lines.lines() { let line = line.strip_prefix("@nix ").unwrap_or(line); sleep(Duration::from_nanos(500)); let action = action::Action::parse(line)?; state.handle(&action)?; // match action { // action::Action::Msg { level, msg } => { // state.println(format!("MSG (level {level}): {msg}"))?; // } // action::Action::Start { // start_type, // id, // level: _, // parent, // text: _, // } => { // // state.progress.println(format!("START {start_type:?}")); // if let Some(parent) = state.manager.get(*parent) { // let mut child = parent.clone(); // child.merge(&mut state, start_type); // child.tick(&mut state); // state.manager.insert_parent(*id, child); // }; // // match start_type { // // StartFields::Substitute { source, target } => { // // state // // .manager // // .insert_parent(*id, BuildState::new(Some(source.to_string()), None)); // // // // let build_state = state.manager.get_mut(*id).unwrap(); // // build_state.state = BuildEnumState::Substituting; // // } // // StartFields::CopyPath { // // path, // // origin, // // destination, // // } => { // // state.manager.add_child(*id, *parent); // // } // // StartFields::QueryPathInfo { path, source } => { // // state.progress.println(format!( // // "START QueryPathInfo (id: {}, parent: {}): path={}", // // id, parent, path // // ))?; // // } // // StartFields::FileTransfer { target } => { // // // state.progress.println(format!( // // // "START FileTransfer (id: {}, parent: {}): target={}", // // // id, parent, target // // // ))?; // // if let Some(parent) = state.manager.get(*parent) { // // state // // .manager // // .insert_parent(*id, BuildState::new(parent.path.clone(), None)); // // let build_state = state.manager.get_mut(*id).unwrap(); // // build_state.state = BuildEnumState::Downloading; // // }; // // // // // state.manager.add_child(*id, *parent); // // // Add child ID mapping to parent // // } // // _ => {} // // }; // } // action::Action::Stop { id } => { // state.manager.remove(*id); // // Stop will only return Some when the last reference is stopped // // if let Some(build_state) = state.manager.stop(*id) { // // if let Some(pb) = &build_state.progress_bar { // // pb.finish_and_clear(); // // state.progress.remove(pb); // // } // // state.progress.println(format!( // // "Completed: {}", // // build_state.path.as_deref().unwrap_or("unknown") // // ))?; // // } // } // action::Action::Result { id, fields } => match fields { // action::ResultFields::FetchStatus(status) => { // state.println(format!( // "RESULT FetchStatus (id: {}): status={}", // id, status // ))?; // } // action::ResultFields::Progress { // done, // expected, // running, // failed, // } => { // if expected == 0 { // continue; // }; // if let Some(mut child) = state.manager.take(*id) { // child.progress(&mut state, done, expected); // state.manager.insert_parent(*id, child); // } // // sleep(Duration::from_millis(1)); // // if let Some(build_state) = state.manager.get_mut(*id) // // && expected > 0 // // { // // let percentage = if expected == 0 { // // 0 // // } else { // // (done * 100 / expected) as u64 // // }; // // match &build_state.progress_bar { // // Some(pb) => { // // if percentage > pb.position() { // // pb.set_position(percentage); // // }; // // } // // None => { // // state.progress.println(format!( // // "Creating progress bar for id {} (done: {}, expected: {})", // // id, done, expected // // ))?; // // let n = match build_state.state { // // BuildEnumState::Downloading => "Downloading", // // BuildEnumState::Substituting => "Substituting", // // _ => "Processing", // // }; // // let name = // // nix_path::extract_full_name(build_state.path.as_ref().unwrap()); // // let pb = state.progress.add( // // ProgressBar::new(100) // // .with_style( // // ProgressStyle::default_bar() // // .template("{msg} [{bar:40.cyan/blue}] {pos:>3}%") // // .unwrap(), // // ) // // .with_message(format!( // // "{n} {}", // // name.as_deref().unwrap_or("unknown") // // )), // // ); // // pb.set_position(percentage); // // build_state.progress_bar = Some(pb); // // } // // }; // // } else { // // // state.progress.println(format!( // // // "RESULT Progress (id: {}): done={}, expected={}, running={}, failed={}", // // // id, done, expected, running, failed // // // ))?; // // }; // } // _ => {} // }, // } } Ok(()) }