From 3643ee6dfdce5d59d0fbded034e4d2fd0666ea07 Mon Sep 17 00:00:00 2001 From: nibon7 Date: Tue, 26 Jul 2022 01:01:10 +0800 Subject: [PATCH] Simplify print_table_or_error (#6122) Signed-off-by: nibon7 --- crates/nu-cli/src/eval_file.rs | 47 ++++++++++++++-------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index 1b247d21e9..be7630c52d 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -86,20 +86,7 @@ pub fn print_table_or_error( match table { Ok(table) => { - for item in table { - if let Value::Error { error } = item { - let working_set = StateWorkingSet::new(engine_state); - - report_error(&working_set, &error); - - std::process::exit(1); - } - - let mut out = item.into_string("\n", config); - out.push('\n'); - - let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{}", err)); - } + print_or_exit(table, engine_state, config); } Err(error) => { let working_set = StateWorkingSet::new(engine_state); @@ -111,20 +98,7 @@ pub fn print_table_or_error( } } None => { - for item in pipeline_data { - if let Value::Error { error } = item { - let working_set = StateWorkingSet::new(engine_state); - - report_error(&working_set, &error); - - std::process::exit(1); - } - - let mut out = item.into_string("\n", config); - out.push('\n'); - - let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{}", err)); - } + print_or_exit(pipeline_data, engine_state, config); } }; @@ -141,3 +115,20 @@ pub fn print_table_or_error( None } } + +fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) { + for item in pipeline_data { + if let Value::Error { error } = item { + let working_set = StateWorkingSet::new(engine_state); + + report_error(&working_set, &error); + + std::process::exit(1); + } + + let mut out = item.into_string("\n", config); + out.push('\n'); + + let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{}", err)); + } +}