Skip to content

Commit

Permalink
feat(clap_complete): Support hiding possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu authored and epage committed Jul 19, 2024
1 parent 4395c31 commit 9220bbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
38 changes: 22 additions & 16 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ fn complete_arg(
completions.extend(
complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
.into_iter()
.map(|(os, help)| {
// HACK: Need better `OsStr` manipulation
.map(|comp| {
CompletionCandidate::new(format!(
"--{}={}",
flag,
os.to_string_lossy()
comp.get_content().to_string_lossy()
))
.help(help)
.visible(true)
.help(comp.get_help().cloned())
.visible(comp.is_visible())
}),
);
}
Expand Down Expand Up @@ -169,11 +168,7 @@ fn complete_arg(
.get_positionals()
.find(|p| p.get_index() == Some(pos_index))
{
completions.extend(
complete_arg_value(arg.to_value(), positional, current_dir)
.into_iter()
.map(|(os, help)| CompletionCandidate::new(os).help(help).visible(true)),
);
completions.extend(complete_arg_value(arg.to_value(), positional, current_dir).into_iter());
}

if let Ok(value) = arg.to_value() {
Expand All @@ -191,16 +186,19 @@ fn complete_arg_value(
value: Result<&str, &OsStr>,
arg: &clap::Arg,
current_dir: Option<&std::path::Path>,
) -> Vec<(OsString, Option<StyledStr>)> {
) -> Vec<CompletionCandidate> {
let mut values = Vec::new();
debug!("complete_arg_value: arg={arg:?}, value={value:?}");

if let Some(possible_values) = possible_values(arg) {
if let Ok(value) = value {
values.extend(possible_values.into_iter().filter_map(|p| {
let name = p.get_name();
name.starts_with(value)
.then(|| (OsString::from(name), p.get_help().cloned()))
name.starts_with(value).then(|| {
CompletionCandidate::new(OsString::from(name))
.help(p.get_help().cloned())
.visible(!p.is_hide_set())
})
}));
}
} else {
Expand Down Expand Up @@ -249,7 +247,7 @@ fn complete_path(
value_os: &OsStr,
current_dir: Option<&std::path::Path>,
is_wanted: impl Fn(&std::path::Path) -> bool,
) -> Vec<(OsString, Option<StyledStr>)> {
) -> Vec<CompletionCandidate> {
let mut completions = Vec::new();

let current_dir = match current_dir {
Expand Down Expand Up @@ -281,12 +279,20 @@ fn complete_path(
let path = entry.path();
let mut suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path);
suggestion.push(""); // Ensure trailing `/`
completions.push((suggestion.as_os_str().to_owned(), None));
completions.push(
CompletionCandidate::new(suggestion.as_os_str().to_owned())
.help(None)
.visible(true),
);
} else {
let path = entry.path();
if is_wanted(&path) {
let suggestion = pathdiff::diff_paths(&path, current_dir).unwrap_or(path);
completions.push((suggestion.as_os_str().to_owned(), None));
completions.push(
CompletionCandidate::new(suggestion.as_os_str().to_owned())
.help(None)
.visible(true),
);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ fn suggest_hidden_possible_value() {

assert_data_eq!(
complete!(cmd, "--test=test"),
snapbox::str![
"--test=test-visible\tSay hello to the world
--test=test-hidden\tSay hello to the moon"
]
snapbox::str!["--test=test-visible\tSay hello to the world"]
);

assert_data_eq!(
Expand Down

0 comments on commit 9220bbd

Please sign in to comment.