From b9be416937074fdeb5b337fc2dfda165e84be5a9 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Mon, 20 Feb 2023 15:59:33 +0300 Subject: [PATCH] Add colors in `table --collapse` (#8120) close #8110 cc: @amtoine --------- Signed-off-by: Maxim Zhiburt --- crates/nu-command/src/viewers/table.rs | 49 ++++++++++++++++++++------ 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index b6798ab50b..b8ee4adbf6 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -388,12 +388,14 @@ fn build_collapsed_table( config: &Config, term_width: usize, ) -> Result, ShellError> { - let value = Value::Record { + let mut value = Value::Record { cols, vals, span: Span::new(0, 0), }; + colorize_value(&mut value, config, style_computer); + let theme = load_theme_from_config(config); let table = nu_table::NuTable::new( value, @@ -1000,10 +1002,7 @@ fn convert_to_table2<'a>( if with_index { if with_header { - data[0].push(NuTable::create_cell( - "#", - header_style(style_computer, String::from("#")), - )); + data[0].push(NuTable::create_cell("#", header_style(style_computer, "#"))); } let mut last_index = 0; @@ -1145,8 +1144,7 @@ fn convert_to_table2<'a>( } } - let head_cell = - NuTable::create_cell(header.clone(), header_style(style_computer, header.clone())); + let head_cell = NuTable::create_cell(header.clone(), header_style(style_computer, &header)); data[0].push(head_cell); for (row, item) in input.clone().enumerate() { @@ -1268,8 +1266,8 @@ fn lookup_index_value(item: &Value, config: &Config) -> Option { .map(|value| value.into_string("", config)) } -fn header_style(style_computer: &StyleComputer, header: String) -> TextStyle { - let style = style_computer.compute("header", &Value::string(header.as_str(), Span::unknown())); +fn header_style(style_computer: &StyleComputer, header: &str) -> TextStyle { + let style = style_computer.compute("header", &Value::string(header, Span::unknown())); TextStyle { alignment: Alignment::Center, color_style: Some(style), @@ -1605,11 +1603,13 @@ impl PagingTableCreator { let term_width = get_width_param(self.width_param); let need_footer = matches!(config.footer_mode, FooterMode::RowCount(limit) if batch.len() as u64 > limit) || matches!(config.footer_mode, FooterMode::Always); - let value = Value::List { + let mut value = Value::List { vals: batch, span: Span::new(0, 0), }; + colorize_value(&mut value, config, &style_computer); + let table = nu_table::NuTable::new( value, true, @@ -1848,3 +1848,32 @@ fn need_footer(config: &Config, count_records: u64) -> bool { matches!(config.footer_mode, FooterMode::RowCount(limit) if count_records > limit) || matches!(config.footer_mode, FooterMode::Always) } + +fn colorize_value(value: &mut Value, config: &Config, style_computer: &StyleComputer) { + match value { + Value::Record { cols, vals, .. } => { + for val in vals { + colorize_value(val, config, style_computer); + } + + let style = header_style(style_computer, ""); + if let Some(color) = style.color_style { + for header in cols { + *header = color.paint(header.to_owned()).to_string(); + } + } + } + Value::List { vals, .. } => { + for val in vals { + colorize_value(val, config, style_computer); + } + } + val => { + let (text, style) = value_to_styled_string(val, config, style_computer); + if let Some(color) = style.color_style { + let text = color.paint(text); + *val = Value::string(text.to_string(), val.span().unwrap_or(Span::unknown())); + } + } + } +}