more value hints in fish

This commit is contained in:
Terts Diepraam
2023-12-08 11:29:50 +01:00
parent 2f6c32ddfc
commit ba7a40a5f0
2 changed files with 49 additions and 14 deletions
+43 -5
View File
@@ -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() {
@@ -65,4 +68,39 @@ 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")
)
}
}
}
+6 -9
View File
@@ -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 {