Skip to content

Commit 2ec365a

Browse files
committed
feat(clap-complete/unstable-dynamic): add '~' to HOME dir completion support
if input starts with '~' will complete to `/home/USER`
1 parent 634ef20 commit 2ec365a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clap_complete/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ is_executable = { version = "1.0.1", optional = true }
4040
shlex = { version = "1.3.0", optional = true }
4141
completest = { version = "0.4.2", optional = true }
4242
completest-pty = { version = "0.5.5", optional = true }
43+
home = { version = "0.5.0", optional = true }
4344

4445
[dev-dependencies]
4546
snapbox = { version = "0.6.0", features = ["diff", "dir", "examples"] }
@@ -55,7 +56,7 @@ required-features = ["unstable-dynamic"]
5556
[features]
5657
default = []
5758
unstable-doc = ["unstable-dynamic"] # for docs.rs
58-
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:is_executable", "clap/unstable-ext"]
59+
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:is_executable", "dep:home", "clap/unstable-ext"]
5960
unstable-shell-tests = ["dep:completest", "dep:completest-pty"]
6061
debug = ["clap/debug"]
6162

clap_complete/src/engine/custom.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,15 @@ impl ValueCompleter for PathCompleter {
274274
current_dir_actual = std::env::current_dir().ok();
275275
current_dir_actual.as_deref()
276276
});
277-
let mut candidates = complete_path(current, current_dir, filter);
277+
278+
let current = current.to_string_lossy().to_string();
279+
if current.starts_with("~") {
280+
if let Some(home) = home_dir() {
281+
current.replace_range(..1, &home.to_string_lossy());
282+
}
283+
}
284+
285+
let mut candidates = complete_path(&current, current_dir, filter);
278286
if self.stdio && current.is_empty() {
279287
candidates.push(CompletionCandidate::new("-").help(Some("stdio".into())));
280288
}
@@ -283,7 +291,7 @@ impl ValueCompleter for PathCompleter {
283291
}
284292

285293
pub(crate) fn complete_path(
286-
value_os: &OsStr,
294+
value_os: &str,
287295
current_dir: Option<&std::path::Path>,
288296
is_wanted: &dyn Fn(&std::path::Path) -> bool,
289297
) -> Vec<CompletionCandidate> {

0 commit comments

Comments
 (0)