diff --git a/crates/nu-cli/src/completion/command.rs b/crates/nu-cli/src/completion/command.rs index 66457ac9f..3d062962d 100644 --- a/crates/nu-cli/src/completion/command.rs +++ b/crates/nu-cli/src/completion/command.rs @@ -1,5 +1,5 @@ use std::iter::FromIterator; -use std::path::Path; +use std::path::{Path, PathBuf}; use indexmap::set::IndexSet; @@ -36,7 +36,24 @@ impl Completer { let path_completer = crate::completion::path::Completer; let path_results = path_completer.complete(ctx, partial); suggestions.extend(path_results.into_iter().filter(|suggestion| { - let path = Path::new(&suggestion.replacement); + // TODO better path abstractions to avoid a mess like this + let path = { + #[cfg(feature = "directories")] + { + let home_prefix = format!("~{}", std::path::MAIN_SEPARATOR); + if let Some(mut home) = dirs::home_dir() { + home.push(suggestion.replacement.replacen(&home_prefix, "", 1)); + home + } else { + PathBuf::from(&suggestion.replacement) + } + } + #[cfg(not(feature = "directories"))] + { + PathBuf::from(&suggestion.replacement) + } + }; + path.is_dir() || is_executable(&path) })); } diff --git a/crates/nu-cli/src/completion/path.rs b/crates/nu-cli/src/completion/path.rs index 85acc8308..80838f11b 100644 --- a/crates/nu-cli/src/completion/path.rs +++ b/crates/nu-cli/src/completion/path.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::PathBuf; use crate::completion::{Context, Suggestion}; @@ -17,9 +17,18 @@ impl Completer { }; let base_dir = if base_dir_name == "" { - Path::new(".") + PathBuf::from(".") + } else if base_dir_name == format!("~{}", SEP) { + #[cfg(feature = "directories")] + { + dirs::home_dir().unwrap_or_else(|| PathBuf::from("~")) + } + #[cfg(not(feature = "directories"))] + { + PathBuf::from("~") + } } else { - Path::new(base_dir_name) + PathBuf::from(base_dir_name) }; if let Ok(result) = base_dir.read_dir() {