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::{
ast::{Block, Call, Expr, Expression, Operator},
engine::{EngineState, Stack, Visibility},
HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream, PipelineData,
Range, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId, ENV_VARIABLE_ID,
Config, HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
PipelineData, Range, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId,
ENV_VARIABLE_ID,
};
use nu_utils::stdout_write_all_and_flush;
use std::cmp::Ordering;
@ -766,28 +767,10 @@ pub fn eval_block(
input,
)?;
for item in table {
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)?
}
print_or_return(table, config)?;
}
None => {
for item in input {
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)?
}
print_or_return(input, config)?;
}
};
@ -812,28 +795,10 @@ pub fn eval_block(
input,
)?;
for item in table {
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)?
}
print_or_return(table, config)?;
}
None => {
for item in input {
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)?
}
print_or_return(input, config)?;
}
};
}
@ -846,6 +811,21 @@ pub fn eval_block(
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(
engine_state: &EngineState,
stack: &mut Stack,