table_demo/
table_demo.rs

1use nu_ansi_term::{Color, Style};
2use nu_color_config::TextStyle;
3use nu_table::{NuTable, TableTheme};
4use tabled::grid::records::vec_records::Text;
5
6fn main() {
7    let args: Vec<_> = std::env::args().collect();
8    let mut width = 0;
9
10    if args.len() > 1 {
11        width = args[1].parse::<usize>().expect("Need a width in columns");
12    }
13
14    if width < 4 {
15        println!("Width must be greater than or equal to 4, setting width to 80");
16        width = 80;
17    }
18
19    let (table_headers, row_data) = make_table_data();
20
21    let headers = to_cell_info_vec(&table_headers);
22    let rows = to_cell_info_vec(&row_data);
23
24    let mut rows = vec![rows; 3];
25    rows.insert(0, headers);
26
27    let mut table = NuTable::from(rows);
28
29    table.set_data_style(TextStyle::basic_left());
30    table.set_header_style(TextStyle::basic_center().style(Style::new().on(Color::Blue)));
31    table.set_theme(TableTheme::rounded());
32    table.set_structure(false, true, false);
33
34    let output_table = table
35        .draw(width)
36        .unwrap_or_else(|| format!("Couldn't fit table into {width} columns!"));
37
38    println!("{output_table}")
39}
40
41fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) {
42    let table_headers = vec![
43        "category",
44        "description",
45        "emoji",
46        "ios_version",
47        "unicode_version",
48        "aliases",
49        "tags",
50        "category2",
51        "description2",
52        "emoji2",
53        "ios_version2",
54        "unicode_version2",
55        "aliases2",
56        "tags2",
57    ];
58
59    let row_data = vec![
60        "Smileys & Emotion",
61        "grinning face",
62        "😀",
63        "6",
64        "6.1",
65        "grinning",
66        "smile",
67        "Smileys & Emotion",
68        "grinning face",
69        "😀",
70        "6",
71        "6.1",
72        "grinning",
73        "smile",
74    ];
75
76    (table_headers, row_data)
77}
78
79fn to_cell_info_vec(data: &[&str]) -> Vec<Text<String>> {
80    let mut v = vec![];
81    for x in data {
82        v.push(Text::new(String::from(*x)));
83    }
84
85    v
86}