feat: support non-interactive selects

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-11-16 11:08:41 +08:00
1 parent d1c60097b3
commit eb02cdf11f
4 files changed
+72 -13

No files matched your search

+64 -9
View File
@@ -2,13 +2,21 @@ use crate::error;
use clap::Parser;
use console::style;
use dialoguer::Select;
use packie::PackieBuilder;
use packie::{
Packie, PackieBuilder,
link::LinkDescription,
package::{PkgIdent, PkgSpec},
};
use rust_i18n::t;
use std::path::PathBuf;
#[derive(Debug, Parser)]
pub struct Cli {
/// The file being selected
file: PathBuf,
/// The package to use for the file (prompt if not provided)
pkg: Option<PkgSpec>,
}
pub fn main(cli: Cli) -> Result<(), crate::Error> {
@@ -18,6 +26,57 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
} else {
std::env::current_dir()?.join(&cli.file)
};
let data = if let Some(pkg) = cli.pkg {
non_interactive(&mut packie, cli.file, pkg)?
} else {
interactive(&mut packie, cli.file)?
};
if let Some((pkgident, linkdes)) = data {
_ = packie::link::remove(&file);
_ = packie::link::deactivate_by_description(&mut packie, &pkgident, &linkdes);
packie::link::activate_by_description(&mut packie, &pkgident, &linkdes)?;
}
Ok(())
}
fn non_interactive(
packie: &mut Packie,
file: PathBuf,
pkg: PkgSpec,
) -> Result<Option<(PkgIdent, LinkDescription)>, crate::Error> {
let installations = packie.search_installation(&pkg)?;
let mut candidates = Vec::with_capacity(installations.len());
for pkgident in installations.iter().map(|x| x.pkg_manifest.pkg_ident()) {
let links = packie.links_of_pkg(&pkgident);
let Ok(links) = links.iter() else {
return Ok(None);
};
if let Some(link) = links
.filter_map(|x| x.ok())
.find(|x| x.to == file.to_string_lossy())
{
candidates.push((pkgident, link));
}
}
if candidates.is_empty() {
return Err(crate::Error {
message: t!("select.empty_list").into(),
note: Some(t!("select.note_relative_path").into()),
});
}
if candidates.len() != 1 {
return Err(error!("select.too_many_candidates"));
}
Ok(Some(candidates.remove(0)))
}
fn interactive(
packie: &mut Packie,
file: PathBuf,
) -> Result<Option<(PkgIdent, LinkDescription)>, crate::Error> {
let mut all_pkgs = Vec::with_capacity(1024);
let mut candidates = Vec::new();
packie.for_each_installation::<anyhow::Error>(|inst| {
@@ -27,7 +86,7 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
for pkgident in all_pkgs {
let links = packie.links_of_pkg(&pkgident);
let Ok(links) = links.iter() else {
return Ok(());
return Ok(None);
};
if let Some(link) = links
.filter_map(|x| x.ok())
@@ -55,15 +114,11 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
return Err(error!("select.not_a_link"));
}
if err.kind() == std::io::ErrorKind::NotFound {
return Ok(());
return Ok(None);
}
Err(err)?;
}
return Ok(());
return Ok(None);
}
let (pkgident, linkdes) = candidates.remove(choice - 1);
_ = packie::link::remove(&file);
_ = packie::link::deactivate_by_description(&mut packie, &pkgident, &linkdes);
packie::link::activate_by_description(&mut packie, &pkgident, &linkdes)?;
Ok(())
Ok(Some(candidates.remove(choice - 1)))
}