diff --git a/crates/nu-table/src/common.rs b/crates/nu-table/src/common.rs index c35eecf2f0..f2a3b48ec9 100644 --- a/crates/nu-table/src/common.rs +++ b/crates/nu-table/src/common.rs @@ -30,6 +30,20 @@ pub fn create_nu_table_config( } } +pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, style: &StyleComputer) -> String { + let (mut text, value_style) = nu_value_to_string(val, cfg, style); + if let Some(color) = value_style.color_style { + text = color.paint(text).to_string(); + } + + if matches!(val, Value::String { .. }) { + text = clean_charset(&text); + colorize_space_str(&mut text, style); + } + + text +} + pub fn nu_value_to_string(val: &Value, cfg: &Config, style: &StyleComputer) -> NuText { let float_precision = cfg.float_precision as usize; let text = val.into_abbreviated_string(cfg); diff --git a/crates/nu-table/src/types/expanded.rs b/crates/nu-table/src/types/expanded.rs index 3b2ad16b87..9e5c11928e 100644 --- a/crates/nu-table/src/types/expanded.rs +++ b/crates/nu-table/src/types/expanded.rs @@ -9,8 +9,9 @@ use tabled::grid::config::Position; use crate::{ common::{ create_nu_table_config, error_sign, get_header_style, get_index_style, - load_theme_from_config, nu_value_to_string, nu_value_to_string_clean, wrap_text, NuText, - StringResult, TableResult, INDEX_COLUMN_NAME, + load_theme_from_config, nu_value_to_string, nu_value_to_string_clean, + nu_value_to_string_colored, wrap_text, NuText, StringResult, TableResult, + INDEX_COLUMN_NAME, }, string_width, NuTable, NuTableCell, TableOpts, TableOutput, }; @@ -448,10 +449,10 @@ fn expand_table_value( ))), } } - _ => Ok(Some(( - value_to_wrapped_string_clean(value, cfg, value_width), - false, - ))), + _ => { + let text = value_to_wrapped_string_clean(value, cfg, value_width); + Ok(Some((text, false))) + } } } @@ -607,9 +608,6 @@ fn value_to_wrapped_string(value: &Value, cfg: &Cfg<'_>, value_width: usize) -> } fn value_to_wrapped_string_clean(value: &Value, cfg: &Cfg<'_>, value_width: usize) -> String { - wrap_text( - &value_to_string_clean(value, cfg), - value_width, - cfg.opts.config, - ) + let text = nu_value_to_string_colored(value, cfg.opts.config, cfg.opts.style_computer); + wrap_text(&text, value_width, cfg.opts.config) } diff --git a/crates/nu-table/src/types/general.rs b/crates/nu-table/src/types/general.rs index 5301c683d6..c76c319b9f 100644 --- a/crates/nu-table/src/types/general.rs +++ b/crates/nu-table/src/types/general.rs @@ -6,7 +6,7 @@ use crate::{ clean_charset, colorize_space, common::{ create_nu_table_config, get_empty_style, get_header_style, get_index_style, - get_value_style, NuText, INDEX_COLUMN_NAME, + get_value_style, nu_value_to_string_colored, NuText, INDEX_COLUMN_NAME, }, NuTable, NuTableCell, StringResult, TableOpts, TableOutput, TableResult, }; @@ -47,20 +47,15 @@ fn kv_table(record: &Record, opts: TableOpts<'_>) -> StringResult { return Ok(None); } - let is_string_value = matches!(value, Value::String { .. }); - let mut value = value.into_abbreviated_string(opts.config); - if is_string_value { - value = clean_charset(&value); - } + let value = nu_value_to_string_colored(value, opts.config, opts.style_computer); let key = NuTableCell::new(column.to_string()); let value = NuTableCell::new(value); + row.push(key); row.push(value); } - colorize_space(&mut data, opts.style_computer); - let mut table = NuTable::from(data); table.set_index_style(TextStyle::default_field());