use crate::{
config::Config,
package::{Package, Packages},
repo::Repo,
};
use anyhow::{Context, Result};
use std::path::Path;
pub fn clean<P>(config_path: P) -> Result<()>
where
P: AsRef<Path>,
{
let config = Config::from_path_or_new(&config_path)?;
let repo = Repo::new(config, true)?;
let packages = repo.read().context("reading packages to clean")?;
let leftover_packages = packages_to_clean(packages).context("selecting packages to clean")?;
repo.clean(leftover_packages)
}
fn packages_to_clean(packages: Packages) -> Result<Packages> {
let package_names: Vec<String> = packages
.iter()
.map(|package| package.color_full_name())
.collect::<_>();
let package_names: Vec<&str> = package_names
.iter()
.map(|name| name.as_str())
.collect::<_>();
if package_names.is_empty() {
println!("No packages have been added yet.");
return Ok(packages);
}
let selections = dialoguer::MultiSelect::new()
.with_prompt("Select the packages you want to stop syncing (space to select)")
.items(&package_names[..])
.interact()
.context("failed constructing checkboxes")?;
Ok(packages
.iter()
.enumerate()
.filter_map(|(index, package)| {
for selection in &selections {
if index == *selection {
return None;
}
}
Some(package.clone())
})
.collect::<Vec<Package>>()
.into())
}