From ba7a40a5f08718f5fdb7abe1cc65def845fee49e Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Fri, 8 Dec 2023 11:26:38 +0100 Subject: [PATCH] more value hints in fish --- complete/src/fish.rs | 48 +++++++++++++++++++++++++++++++++++++++----- complete/src/lib.rs | 15 ++++++-------- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/complete/src/fish.rs b/complete/src/fish.rs index 5bbb451..3571b01 100644 --- a/complete/src/fish.rs +++ b/complete/src/fish.rs @@ -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") + ) + } + } } diff --git a/complete/src/lib.rs b/complete/src/lib.rs index 32a88fb..2be302e 100644 --- a/complete/src/lib.rs +++ b/complete/src/lib.rs @@ -16,19 +16,16 @@ pub struct Arg { pub value: Option, } +// Modelled after claps ValueHint pub enum ValueHint { Strings(Vec), Unknown, - // Other, AnyPath, - // FilePath, - // DirPath, - // ExecutablePath, - // CommandName, - // CommandString, - // CommandWithArguments, - // Username, - // Hostname, + FilePath, + DirPath, + ExecutablePath, + Username, + Hostname, } pub fn render(c: &Command, shell: &str) -> String {