Add column kind values to shell completions

Inject KIND_LIST entries as possible values into the clap Command used
for completion generation, so --sorta, --sortd, --insert, and --only
now offer tab-completable kind names with descriptions.
This commit is contained in:
Lam Chau
2026-03-11 00:13:02 -07:00
parent b4b67d2481
commit 2ad10e14cb
3 changed files with 44 additions and 12 deletions
+7 -5
View File
@@ -1,13 +1,15 @@
use clap::CommandFactory;
include!("src/opt.rs");
fn main() {
if let Ok(path) = std::env::var("COMPLETION_PATH") {
let out_dir = PathBuf::from(path);
gen_completion(Shell::Bash, &out_dir).unwrap();
gen_completion(Shell::Elvish, &out_dir).unwrap();
gen_completion(Shell::Fish, &out_dir).unwrap();
gen_completion(Shell::PowerShell, &out_dir).unwrap();
gen_completion(Shell::Zsh, &out_dir).unwrap();
gen_completion(Shell::Bash, &out_dir, &mut Opt::command()).unwrap();
gen_completion(Shell::Elvish, &out_dir, &mut Opt::command()).unwrap();
gen_completion(Shell::Fish, &out_dir, &mut Opt::command()).unwrap();
gen_completion(Shell::PowerShell, &out_dir, &mut Opt::command()).unwrap();
gen_completion(Shell::Zsh, &out_dir, &mut Opt::command()).unwrap();
}
}
+31 -3
View File
@@ -19,6 +19,7 @@ use crate::watcher::Watcher;
use anyhow::{Context, Error};
use clap::{CommandFactory, Parser};
use console::Term;
use once_cell::sync::Lazy;
use std::cmp;
use std::collections::HashMap;
use std::fs;
@@ -31,6 +32,29 @@ use unicode_width::UnicodeWidthStr;
// Functions
// ---------------------------------------------------------------------------------------------------------------------
static KIND_NAMES_LOWER: Lazy<Vec<String>> = Lazy::new(|| {
KIND_LIST
.iter()
.map(|(_, (name, _))| name.to_lowercase())
.collect()
});
fn command_with_kind_values() -> clap::Command {
let kind_values: Vec<clap::builder::PossibleValue> = KIND_LIST
.iter()
.zip(KIND_NAMES_LOWER.iter())
.map(|((_, (_, desc)), lower)| {
clap::builder::PossibleValue::new(lower.as_str()).help(*desc)
})
.collect();
let parser = clap::builder::PossibleValuesParser::new(kind_values);
Opt::command()
.mut_arg("sorta", |a| a.value_parser(parser.clone()))
.mut_arg("sortd", |a| a.value_parser(parser.clone()))
.mut_arg("insert", |a| a.value_parser(parser.clone()))
.mut_arg("only", |a| a.value_parser(parser))
}
fn get_config(opt: &Opt) -> Result<Config, Error> {
let dot_cfg_path = directories::BaseDirs::new()
.map(|base| base.home_dir().join(".procs.toml"))
@@ -122,10 +146,14 @@ fn run() -> Result<(), Error> {
run_list();
Ok(())
} else if let Some(shell) = opt.gen_completion {
gen_completion(shell, "./")
gen_completion(shell, "./", &mut command_with_kind_values())
} else if let Some(shell) = opt.gen_completion_out {
//Opt::clap().gen_completions_to("procs", shell, &mut stdout());
clap_complete::generate(shell, &mut Opt::command(), "procs", &mut stdout());
clap_complete::generate(
shell,
&mut command_with_kind_values(),
"procs",
&mut stdout(),
);
Ok(())
} else {
let config = get_config(&opt)?;
+6 -4
View File
@@ -1,5 +1,4 @@
use anyhow::{Error, anyhow};
use clap::CommandFactory;
use clap::builder::styling::{AnsiColor, Effects, Styles};
use clap::{Parser, ValueEnum};
use clap_complete::Shell;
@@ -186,10 +185,13 @@ pub struct Opt {
pub debug: bool,
}
pub fn gen_completion<P: AsRef<Path>>(shell: Shell, path: P) -> Result<(), Error> {
pub fn gen_completion<P: AsRef<Path>>(
shell: Shell,
path: P,
cmd: &mut clap::Command,
) -> Result<(), Error> {
let path_str = path.as_ref().as_os_str();
//Opt::clap().gen_completions("procs", shell, path_str);
clap_complete::generate_to(shell, &mut Opt::command(), "procs", path_str)?;
clap_complete::generate_to(shell, cmd, "procs", path_str)?;
let filename = match shell {
Shell::Bash => "procs.bash",
Shell::Elvish => "procs.elv",