From eb02cdf11fbb7ce139bef8cf2bdb02d4abf283c185a5b46faaf4da3968352ef2 Mon Sep 17 00:00:00 2001 From: sisungo Date: Mon, 16 Nov 2026 11:08:41 +0800 Subject: [PATCH] feat: support non-interactive selects Signed-off-by: sisungo --- cli/src/select.rs | 73 ++++++++++++++++++++++++++++++++----- locales/libpackie/main.yml | 4 +- locales/packie-cli/main.yml | 4 ++ src/install.rs | 4 +- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/cli/src/select.rs b/cli/src/select.rs index e074869..d9e56c8 100644 --- a/cli/src/select.rs +++ b/cli/src/select.rs @@ -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, } 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, 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, crate::Error> { let mut all_pkgs = Vec::with_capacity(1024); let mut candidates = Vec::new(); packie.for_each_installation::(|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))) } diff --git a/locales/libpackie/main.yml b/locales/libpackie/main.yml index 1238ce1..8ca072c 100644 --- a/locales/libpackie/main.yml +++ b/locales/libpackie/main.yml @@ -7,8 +7,8 @@ install.InstallError.Database: en: "local database error: %{error}" zh-CN: "本地数据库错误:%{error}" ja: "ローカルデータベースエラー:%{error}" -install.InstallError.DependencyRing: - en: "dependency ring detected" +install.InstallError.DependencyCycle: + en: "dependency cycle detected" zh-CN: "检测到依赖环" ja: "依存関係の循環が検出されました" install.InstallError.DependencyNotSatisfied: diff --git a/locales/packie-cli/main.yml b/locales/packie-cli/main.yml index f4a166b..883854c 100644 --- a/locales/packie-cli/main.yml +++ b/locales/packie-cli/main.yml @@ -223,6 +223,10 @@ select.empty_list: en: "No package provided the given link" zh-CN: "没有包提供要求的链接" ja: "指定されたリンクを提供するパッケージはありません" +select.too_many_candidates: + en: "Too many candidates." + zh-CN: "候选包过多。" + ja: "候補が多すぎます。" select.note_relative_path: en: "A relative path is specified, and Packie resolves path based on your current directory. If this is not expected, provide an absolute path instead." zh-CN: "指定了相对路径,Packie 会根据您当前目录解析路径。如果不符合预期,请改用绝对路径。" diff --git a/src/install.rs b/src/install.rs index c7b9a0a..5ec140e 100644 --- a/src/install.rs +++ b/src/install.rs @@ -185,8 +185,8 @@ impl super::Packie { #[derive(Debug, thiserror::Error)] pub enum InstallError { - #[error("{}", t!("install.InstallError.DependencyRing"))] - DependencyRing, + #[error("{}", t!("install.InstallError.DependencyCycle"))] + DependencyCycle, #[error("{}", t!("install.InstallError.DependencyNotSatisfied", pkgspec = .0))] DependencyNotSatisfied(PkgSpec),