2022-02-18 19:43:34 +01:00
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{EngineState, Stack},
|
|
|
|
PipelineData, ShellError, Span, Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub 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();
|
|
|
|
|
2022-03-08 02:17:33 +01:00
|
|
|
if let PipelineData::ExternalStream {
|
2022-03-10 12:29:23 +01:00
|
|
|
stdout: stream,
|
|
|
|
exit_code,
|
2022-03-08 02:17:33 +01:00
|
|
|
..
|
|
|
|
} = input
|
|
|
|
{
|
2022-03-10 12:29:23 +01:00
|
|
|
if let Some(stream) = stream {
|
|
|
|
for s in stream {
|
|
|
|
let _ = stdout.lock().write_all(s?.as_binary()?);
|
|
|
|
}
|
2022-02-18 19:43:34 +01:00
|
|
|
}
|
2022-03-10 12:29:23 +01:00
|
|
|
|
|
|
|
// Make sure everything has finished
|
|
|
|
if let Some(exit_code) = exit_code {
|
|
|
|
let _: Vec<_> = exit_code.into_iter().collect();
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:43:34 +01:00
|
|
|
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(())
|
|
|
|
}
|