Feat: listen for signal on glob command (#9088)

Fixes : #9002 

listen for signal cancel .
other way is in listening parallel for ctrl+c wit Arc and mps channels
if this way is not a profit
This commit is contained in:
Amirhossein Akhlaghpour 2023-05-03 23:21:25 +03:30 committed by GitHub
parent 345cdef113
commit 5fcbefb7b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -115,6 +115,7 @@ impl Command for Glob {
call: &Call, call: &Call,
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let ctrlc = engine_state.ctrlc.clone();
let span = call.head; let span = call.head;
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)?;
@ -154,7 +155,7 @@ impl Command for Glob {
}; };
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
let glob_results: Vec<Value> = glob let glob_results = glob
.walk_with_behavior( .walk_with_behavior(
path, path,
WalkBehavior { WalkBehavior {
@ -162,21 +163,27 @@ impl Command for Glob {
..Default::default() ..Default::default()
}, },
) )
.flatten() .flatten();
.filter(|entry| { 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(); let file_type = entry.file_type();
!(no_dirs && file_type.is_dir() if !(no_dirs && file_type.is_dir()
|| no_files && file_type.is_file() || no_files && file_type.is_file()
|| no_symlinks && file_type.is_symlink()) || no_symlinks && file_type.is_symlink())
}) {
.map(|entry| Value::String { result.push(Value::String {
val: entry.into_path().to_string_lossy().to_string(), val: entry.into_path().to_string_lossy().to_string(),
span, span,
}) });
.collect(); }
}
Ok(glob_results Ok(result
.into_iter() .into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())) .into_pipeline_data(engine_state.ctrlc.clone()))
} }