mirror of
https://github.com/nushell/nushell.git
synced 2024-12-24 07:59:21 +01:00
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:
parent
6c13c67528
commit
a8b4e81408
@ -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,37 +169,80 @@ impl Command for Glob {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#[allow(clippy::needless_collect)]
|
let (not_pat, not_span) = if let Some(not_pat) = not_pattern.clone() {
|
||||||
let glob_results = glob
|
(not_pat.item, not_pat.span)
|
||||||
.walk_with_behavior(
|
} else {
|
||||||
path,
|
(String::new(), Span::test_data())
|
||||||
WalkBehavior {
|
};
|
||||||
depth: folder_depth,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.flatten();
|
|
||||||
let mut result: Vec<Value> = Vec::new();
|
|
||||||
for entry in glob_results {
|
|
||||||
if nu_utils::ctrl_c::was_pressed(&ctrlc) {
|
|
||||||
result.clear();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let file_type = entry.file_type();
|
|
||||||
|
|
||||||
if !(no_dirs && file_type.is_dir()
|
Ok(if not_pattern.is_some() {
|
||||||
|| no_files && file_type.is_file()
|
let glob_results = glob
|
||||||
|| no_symlinks && file_type.is_symlink())
|
.walk_with_behavior(
|
||||||
{
|
path,
|
||||||
result.push(Value::String {
|
WalkBehavior {
|
||||||
val: entry.into_path().to_string_lossy().to_string(),
|
depth: folder_depth,
|
||||||
span,
|
..Default::default()
|
||||||
});
|
},
|
||||||
}
|
)
|
||||||
}
|
.not([not_pat.as_str()])
|
||||||
|
.map_err(|err| {
|
||||||
Ok(result
|
ShellError::GenericError(
|
||||||
.into_iter()
|
"error with glob's not pattern".to_string(),
|
||||||
.into_pipeline_data(engine_state.ctrlc.clone()))
|
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
|
||||||
|
.walk_with_behavior(
|
||||||
|
path,
|
||||||
|
WalkBehavior {
|
||||||
|
depth: folder_depth,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.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();
|
||||||
|
for entry in glob_results {
|
||||||
|
if nu_utils::ctrl_c::was_pressed(&ctrlc) {
|
||||||
|
result.clear();
|
||||||
|
return Err(ShellError::InterruptedByUser { span: None });
|
||||||
|
}
|
||||||
|
let file_type = entry.file_type();
|
||||||
|
|
||||||
|
if !(no_dirs && file_type.is_dir()
|
||||||
|
|| no_files && file_type.is_file()
|
||||||
|
|| no_symlinks && file_type.is_symlink())
|
||||||
|
{
|
||||||
|
result.push(Value::String {
|
||||||
|
val: entry.into_path().to_string_lossy().to_string(),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user