add a negation glob option to the glob command (#9153)

# Description
This PR adds the ability to add a negation glob.

Normal Example:
```
> glob **/tsconfig.json
╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ 0 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\client\node_modules\big-integer\tsconfig.json │
│ 1 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\client\tsconfig.json                          │
│ 2 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\node_modules\fastq\test\tsconfig.json         │
│ 3 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\node_modules\jszip\tsconfig.json              │
│ 4 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\server\tsconfig.json                          │
│ 5 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\tsconfig.json                                 │
╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────╯
```
Negation Example:
```
> glob **/tsconfig.json --not **/node_modules/**
╭───┬───────────────────────────────────────────────────────────────────────────────╮
│ 0 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\client\tsconfig.json │
│ 1 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\server\tsconfig.json │
│ 2 │ C:\Users\username\source\repos\forks\vscode-nushell-lang\tsconfig.json        │
╰───┴───────────────────────────────────────────────────────────────────────────────╯
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Darren Schroeder 2023-05-10 06:31:34 -05:00 committed by GitHub
parent 6c13c67528
commit a8b4e81408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,15 @@
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use nu_engine::env::current_dir; use nu_engine::env::current_dir;
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Spanned, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value, Spanned, SyntaxShape, Type, Value,
}; };
use wax::{Glob as WaxGlob, WalkBehavior}; use wax::{Glob as WaxGlob, WalkBehavior, WalkEntry};
#[derive(Clone)] #[derive(Clone)]
pub struct Glob; pub struct Glob;
@ -41,6 +44,12 @@ impl Command for Glob {
"Whether to filter out symlinks from the returned paths", "Whether to filter out symlinks from the returned paths",
Some('S'), Some('S'),
) )
.named(
"not",
SyntaxShape::String,
"Pattern to exclude from the results",
Some('n'),
)
.category(Category::FileSystem) .category(Category::FileSystem)
} }
@ -101,6 +110,12 @@ impl Command for Glob {
example: r#"glob "[A-Z]*" --no-file --no-symlink"#, example: r#"glob "[A-Z]*" --no-file --no-symlink"#,
result: None, result: None,
}, },
Example {
description: "Search for files named tsconfig.json that are not in node_modules directories",
example: r#"glob **/tsconfig.json --not **/node_modules/**"#,
result: None,
},
] ]
} }
@ -120,10 +135,10 @@ impl Command for Glob {
let path = current_dir(engine_state, stack)?; let path = current_dir(engine_state, stack)?;
let glob_pattern: Spanned<String> = call.req(engine_state, stack, 0)?; let glob_pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
let depth = call.get_flag(engine_state, stack, "depth")?; let depth = call.get_flag(engine_state, stack, "depth")?;
let no_dirs = call.has_flag("no-dir"); let no_dirs = call.has_flag("no-dir");
let no_files = call.has_flag("no-file"); let no_files = call.has_flag("no-file");
let no_symlinks = call.has_flag("no-symlink"); let no_symlinks = call.has_flag("no-symlink");
let not_pattern: Option<Spanned<String>> = call.get_flag(engine_state, stack, "not")?;
if glob_pattern.item.is_empty() { if glob_pattern.item.is_empty() {
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
@ -154,7 +169,37 @@ impl Command for Glob {
} }
}; };
#[allow(clippy::needless_collect)] let (not_pat, not_span) = if let Some(not_pat) = not_pattern.clone() {
(not_pat.item, not_pat.span)
} else {
(String::new(), Span::test_data())
};
Ok(if not_pattern.is_some() {
let glob_results = glob
.walk_with_behavior(
path,
WalkBehavior {
depth: folder_depth,
..Default::default()
},
)
.not([not_pat.as_str()])
.map_err(|err| {
ShellError::GenericError(
"error with glob's not pattern".to_string(),
format!("{err}"),
Some(not_span),
None,
Vec::new(),
)
})?
.flatten();
let result = glob_to_value(ctrlc, glob_results, no_dirs, no_files, no_symlinks, span)?;
result
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())
} else {
let glob_results = glob let glob_results = glob
.walk_with_behavior( .walk_with_behavior(
path, path,
@ -164,11 +209,27 @@ impl Command for Glob {
}, },
) )
.flatten(); .flatten();
let result = glob_to_value(ctrlc, glob_results, no_dirs, no_files, no_symlinks, span)?;
result
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())
})
}
}
fn glob_to_value<'a>(
ctrlc: Option<Arc<AtomicBool>>,
glob_results: impl Iterator<Item = WalkEntry<'a>>,
no_dirs: bool,
no_files: bool,
no_symlinks: bool,
span: Span,
) -> Result<Vec<Value>, ShellError> {
let mut result: Vec<Value> = Vec::new(); let mut result: Vec<Value> = Vec::new();
for entry in glob_results { for entry in glob_results {
if nu_utils::ctrl_c::was_pressed(&ctrlc) { if nu_utils::ctrl_c::was_pressed(&ctrlc) {
result.clear(); result.clear();
break; return Err(ShellError::InterruptedByUser { span: None });
} }
let file_type = entry.file_type(); let file_type = entry.file_type();
@ -183,8 +244,5 @@ impl Command for Glob {
} }
} }
Ok(result Ok(result)
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
}
} }