Add an explicit 'print' command (#4535)

This commit is contained in:
JT
2022-02-18 13:43:34 -05:00
committed by GitHub
parent 786e4ab971
commit 06f9047be4
6 changed files with 151 additions and 71 deletions

View File

@ -51,6 +51,7 @@ fn main() -> Result<()> {
let delta = {
let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state);
working_set.add_decl(Box::new(nu_cli::NuHighlight));
working_set.add_decl(Box::new(nu_cli::Print));
working_set.render()
};

View File

@ -1,13 +1,12 @@
use log::trace;
use nu_cli::CliError;
use nu_cli::{print_pipeline_data, CliError};
use nu_engine::eval_block;
use nu_parser::{lex, parse, trim_quotes, Token, TokenContents};
use nu_protocol::{
ast::Call,
engine::{EngineState, Stack, StateWorkingSet},
PipelineData, ShellError, Span, Value,
PipelineData, ShellError, Value,
};
use std::{io::Write, path::PathBuf};
use std::path::PathBuf;
// This will collect environment variables from std::env and adds them to a stack.
//
@ -188,72 +187,6 @@ pub(crate) fn gather_parent_env_vars(engine_state: &mut EngineState) {
}
}
fn print_pipeline_data(
input: PipelineData,
engine_state: &EngineState,
stack: &mut Stack,
) -> Result<(), ShellError> {
// If the table function is in the declarations, then we can use it
// to create the table value that will be printed in the terminal
let config = stack.get_config().unwrap_or_default();
let stdout = std::io::stdout();
if let PipelineData::RawStream(stream, _, _) = input {
for s in stream {
let _ = stdout.lock().write_all(s?.as_binary()?);
}
return Ok(());
}
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(Span::new(0, 0)),
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),
};
}
}
};
Ok(())
}
pub(crate) fn eval_source(
engine_state: &mut EngineState,
stack: &mut Stack,