Flush stmts (#584)

* Flush the stmt via table to the screen

* Fix test
This commit is contained in:
JT 2021-12-27 07:21:24 +11:00 committed by GitHub
parent 89a000a572
commit e62e0fb679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 2 deletions

View File

@ -1,4 +1,5 @@
use std::cmp::Ordering;
use std::io::Write;
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::engine::{EngineState, Stack};
@ -383,7 +384,9 @@ pub fn eval_block(
block: &Block,
mut input: PipelineData,
) -> Result<PipelineData, ShellError> {
for stmt in block.stmts.iter() {
let config = stack.get_config().unwrap_or_default();
let num_stmts = block.stmts.len();
for (stmt_idx, stmt) in block.stmts.iter().enumerate() {
if let Statement::Pipeline(pipeline) = stmt {
for (i, elem) in pipeline.expressions.iter().enumerate() {
match elem {
@ -414,6 +417,56 @@ pub fn eval_block(
}
}
}
if stmt_idx < (num_stmts) - 1 {
// Drain the input to the screen via tabular output
match engine_state.find_decl("table".as_bytes()) {
Some(decl_id) => {
let table = engine_state.get_decl(decl_id).run(
engine_state,
stack,
&Call::new(),
input,
)?;
for item in table {
let stdout = std::io::stdout();
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", &config);
out.push('\n');
match stdout.lock().write_all(out.as_bytes()) {
Ok(_) => (),
Err(err) => eprintln!("{}", err),
};
}
}
None => {
for item in input {
let stdout = std::io::stdout();
if let Value::Error { error } = item {
return Err(error);
}
let mut out = item.into_string("\n", &config);
out.push('\n');
match stdout.lock().write_all(out.as_bytes()) {
Ok(_) => (),
Err(err) => eprintln!("{}", err),
};
}
}
};
input = PipelineData::new(Span { start: 0, end: 0 })
}
}
Ok(input)

View File

@ -3,7 +3,7 @@ use crate::tests::{fail_test, run_test, TestResult};
#[test]
fn concrete_variable_assignment() -> TestResult {
run_test(
"let x = (1..100 | each { |y| $y + 100 }); $x | length; $x | length",
"let x = (1..100 | each { |y| $y + 100 }); let y = ($x | length); $x | length",
"100",
)
}