From 906c0e6bcabff096a757947cca814d1e47d1ad9c Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 13 Dec 2021 17:46:30 +1100 Subject: [PATCH] Better filepath completions (#485) --- crates/nu-cli/src/completions.rs | 16 ++++++++++++++++ crates/nu-command/src/system/run_external.rs | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/completions.rs b/crates/nu-cli/src/completions.rs index b3235fcb01..9cb98d7b32 100644 --- a/crates/nu-cli/src/completions.rs +++ b/crates/nu-cli/src/completions.rs @@ -207,6 +207,18 @@ fn file_path_completion( ) -> Vec<(nu_protocol::Span, String)> { use std::path::{is_separator, Path}; + let partial = if let Some(s) = partial.strip_prefix('"') { + s + } else { + partial + }; + + let partial = if let Some(s) = partial.strip_suffix('"') { + s + } else { + partial + }; + let (base_dir_name, partial) = { // If partial is only a word we want to search in the current dir let (base, rest) = partial.rsplit_once(is_separator).unwrap_or((".", partial)); @@ -237,6 +249,10 @@ fn file_path_completion( file_name.push(SEP); } + if path.contains(' ') { + path = format!("\"{}\"", path); + } + Some((span, path)) } else { None diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 56d311f250..044c1df015 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -57,10 +57,14 @@ impl Command for External { // Check if this is a single call to a directory, if so auto-cd let path = nu_path::expand_path(&name.item); + let orig = name.item.clone(); name.item = path.to_string_lossy().to_string(); let path = Path::new(&name.item); - if (name.item.starts_with('.') || name.item.starts_with('/') || name.item.starts_with('\\')) + if (orig.starts_with('.') + || orig.starts_with('~') + || orig.starts_with('/') + || orig.starts_with('\\')) && path.is_dir() && args.is_empty() {