feat: support non-interactive selects #1

Merged
sisungo merged 1 commits from sisungo/packie:select-non-interactive into main 2026-11-16 03:11:55 +00:00
4 changed files with 72 additions and 13 deletions

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)))
}
+2 -2
View File
@@ -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:
+4
View File
@@ -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 会根据您当前目录解析路径。如果不符合预期,请改用绝对路径。"
+2 -2
View File
@@ -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),