1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use nu_color_config::StyleComputer;
use nu_protocol::{Config, Span, Value};

use crate::UnstructuredTable;

use crate::common::nu_value_to_string_clean;
use crate::{
    common::{get_index_style, load_theme_from_config},
    StringResult, TableOpts,
};

pub struct CollapsedTable;

impl CollapsedTable {
    pub fn build(value: Value, opts: TableOpts<'_>) -> StringResult {
        collapsed_table(value, opts.config, opts.width, opts.style_computer)
    }
}

fn collapsed_table(
    mut value: Value,
    config: &Config,
    term_width: usize,
    style_computer: &StyleComputer,
) -> StringResult {
    colorize_value(&mut value, config, style_computer);

    let theme = load_theme_from_config(config);
    let mut table = UnstructuredTable::new(value, config);
    let is_empty = table.truncate(&theme, term_width);
    if is_empty {
        return Ok(None);
    }

    let indent = (config.table_indent.left, config.table_indent.right);
    let table = table.draw(style_computer, &theme, indent);

    Ok(Some(table))
}

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 = get_index_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);
            }
        }
        value => {
            let (text, style) = nu_value_to_string_clean(value, config, style_computer);
            if let Some(color) = style.color_style {
                let text = color.paint(text).to_string();
                let span = value.span().unwrap_or(Span::unknown());
                *value = Value::string(text, span);
            }
        }
    }
}