Skip to content

Commit

Permalink
basic nushell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 12, 2023
1 parent 3ed7316 commit ab662f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
mod fish;
mod man;
mod md;
mod nu;
mod zsh;

/// A description of a CLI command
Expand Down Expand Up @@ -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\""),
Expand Down
51 changes: 51 additions & 0 deletions complete/src/nu.rs
Original file line number Diff line number Diff line change
@@ -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\
"
)
}

0 comments on commit ab662f4

Please sign in to comment.