From c9f9078726cf4116309770f158ef410564c45670 Mon Sep 17 00:00:00 2001 From: Kamil Koczurek Date: Tue, 22 Nov 2022 15:47:12 +0100 Subject: [PATCH] Fix while ctrlc behavior (#7195) Currently while didn't respect ctrl-c, and thus non-terminating loops couldn't be interrupted. This patch fixes this. --- crates/nu-command/src/core_commands/while_.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/nu-command/src/core_commands/while_.rs b/crates/nu-command/src/core_commands/while_.rs index 0217f78d3c..5c9bccc3f3 100644 --- a/crates/nu-command/src/core_commands/while_.rs +++ b/crates/nu-command/src/core_commands/while_.rs @@ -1,3 +1,5 @@ +use std::sync::atomic::Ordering; + use nu_engine::{eval_block, eval_expression, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Block, Command, EngineState, Stack}; @@ -46,6 +48,12 @@ impl Command for While { let block: Block = call.req(engine_state, stack, 1)?; loop { + if let Some(ctrlc) = &engine_state.ctrlc { + if ctrlc.load(Ordering::SeqCst) { + break; + } + } + let result = eval_expression(engine_state, stack, cond)?; match &result { Value::Bool { val, .. } => {