Skip to content

Commit

Permalink
fix(complete): Remove usage of unix specific functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Oct 3, 2022
1 parent 0b06f21 commit b848ffa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 1 addition & 2 deletions clap_complete/src/dynamic/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::{
fmt::Display,
io::{stdout, Write},
iter,
os::unix::prelude::OsStrExt,
};

use clap::Args;
Expand Down Expand Up @@ -86,7 +85,7 @@ impl Completer for Fish {
// TODO display non option values, in case the arg allows values starting with `-`
} else if let Some(value_for) = value_for {
for (item, help) in completions_for_arg(value_for, &current)? {
add_completion(&mut completions, item.as_bytes(), help)?;
add_completion(&mut completions, item.as_raw_bytes(), help)?;
}
} else {
for subcommand in subcommands {
Expand Down
16 changes: 6 additions & 10 deletions clap_complete/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,20 +346,16 @@ fn is_some_and<T>(option: Option<T>, fun: impl Fn(T) -> bool) -> bool {
fn completions_for_arg(
arg: &Arg,
value: &RawOsStr,
) -> io::Result<Vec<(OsString, Option<StyledStr>)>> {
) -> io::Result<Vec<(RawOsString, Option<StyledStr>)>> {
// TODO take current token to complete subdirectories
let mut values = Vec::new();
debug!("complete_arg_value: arg={:?}, value={:?}", arg, value);

if let Some(possible_values) = crate::generator::utils::possible_values(arg) {
values.extend(possible_values.into_iter().filter_map(|p| {
let name = RawOsStr::from_str(p.get_name());
name.starts_with_os(value).then(|| {
(
name.to_os_str().to_os_string(),
p.get_help().map(ToOwned::to_owned),
)
})
name.starts_with_os(value)
.then(|| (name.to_owned(), p.get_help().map(ToOwned::to_owned)))
}));
} else {
match arg.get_value_hint() {
Expand Down Expand Up @@ -402,7 +398,7 @@ fn completions_for_arg(
fn complete_path(
path: &clap_lex::RawOsStr,
is_wanted: impl Fn(&Path) -> bool,
) -> Vec<(OsString, Option<StyledStr>)> {
) -> Vec<(RawOsString, Option<StyledStr>)> {
let mut completions = Vec::new();

// `/` on Unix and `/`+`\` on Windows
Expand Down Expand Up @@ -449,12 +445,12 @@ fn complete_path(
let path = entry.path();
let mut suggestion = pathdiff::diff_paths(&path, &root).unwrap_or(path);
suggestion.push(""); // Ensure trailing `/`
completions.push((suggestion.as_os_str().to_owned(), None));
completions.push((RawOsString::new(suggestion.as_os_str().to_owned()), None));
} else {
let path = entry.path();
if is_wanted(&path) {
let suggestion = pathdiff::diff_paths(&path, &root).unwrap_or(path);
completions.push((suggestion.as_os_str().to_owned(), None));
completions.push((RawOsString::new(suggestion.as_os_str().to_owned()), None));
}
}
}
Expand Down

0 comments on commit b848ffa

Please sign in to comment.