use std::io::Write as _;
use std::path::Path;
use anyhow::Result;
use openmoat_hosts::Host;
use super::preview::{self, Change};
use crate::home::Home;
use crate::install::{HookState, HostConfig};
use crate::render::{Deferred, clean};
use crate::report::{Mark, Paths, Style};
use crate::sandbox::Plan;
struct Found<'a> {
config: &'a HostConfig,
dir: String,
changes: Vec<Change>,
what: Option<String>,
}
pub(super) struct Asking<'a> {
pub plan: Option<&'a Plan>,
pub verbose: bool,
pub every_agent: bool,
}
pub(super) fn ask(
home: &Home,
found: &[HostConfig],
binary: &Path,
asking: &Asking<'_>,
out: &mut Deferred,
) -> Result<Vec<Host>> {
let (style, paths) = (Style::detect(), Paths::detect());
let verbose = asking.verbose;
let mut agents = Vec::new();
for config in found {
let changes = preview::changes(home, config, binary, asking.plan)?;
let what = if !asking.every_agent && preview::nothing_to_do(home, config, binary, &changes)?
{
None
} else if changes.is_empty() {
Some("no change to its files".to_owned())
} else {
let installed = config.state(binary) == HookState::Installed;
Some(preview::what(&changes, installed))
};
agents.push(Found {
config,
dir: paths.path(config.dir()),
changes,
what,
});
}
let widths = Widths {
name: width(agents.iter().map(|a| a.config.host.display_name())),
dir: width(agents.iter().map(|a| a.dir.as_str())),
what: width(agents.iter().filter_map(|a| a.what.as_deref())),
};
writeln!(out, "{}", style.dim(INTRO))?;
writeln!(out)?;
let mut chosen = Vec::new();
for agent in &agents {
let name = agent.config.host.display_name();
if verbose {
preview::summary(agent.config, &agent.changes, out)?;
}
let line = widths.line(name, &agent.dir, agent.what.as_deref(), style);
if agent.what.is_none() {
writeln!(out, "{line}")?;
chosen.push(agent.config.host);
continue;
}
loop {
write!(out, "{line}")?;
out.flush()?;
let answer = crate::terminal::read_answer()?;
if answer.is_empty() {
writeln!(out)?;
break;
}
match answer.trim().to_ascii_lowercase().as_str() {
"d" | "diff" => preview::diff(&agent.changes, out)?,
"" | "y" | "yes" => {
chosen.push(agent.config.host);
break;
}
_ => break,
}
}
}
Ok(chosen)
}
pub(super) const INTRO: &str =
" Each file is backed up first · d shows the full diff · openmoat uninstall undoes it";
pub(super) struct Widths {
pub name: usize,
pub dir: usize,
pub what: usize,
}
impl Widths {
pub fn line(&self, name: &str, dir: &str, what: Option<&str>, style: Style) -> String {
let (name_w, dir_w, what_w) = (self.name, self.dir, self.what);
let head = clean(&format!(" {name:<name_w$} {dir:<dir_w$}")).into_owned();
if let Some(what) = what {
format!("{head} {what:<what_w$} Protect? [Y/n/d] ")
} else {
let set_up = format!("{:<what_w$}", "already set up");
format!("{head} {set_up} {}", style.mark(Mark::Ok))
}
}
}
fn width<'a>(texts: impl Iterator<Item = &'a str>) -> usize {
texts.map(|t| t.chars().count()).max().unwrap_or(0)
}