mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:55:42 +02:00
treat path contains '?' as pattern (#10142)
Fix https://github.com/nushell/nushell/issues/10136 # Description Current nushell only handle path containing '*' as match pattern and treat '?' as just normal path. This pr makes path containing '?' is also processed as pattern. 🔴 **Concerns: Need to design/comfirm a consistent rule to handle dirs/files with '?' in their names.** Currently: - if no dir has exactly same name with pattern, it will print the list of matched directories - if pattern exactly matches an empty dir's name, it will just print the empty dir's content ( i.e. `[]`) - if pattern exactly matches an dir's name, it will perform pattern match and print all the dir contains e.g. ```bash mkdir src ls s?c ``` | name | type | size | modified | | ---- | ---- | ------ | --------------------------------------------- | | src | dir | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) | ----------- ```bash mkdir src mkdir scc mkdir scs ls s?c ``` | name | type | size | modified | | ---- | ---- | ------ | ------------------------------------------------ | | scc | dir | 64 B | Tue, 29 Aug 2023 16:55:31 +0900 (14 seconds ago) | | src | dir | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) | ----------- ```bash mkdir s?c ls s?c ``` print empty (i.e. ls of dir `s?c`) ----------- ```bash mkdir -p s?c/test ls s?c ``` |name|type|size|modified| |-|-|-|-| |s?c/test|dir|64 B|Tue, 29 Aug 2023 16:47:53 +0900 (2 minutes ago)| |src/bytes|dir|480 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)| |src/charting|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)| |src/conversions|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)| ----------- # User-Facing Changes User will be able to use '?' to match directory/file. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting None --------- Co-authored-by: Horasal <horsal@horsal.dev>
This commit is contained in:
@ -7,6 +7,8 @@ use nu_glob::MatchOptions;
|
||||
use nu_path::{canonicalize_with, expand_path_with};
|
||||
use nu_protocol::{ShellError, Span, Spanned};
|
||||
|
||||
const GLOB_CHARS: &[char] = &['*', '?', '['];
|
||||
|
||||
/// This function is like `nu_glob::glob` from the `glob` crate, except it is relative to a given cwd.
|
||||
///
|
||||
/// It returns a tuple of two values: the first is an optional prefix that the expanded filenames share.
|
||||
@ -27,25 +29,16 @@ pub fn glob_from(
|
||||
),
|
||||
ShellError,
|
||||
> {
|
||||
let path = PathBuf::from(&pattern.item);
|
||||
let path = expand_path_with(path, cwd);
|
||||
let is_symlink = match fs::symlink_metadata(&path) {
|
||||
Ok(attr) => attr.file_type().is_symlink(),
|
||||
Err(_) => false,
|
||||
};
|
||||
|
||||
// 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 (prefix, pattern) = if pattern.item.contains(GLOB_CHARS) {
|
||||
// Pattern contains glob, split it
|
||||
let mut p = PathBuf::new();
|
||||
let path = PathBuf::from(&pattern.item);
|
||||
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('*') {
|
||||
if os.to_string_lossy().contains(GLOB_CHARS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -53,7 +46,6 @@ pub fn glob_from(
|
||||
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) {
|
||||
@ -61,30 +53,29 @@ pub fn glob_from(
|
||||
}
|
||||
}
|
||||
|
||||
(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();
|
||||
for c in path.components() {
|
||||
if let Component::Normal(os) = c {
|
||||
if os.to_string_lossy().contains('*') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
p.push(c);
|
||||
}
|
||||
// Now expand `p` to get full prefix
|
||||
let path = expand_path_with(p, cwd);
|
||||
let escaped_prefix = PathBuf::from(nu_glob::Pattern::escape(&path.to_string_lossy()));
|
||||
|
||||
(Some(p), path)
|
||||
} else if is_symlink {
|
||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||
(Some(path), escaped_prefix.join(just_pattern))
|
||||
} else {
|
||||
let path = if let Ok(p) = canonicalize_with(path, cwd) {
|
||||
p
|
||||
} else {
|
||||
return Err(ShellError::DirectoryNotFound(pattern.span, None));
|
||||
let path = PathBuf::from(&pattern.item);
|
||||
let path = expand_path_with(path, cwd);
|
||||
let is_symlink = match fs::symlink_metadata(&path) {
|
||||
Ok(attr) => attr.file_type().is_symlink(),
|
||||
Err(_) => false,
|
||||
};
|
||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||
|
||||
if is_symlink {
|
||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||
} else {
|
||||
let path = if let Ok(p) = canonicalize_with(path, cwd) {
|
||||
p
|
||||
} else {
|
||||
return Err(ShellError::DirectoryNotFound(pattern.span, None));
|
||||
};
|
||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||
}
|
||||
};
|
||||
|
||||
let pattern = pattern.to_string_lossy().to_string();
|
||||
|
Reference in New Issue
Block a user