show_empty respect use_ansi_coloring

This commit is contained in:
freshorange 2025-02-23 08:57:02 +08:00
parent 1d44843970
commit 119e62e2b4
2 changed files with 51 additions and 4 deletions

View File

@ -545,8 +545,13 @@ fn handle_record(input: CmdInput, mut record: Record) -> ShellResult<PipelineDat
let span = input.data.span().unwrap_or(input.call.head); let span = input.data.span().unwrap_or(input.call.head);
if record.is_empty() { if record.is_empty() {
let value = let value = create_empty_placeholder(
create_empty_placeholder("record", input.cfg.width, input.engine_state, input.stack); "record",
input.cfg.width,
input.engine_state,
input.stack,
input.cfg.use_ansi_coloring,
);
let value = Value::string(value, span); let value = Value::string(value, span);
return Ok(value.into_pipeline_data()); return Ok(value.into_pipeline_data());
}; };
@ -898,6 +903,7 @@ impl Iterator for PagingTableCreator {
self.table_config.width, self.table_config.width,
&self.engine_state, &self.engine_state,
&self.stack, &self.stack,
self.table_config.use_ansi_coloring,
); );
let mut bytes = result.into_bytes(); let mut bytes = result.into_bytes();
// Add extra newline if show_empty is enabled // Add extra newline if show_empty is enabled
@ -1084,6 +1090,7 @@ fn create_empty_placeholder(
termwidth: usize, termwidth: usize,
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &Stack,
use_ansi_coloring: bool,
) -> String { ) -> String {
let config = stack.get_config(engine_state); let config = stack.get_config(engine_state);
if !config.table.show_empty { if !config.table.show_empty {
@ -1099,9 +1106,10 @@ fn create_empty_placeholder(
let style_computer = &StyleComputer::from_config(engine_state, stack); let style_computer = &StyleComputer::from_config(engine_state, stack);
configure_table(&mut out, &config, style_computer, TableMode::default()); configure_table(&mut out, &config, style_computer, TableMode::default());
out.table let ret = out.table
.draw(termwidth) .draw(termwidth)
.expect("Could not create empty table placeholder") .expect("Could not create empty table placeholder");
maybe_strip_color(ret, use_ansi_coloring)
} }
fn convert_table_to_output( fn convert_table_to_output(

View File

@ -3120,6 +3120,45 @@ fn table_colors() {
assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯"); assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯");
} }
#[test]
fn empty_table_colors() {
let actual = nu!(concat!(
"$env.config.use_ansi_coloring = true;",
"{}",
));
assert_eq!(
actual.out,
"\u{1b}[37m╭──────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty record\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰──────────────╯\u{1b}[0m"
);
let actual = nu!(concat!(
"$env.config.use_ansi_coloring = true;",
"[]",
));
assert_eq!(
actual.out,
"\u{1b}[37m╭────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty list\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰────────────╯\u{1b}[0m"
);
let actual = nu!(concat!(
"$env.config.use_ansi_coloring = false;",
"{}",
));
assert_eq!(
actual.out,
"╭──────────────╮│ empty record │╰──────────────╯"
);
let actual = nu!(concat!(
"$env.config.use_ansi_coloring = false;",
"[]",
));
assert_eq!(
actual.out,
"╭────────────╮│ empty list │╰────────────╯"
);
}
#[test] #[test]
fn table_index() { fn table_index() {
let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80"); let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80");