Skip to content

Commit

Permalink
nu-table: Use config color scheme in kv tables and table -e (#10720)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiburt committed Oct 15, 2023
1 parent 1f62024 commit 4e5a1ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
14 changes: 14 additions & 0 deletions crates/nu-table/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions crates/nu-table/src/types/expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)))
}
}
}

Expand Down Expand Up @@ -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)
}
11 changes: 3 additions & 8 deletions crates/nu-table/src/types/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 4e5a1ce

Please sign in to comment.