diff --git a/complete/src/lib.rs b/complete/src/lib.rs index 15dc7ba..5d8649c 100644 --- a/complete/src/lib.rs +++ b/complete/src/lib.rs @@ -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\""), diff --git a/complete/src/nu.rs b/complete/src/nu.rs new file mode 100644 index 0000000..95310aa --- /dev/null +++ b/complete/src/nu.rs @@ -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: String { + format!( + "\ + export extern {name} [\n{args}\ + ]\n\ + " + ) +}