I make an CLI that gets the packages from yay (AUR package manager) and suggest, but I have a big problem with the clap lib:
fn package_completion() -> Result<Vec<String>, String> {
let command = process::Command::new("yay")
.arg("-Slq")
.output()
.expect("failed to execute process");
let mut stdout = String::with_capacity(command.stdout.len());
let mut stderr = String::with_capacity(command.stderr.len());
stdout.push_str(&String::from_utf8_lossy(&command.stdout));
stderr.push_str(&String::from_utf8_lossy(&command.stderr));
if !stderr.is_empty() {
return Err(stderr);
}
let mut output: Vec<String> = stdout.lines().map(|s| s.to_string()).collect();
if output.len() > 0 {
output.remove(0);
}
Ok(output)
}
pub fn build_cli() -> Command {
Command::new("yay-helper")
.version("0.0.0")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("query").about("Query a package").arg(
Arg::new("package")
.help("Package to query")
.required(true)
.action(ArgAction::Set)
.num_args(1..),
),
)
.subcommand(
Command::new("install").about("Install a package").arg(
Arg::new("package")
.help("Package to install")
.value_parser(package_completion().unwrap()) # This causes the issue
.required(true)
.action(ArgAction::Set)
.num_args(1..),
),
)
The program runs correctly, but the autocompletion is lazy, I need await more then 30 seconds to get the autocompletion.
I tried work with other lib (shell_completion) to make the completion and works well for Bash (the completion just generates a Bash version, I want a multi-shell version), the problem isn't the size of package list.
I need a more fastest approach to generate the autocompletion, if someone helps me, i'll be thankful.