mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
refactor(complete): Remove manual path splitting
This commit is contained in:
parent
87647d268c
commit
2f07f3cc61
1 changed files with 24 additions and 5 deletions
|
@ -355,12 +355,10 @@ fn complete_path(
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let (existing, prefix) = value_os
|
let absolute = current_dir.join(value_os);
|
||||||
.split_once("\\")
|
let (root, prefix) = split_file_name(&absolute);
|
||||||
.unwrap_or((OsStr::new(""), value_os));
|
|
||||||
let root = current_dir.join(existing);
|
|
||||||
debug!("complete_path: root={root:?}, prefix={prefix:?}");
|
|
||||||
let prefix = prefix.to_string_lossy();
|
let prefix = prefix.to_string_lossy();
|
||||||
|
debug!("complete_path: root={root:?}, prefix={prefix:?}");
|
||||||
|
|
||||||
for entry in std::fs::read_dir(&root)
|
for entry in std::fs::read_dir(&root)
|
||||||
.ok()
|
.ok()
|
||||||
|
@ -392,6 +390,27 @@ fn complete_path(
|
||||||
completions
|
completions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn split_file_name(path: &std::path::Path) -> (&std::path::Path, &OsStr) {
|
||||||
|
// Workaround that `Path::new("name/").file_name()` reports `"name"`
|
||||||
|
if path_has_name(path) {
|
||||||
|
(
|
||||||
|
path.parent().unwrap_or_else(|| std::path::Path::new("")),
|
||||||
|
path.file_name().expect("not called with `..`"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(path, Default::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path_has_name(path: &std::path::Path) -> bool {
|
||||||
|
let path = path.as_os_str().as_encoded_bytes();
|
||||||
|
let Some(trailing) = path.last() else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let trailing = *trailing as char;
|
||||||
|
!std::path::is_separator(trailing)
|
||||||
|
}
|
||||||
|
|
||||||
fn complete_custom_arg_value(
|
fn complete_custom_arg_value(
|
||||||
value: &OsStr,
|
value: &OsStr,
|
||||||
completer: &ArgValueCandidates,
|
completer: &ArgValueCandidates,
|
||||||
|
|
Loading…
Reference in a new issue