super basic zsh completion

This commit is contained in:
Terts Diepraam
2023-12-08 13:20:03 +01:00
parent 675bbb95c8
commit c30d5e893b
2 changed files with 61 additions and 1 deletions
+3 -1
View File
@@ -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}'!"),
}
}
+58
View File
@@ -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"
)
}