Simplify eval_block (#6121)

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7 2022-07-26 01:00:31 +08:00 committed by GitHub
parent daa2148136
commit 32e4535f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,9 @@ use nu_path::expand_path_with;
use nu_protocol::{ use nu_protocol::{
ast::{Block, Call, Expr, Expression, Operator}, ast::{Block, Call, Expr, Expression, Operator},
engine::{EngineState, Stack, Visibility}, engine::{EngineState, Stack, Visibility},
HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream, PipelineData, Config, HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
Range, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId, ENV_VARIABLE_ID, PipelineData, Range, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId,
ENV_VARIABLE_ID,
}; };
use nu_utils::stdout_write_all_and_flush; use nu_utils::stdout_write_all_and_flush;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -766,28 +767,10 @@ pub fn eval_block(
input, input,
)?; )?;
for item in table { print_or_return(table, config)?;
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", config);
out.push('\n');
stdout_write_all_and_flush(out)?
}
} }
None => { None => {
for item in input { print_or_return(input, config)?;
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", config);
out.push('\n');
stdout_write_all_and_flush(out)?
}
} }
}; };
@ -812,28 +795,10 @@ pub fn eval_block(
input, input,
)?; )?;
for item in table { print_or_return(table, config)?;
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", config);
out.push('\n');
stdout_write_all_and_flush(out)?
}
} }
None => { None => {
for item in input { print_or_return(input, config)?;
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", config);
out.push('\n');
stdout_write_all_and_flush(out)?
}
} }
}; };
} }
@ -846,6 +811,21 @@ pub fn eval_block(
Ok(input) Ok(input)
} }
fn print_or_return(pipeline_data: PipelineData, config: &Config) -> Result<(), ShellError> {
for item in pipeline_data {
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", config);
out.push('\n');
stdout_write_all_and_flush(out)?;
}
Ok(())
}
pub fn eval_subexpression( pub fn eval_subexpression(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,