From 5fcbefb7b4998874b1d6aa1034038861b792f0ff Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Wed, 3 May 2023 23:21:25 +0330 Subject: [PATCH] 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 --- crates/nu-command/src/filesystem/glob.rs | 35 ++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/crates/nu-command/src/filesystem/glob.rs b/crates/nu-command/src/filesystem/glob.rs index 20245f0bb4..a599cd225f 100644 --- a/crates/nu-command/src/filesystem/glob.rs +++ b/crates/nu-command/src/filesystem/glob.rs @@ -115,6 +115,7 @@ impl Command for Glob { call: &Call, _input: PipelineData, ) -> Result { + let ctrlc = engine_state.ctrlc.clone(); let span = call.head; let path = current_dir(engine_state, stack)?; let glob_pattern: Spanned = call.req(engine_state, stack, 0)?; @@ -154,7 +155,7 @@ impl Command for Glob { }; #[allow(clippy::needless_collect)] - let glob_results: Vec = glob + let glob_results = glob .walk_with_behavior( path, WalkBehavior { @@ -162,21 +163,27 @@ impl Command for Glob { ..Default::default() }, ) - .flatten() - .filter(|entry| { - let file_type = entry.file_type(); + .flatten(); + let mut result: Vec = Vec::new(); + for entry in glob_results { + if nu_utils::ctrl_c::was_pressed(&ctrlc) { + result.clear(); + break; + } + let file_type = entry.file_type(); - !(no_dirs && file_type.is_dir() - || no_files && file_type.is_file() - || no_symlinks && file_type.is_symlink()) - }) - .map(|entry| Value::String { - val: entry.into_path().to_string_lossy().to_string(), - span, - }) - .collect(); + 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(glob_results + Ok(result .into_iter() .into_pipeline_data(engine_state.ctrlc.clone())) }