Skip to content

Commit

Permalink
more value hints in fish
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 8, 2023
1 parent 2f6c32d commit cfa95bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
41 changes: 36 additions & 5 deletions complete/src/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ pub fn render(c: &Command) -> String {
fn render_value_hint(value: &ValueHint) -> String {
match value {
ValueHint::Strings(s) => {
let joined = s.join(", ");
format!(" -a {{ {joined} }}")
let joined = s.join(" ");
format!(" -f -a \"{joined}\"")
}
ValueHint::Unknown => String::new(),
_ => todo!(),
ValueHint::AnyPath | ValueHint::FilePath | ValueHint::ExecutablePath => String::from(" -F"),
ValueHint::DirPath => " -f -a \"(__fish_complete_directories)\"".into(),
ValueHint::Unknown => " -f".into(),
ValueHint::Username => " -f -a \"(__fish_complete_users)\"".into(),
ValueHint::Hostname => " -f -a \"(__fish_print_hostnames)\"".into(),
}
}

#[cfg(test)]
mod test {
use super::render;
use crate::{Arg, Command};
use crate::{Arg, Command, ValueHint};

#[test]
fn short() {
Expand All @@ -65,4 +68,32 @@ mod test {
};
assert_eq!(render(&c), "complete -c test -l all -d 'some flag'\n",)
}

#[test]
fn value_hints() {
let args = [
(ValueHint::Strings(vec!["all".into(), "none".into()]), "-f -a \"all none\""),
(ValueHint::Unknown, "-f"),
(ValueHint::AnyPath, "-F"),
(ValueHint::FilePath, "-F"),
(ValueHint::DirPath, "-f -a \"(__fish_complete_directories)\""),
(ValueHint::ExecutablePath, "-F"),
(ValueHint::Username, "-f -a \"(__fish_complete_users)\""),
(ValueHint::Hostname, "-f -a \"(__fish_print_hostnames)\""),
];
for (hint, expected) in args {
let c = Command {
name: "test".into(),
args: vec![
Arg {
short: vec!["a".into()],
long: vec![],
help: "some flag".into(),
value: Some(hint),
},
]
};
assert_eq!(render(&c), format!("complete -c test -s a -d 'some flag' {expected}\n"))
}
}
}
15 changes: 6 additions & 9 deletions complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ pub struct Arg {
pub value: Option<ValueHint>,
}

// Modelled after claps ValueHint
pub enum ValueHint {
Strings(Vec<String>),
Unknown,
// Other,
AnyPath,
// FilePath,
// DirPath,
// ExecutablePath,
// CommandName,
// CommandString,
// CommandWithArguments,
// Username,
// Hostname,
FilePath,
DirPath,
ExecutablePath,
Username,
Hostname,
}

pub fn render(c: &Command, shell: &str) -> String {
Expand Down

0 comments on commit cfa95bc

Please sign in to comment.