From e25b1abddf7c32001a15caf0aa1e2630a6822ea5 Mon Sep 17 00:00:00 2001 From: sudotac Date: Sun, 3 Dec 2023 17:20:38 +0900 Subject: [PATCH] feat(complete): Add DirPath support in bash --- clap_complete/src/shells/bash.rs | 3 +++ .../snapshots/home/static/exhaustive/bash/.bashrc | 10 ++++++++-- clap_complete/tests/snapshots/value_hint.bash | 10 ++++++++-- clap_complete/tests/testsuite/bash.rs | 4 ++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/clap_complete/src/shells/bash.rs b/clap_complete/src/shells/bash.rs index 8c2755f6..ed15b1e8 100644 --- a/clap_complete/src/shells/bash.rs +++ b/clap_complete/src/shells/bash.rs @@ -170,6 +170,7 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String { for o in p.get_opts() { let compopt = match o.get_value_hint() { ValueHint::FilePath => Some("compopt -o filenames"), + ValueHint::DirPath => Some("compopt -o plusdirs"), _ => None, }; @@ -229,6 +230,8 @@ fn vals_for(o: &Arg) -> String { .collect::>() .join(" ") ) + } else if o.get_value_hint() == ValueHint::DirPath { + String::from("") // should be empty to avoid duplicate candidates } else if o.get_value_hint() == ValueHint::Other { String::from("\"${cur}\"") } else { diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc index 42621a2d..50f1b0ff 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc +++ b/clap_complete/tests/snapshots/home/static/exhaustive/bash/.bashrc @@ -567,11 +567,17 @@ _exhaustive() { return 0 ;; --dir) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=() + if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then + compopt -o plusdirs + fi return 0 ;; -d) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=() + if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then + compopt -o plusdirs + fi return 0 ;; --exe) diff --git a/clap_complete/tests/snapshots/value_hint.bash b/clap_complete/tests/snapshots/value_hint.bash index ca042c54..19eba6b4 100644 --- a/clap_complete/tests/snapshots/value_hint.bash +++ b/clap_complete/tests/snapshots/value_hint.bash @@ -60,11 +60,17 @@ _my-app() { return 0 ;; --dir) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=() + if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then + compopt -o plusdirs + fi return 0 ;; -d) - COMPREPLY=($(compgen -f "${cur}")) + COMPREPLY=() + if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then + compopt -o plusdirs + fi return 0 ;; --exe) diff --git a/clap_complete/tests/testsuite/bash.rs b/clap_complete/tests/testsuite/bash.rs index 66a6e4c5..a1f18a3e 100644 --- a/clap_complete/tests/testsuite/bash.rs +++ b/clap_complete/tests/testsuite/bash.rs @@ -200,8 +200,8 @@ fn complete() { ); let actual = runtime.complete(input.as_str(), &term).unwrap(); assert!( - actual.contains("a_file") - && actual.contains("b_file") + !actual.contains("a_file") + && !actual.contains("b_file") && actual.contains("c_dir") && actual.contains("d_dir"), "Actual output:\n{}",