From 8a52085ae266bdb5e9376492adfb6dc2b1db6b32 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 13 Jun 2023 07:30:10 -0500 Subject: [PATCH] allow paths to have brackets (#9416) # Description This PR is trying to allow you to have `[blah]` in your path and yet still have `ls` work. This is done by trying to separate the path from the pattern to be searched for. It may still need more work. I've tested it with: - mkdir "[test]" - cd "[test]" - ls Related to #9307 Hopefully fixes #9232 # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-engine/src/glob_from.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/nu-engine/src/glob_from.rs b/crates/nu-engine/src/glob_from.rs index fe9620b070..2490ded5cb 100644 --- a/crates/nu-engine/src/glob_from.rs +++ b/crates/nu-engine/src/glob_from.rs @@ -34,7 +34,35 @@ pub fn glob_from( Err(_) => false, }; - let (prefix, pattern) = if path.to_string_lossy().contains('*') { + // Check for brackets first + let (prefix, pattern) = if path.to_string_lossy().contains('[') { + // Path is a glob pattern => do not check for existence + // Select the longest prefix until the first '*' + let mut p = PathBuf::new(); + let components = path.components(); + let mut counter = 0; + + // Get the path up to the pattern which we'll call the prefix + for c in components { + if let Component::Normal(os) = c { + if os.to_string_lossy().contains('*') { + break; + } + } + p.push(c); + counter += 1; + } + + // Let's separate the pattern from the path and we'll call this the pattern + let mut just_pattern = PathBuf::new(); + for c in counter..path.components().count() { + if let Some(comp) = path.components().nth(c) { + just_pattern.push(comp); + } + } + + (Some(p), just_pattern) + } else if path.to_string_lossy().contains('*') { // Path is a glob pattern => do not check for existence // Select the longest prefix until the first '*' let mut p = PathBuf::new(); @@ -46,6 +74,7 @@ pub fn glob_from( } p.push(c); } + (Some(p), path) } else if is_symlink { (path.parent().map(|parent| parent.to_path_buf()), path)