diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 7f818335ad..6b966d1447 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -467,55 +467,47 @@ impl PipelineData { self, )?; - for item in table { - let mut out = if let Value::Error { error } = item { - let working_set = StateWorkingSet::new(engine_state); - - format_error(&working_set, &error) - } else if no_newline { - item.into_string("", config) - } else { - item.into_string("\n", config) - }; - - if !no_newline { - out.push('\n'); - } - - if !to_stderr { - stdout_write_all_and_flush(out)? - } else { - stderr_write_all_and_flush(out)? - } - } + table.write_all_and_flush(engine_state, config, no_newline, to_stderr)?; } None => { - for item in self { - let mut out = if let Value::Error { error } = item { - let working_set = StateWorkingSet::new(engine_state); - - format_error(&working_set, &error) - } else if no_newline { - item.into_string("", config) - } else { - item.into_string("\n", config) - }; - - if !no_newline { - out.push('\n'); - } - - if !to_stderr { - stdout_write_all_and_flush(out)? - } else { - stderr_write_all_and_flush(out)? - } - } + self.write_all_and_flush(engine_state, config, no_newline, to_stderr)?; } }; Ok(()) } + + fn write_all_and_flush( + self, + engine_state: &EngineState, + config: &Config, + no_newline: bool, + to_stderr: bool, + ) -> Result<(), ShellError> { + for item in self { + let mut out = if let Value::Error { error } = item { + let working_set = StateWorkingSet::new(engine_state); + + format_error(&working_set, &error) + } else if no_newline { + item.into_string("", config) + } else { + item.into_string("\n", config) + }; + + if !no_newline { + out.push('\n'); + } + + if !to_stderr { + stdout_write_all_and_flush(out)? + } else { + stderr_write_all_and_flush(out)? + } + } + + Ok(()) + } } pub struct PipelineIterator(PipelineData);