Fix the way the output of table is printed in print() (#12895)

# Description

Forgot that I fixed this already on my branch, but when printing without
a display output hook, the implicit call to `table` gets its output
mangled with newlines (since #12774). This happens when running `nu -c`
or a script file.

Here's that fix in one PR so it can be merged easily.

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
This commit is contained in:
Devyn Cairns 2024-05-17 07:18:18 -07:00 committed by GitHub
parent 8adf3406e5
commit 59f7c523fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -543,16 +543,15 @@ impl PipelineData {
if let Some(decl_id) = engine_state.table_decl_id { if let Some(decl_id) = engine_state.table_decl_id {
let command = engine_state.get_decl(decl_id); let command = engine_state.get_decl(decl_id);
if command.get_block_id().is_some() { if command.get_block_id().is_some() {
self.write_all_and_flush(engine_state, no_newline, to_stderr)?; self.write_all_and_flush(engine_state, no_newline, to_stderr)
} else { } else {
let call = Call::new(Span::new(0, 0)); let call = Call::new(Span::new(0, 0));
let table = command.run(engine_state, stack, &call, self)?; let table = command.run(engine_state, stack, &call, self)?;
table.write_all_and_flush(engine_state, no_newline, to_stderr)?; table.write_all_and_flush(engine_state, no_newline, to_stderr)
} }
} else { } else {
self.write_all_and_flush(engine_state, no_newline, to_stderr)?; self.write_all_and_flush(engine_state, no_newline, to_stderr)
} }
Ok(None)
} }
} }
@ -561,27 +560,32 @@ impl PipelineData {
engine_state: &EngineState, engine_state: &EngineState,
no_newline: bool, no_newline: bool,
to_stderr: bool, to_stderr: bool,
) -> Result<(), ShellError> { ) -> Result<Option<ExitStatus>, ShellError> {
let config = engine_state.get_config(); if let PipelineData::ByteStream(stream, ..) = self {
for item in self { // Copy ByteStreams directly
let mut out = if let Value::Error { error, .. } = item { stream.print(to_stderr)
return Err(*error); } else {
} else { let config = engine_state.get_config();
item.to_expanded_string("\n", config) for item in self {
}; let mut out = if let Value::Error { error, .. } = item {
return Err(*error);
} else {
item.to_expanded_string("\n", config)
};
if !no_newline { if !no_newline {
out.push('\n'); out.push('\n');
}
if to_stderr {
stderr_write_all_and_flush(out)?
} else {
stdout_write_all_and_flush(out)?
}
} }
if to_stderr { Ok(None)
stderr_write_all_and_flush(out)?
} else {
stdout_write_all_and_flush(out)?
}
} }
Ok(())
} }
} }