You've already forked uutils-args
mirror of
https://github.com/uutils/uutils-args.git
synced 2026-06-10 16:13:08 -07:00
super basic zsh completion
This commit is contained in:
+3
-1
@@ -2,6 +2,7 @@
|
||||
// file that was distributed with this source code.
|
||||
|
||||
mod fish;
|
||||
mod zsh;
|
||||
|
||||
pub struct Command {
|
||||
pub name: String,
|
||||
@@ -31,7 +32,8 @@ pub enum ValueHint {
|
||||
pub fn render(c: &Command, shell: &str) -> String {
|
||||
match shell {
|
||||
"fish" => fish::render(c),
|
||||
"sh" | "zsh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not supported yet!"),
|
||||
"zsh" => zsh::render(c),
|
||||
"sh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not supported yet!"),
|
||||
_ => panic!("unknown shell '{shell}'!"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// For the full copyright and license information, please view the LICENSE
|
||||
// file that was distributed with this source code.
|
||||
|
||||
use crate::{Command, Arg};
|
||||
|
||||
pub fn render(c: &Command) -> String {
|
||||
template(&c.name, &render_args(&c.args))
|
||||
}
|
||||
|
||||
fn render_args(args: &[Arg]) -> String {
|
||||
let mut out = String::new();
|
||||
let indent = " ".repeat(8);
|
||||
for arg in args {
|
||||
let help = &arg.help;
|
||||
for short in &arg.short {
|
||||
out.push_str(
|
||||
&format!("{indent}'-{short}[{help}]' \\\n")
|
||||
);
|
||||
}
|
||||
for long in &arg.long {
|
||||
out.push_str(
|
||||
&format!("{indent}'--{long}[{help}]' \\\n")
|
||||
);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn template(name: &str, args: &str) -> String {
|
||||
format!(
|
||||
"\
|
||||
#compdef {name}
|
||||
|
||||
autoload -U is-at-least
|
||||
|
||||
_{name}() {{
|
||||
typeset -A opt_args
|
||||
typeset -a _arguments_options
|
||||
local ret=1
|
||||
|
||||
if is-at-least 5.2; then
|
||||
_arguments_options=(-s -S -C)
|
||||
else
|
||||
_arguments_options=(-s -C)
|
||||
fi
|
||||
|
||||
local context curcontext=\"$curcontext\" state line
|
||||
_arguments \"${{_arguments_options[@]}}\" \\\n{args}
|
||||
&& ret=0
|
||||
}}
|
||||
|
||||
if [ \"$funcstack[1]\" = \"_{name}\" ]; then
|
||||
{name} \"$@\"
|
||||
else
|
||||
compdef _{name} {name}
|
||||
fi"
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user