use std::sync::Arc;
use super::multi_progress_report::MultiProgressReport;
use super::progress_report::SingleReport;
use super::text_install_progress::{Action, State, TextInstallProgress};
use super::tty_install_progress::TtyInstallProgress;
pub(crate) trait ToolProgress: SingleReport {
fn set_prefix(&self, prefix: String);
fn complete(&self, error: Option<&str>);
fn reporter(&self) -> Box<dyn SingleReport>;
}
pub(crate) trait InstallProgress: Send + Sync {
fn start_tool(&self, key: &str) -> Option<Box<dyn ToolProgress>>;
fn queue_tool(&self, key: &str);
fn set_waiting(&self, key: &str, dependencies: Vec<String>);
fn finish(&mut self, failures: Vec<(String, String)>);
}
pub(crate) fn install_progress(
mpr: &Arc<MultiProgressReport>,
tools: impl Iterator<Item = (String, String)>,
) -> Option<Box<dyn InstallProgress>> {
progress_for(mpr, Action::Install, tools)
}
pub(crate) fn resolution_progress(
tools: impl Iterator<Item = (String, String)>,
) -> Option<Box<dyn InstallProgress>> {
progress_for(&MultiProgressReport::get(), Action::Resolve, tools)
}
pub(crate) fn removal_progress(
mpr: &Arc<MultiProgressReport>,
tools: impl Iterator<Item = (String, String)>,
) -> Option<Box<dyn InstallProgress>> {
progress_for(mpr, Action::Remove, tools)
}
fn progress_for(
mpr: &Arc<MultiProgressReport>,
action: Action,
tools: impl Iterator<Item = (String, String)>,
) -> Option<Box<dyn InstallProgress>> {
let state = State::for_action(action, tools);
if state.is_empty() {
return None;
}
if mpr.use_tty_install_output() {
Some(Box::new(TtyInstallProgress::new(state)))
} else if mpr.use_text_install_output() {
Some(Box::new(TextInstallProgress::new(state)))
} else {
None
}
}