Add colors in table --collapse (#8120)

close #8110 

cc: @amtoine

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-02-20 15:59:33 +03:00 committed by GitHub
parent 0a8c9b22b0
commit b9be416937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -388,12 +388,14 @@ fn build_collapsed_table(
config: &Config,
term_width: usize,
) -> Result<Option<String>, 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<String> {
.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()));
}
}
}
}