diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index c8d7afa47b..76376ebf78 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -8,7 +8,7 @@ use std::sync::mpsc; use nu_engine::env_to_strings; use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::{ast::Call, engine::Command, ShellError, Signature, SyntaxShape, Value}; -use nu_protocol::{Category, Config, PipelineData, RawStream, Span, Spanned}; +use nu_protocol::{Category, PipelineData, RawStream, Span, Spanned}; use itertools::Itertools; @@ -96,7 +96,7 @@ impl Command for External { last_expression, env_vars: env_vars_str, }; - command.run_with_input(engine_state, input, config) + command.run_with_input(engine_state, stack, input) } } @@ -111,8 +111,8 @@ impl ExternalCommand { pub fn run_with_input( &self, engine_state: &EngineState, + stack: &mut Stack, input: PipelineData, - config: Config, ) -> Result { let head = self.name.span; @@ -155,33 +155,42 @@ impl ExternalCommand { self.name.span, )), Ok(mut child) => { - // if there is a string or a stream, that is sent to the pipe std - if let Some(mut stdin_write) = child.stdin.take() { - std::thread::spawn(move || { - for value in input.into_iter() { - match value { - Value::String { val, span: _ } => { - if stdin_write.write(val.as_bytes()).is_err() { - return Ok(()); - } - } - Value::Binary { val, span: _ } => { - if stdin_write.write(&val).is_err() { - return Ok(()); - } - } - x => { - if stdin_write - .write(x.into_string(", ", &config).as_bytes()) - .is_err() - { + if !input.is_nothing() { + let engine_state = engine_state.clone(); + let mut stack = stack.clone(); + stack.update_config( + "use_ansi_coloring", + Value::Bool { + val: false, + span: Span::new(0, 0), + }, + ); + // if there is a string or a stream, that is sent to the pipe std + if let Some(mut stdin_write) = child.stdin.take() { + std::thread::spawn(move || { + let input = crate::Table::run( + &crate::Table, + &engine_state, + &mut stack, + &Call::new(), + input, + ); + + if let Ok(input) = input { + for value in input.into_iter() { + if let Value::String { val, span: _ } = value { + if stdin_write.write(val.as_bytes()).is_err() { + return Ok(()); + } + } else { return Err(()); } } } - } - Ok(()) - }); + + Ok(()) + }); + } } let last_expression = self.last_expression; diff --git a/crates/nu-protocol/src/engine/stack.rs b/crates/nu-protocol/src/engine/stack.rs index 0e2a565e1e..07faaad85c 100644 --- a/crates/nu-protocol/src/engine/stack.rs +++ b/crates/nu-protocol/src/engine/stack.rs @@ -178,10 +178,20 @@ impl Stack { match config { Ok(config) => config.into_config(), - Err(e) => { - println!("Can't find {} in {:?}", CONFIG_VARIABLE_ID, self); - Err(e) + Err(e) => Err(e), + } + } + + pub fn update_config(&mut self, name: &str, value: Value) { + if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) { + for col_val in cols.iter().zip(vals.iter_mut()) { + if col_val.0 == name { + *col_val.1 = value; + return; + } } + cols.push(name.to_string()); + vals.push(value); } } diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 585813b4b6..c095d25bef 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -75,6 +75,10 @@ impl PipelineData { self } + pub fn is_nothing(&self) -> bool { + matches!(self, PipelineData::Value(Value::Nothing { .. }, ..)) + } + pub fn into_value(self, span: Span) -> Value { match self { PipelineData::Value(Value::Nothing { .. }, ..) => Value::nothing(span),