basic nushell completions

This commit is contained in:
Terts Diepraam
2023-12-19 12:06:56 +01:00
parent 91dad39c7a
commit ef898fe43c
2 changed files with 53 additions and 0 deletions
+2
View File
@@ -16,6 +16,7 @@
mod fish;
mod man;
mod md;
mod nu;
mod zsh;
/// A description of a CLI command
@@ -72,6 +73,7 @@ pub fn render(c: &Command, shell: &str) -> String {
"md" => md::render(c),
"fish" => fish::render(c),
"zsh" => zsh::render(c),
"nu" | "nushell" => nu::render(c),
"man" => man::render(c),
"sh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not implemented yet!"),
_ => panic!("unknown option '{shell}'! Expected one of: \"md\", \"fish\", \"zsh\", \"man\", \"sh\", \"bash\", \"csh\", \"elvish\", \"powershell\""),
+51
View File
@@ -0,0 +1,51 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use crate::{Arg, Command, Flag, Value};
use std::fmt::Write;
/// Create completion script for `nushell`
pub fn render(c: &Command) -> String {
let mut args = Vec::new();
let indent = " ".repeat(4);
for Arg {
short,
long,
help,
value: _value,
} in &c.args
{
for Flag { flag, value } in short {
let value = if let Value::Required(_) | Value::Optional(_) = value {
": string"
} else {
""
};
args.push((format!("-{flag}{value}"), help));
}
for Flag { flag, value } in long {
let value = if let Value::Required(_) | Value::Optional(_) = value {
": string"
} else {
""
};
args.push((format!("--{flag}{value}"), help));
}
}
let longest_arg = args.iter().map(|a| a.0.len()).max().unwrap_or_default();
let mut arg_str = String::new();
for (a, h) in args {
writeln!(arg_str, "{indent}{a:<longest_arg$} # {h}").unwrap();
}
template(c.name, &arg_str)
}
fn template(name: &str, args: &str) -> String {
format!(
"\
export extern {name} [\n{args}\
]\n\
"
)
}