nu-table: Use config color scheme in kv tables and table -e (#10720)

fix #10712
cc: @fdncred
This commit is contained in:
Maxim Zhiburt 2023-10-15 00:25:00 +00:00 committed by GitHub
parent 1f62024a15
commit 4e5a1ced13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 19 deletions

View File

@ -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);

View File

@ -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)
}

View File

@ -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());