summaryrefslogtreecommitdiffstats
path: root/vendor/papergrid/examples/common_grid.rs
blob: a0fc8d4c7541640242c16d01fad6219270dacc9c (plain)
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
//! This example demonstrates the flexibility of [`papergrid`] with manual configurations
//! of [`Borders`], [`CompactConfig`], and column counts with [`IterRecords`].
//!
//! * For an alternative to [`CompactGrid`] and [`CompactGridDimension`] with
//! flexible row height, variable intra-column spans, and multiline cell support
//! see [`Grid`] and [`SpannedGridDimension`].

use papergrid::{
    config::compact::CompactConfig,
    config::{AlignmentHorizontal, Borders, Indent, Sides},
    dimension::compact::CompactGridDimension,
    dimension::Estimate,
    grid::compact::CompactGrid,
    records::IterRecords,
};

fn main() {
    let cfg = generate_table_config();

    let data = [
        ["Papergrid", "is a library", "for printing tables", "!"],
        [
            "Just like this",
            "NOTICE",
            "that multiline is not supported",
            "H\ne\nl\nl\no",
        ],
    ];
    let records = IterRecords::new(data, 4, None);

    let mut dim = CompactGridDimension::default();
    dim.estimate(records, &cfg);

    let grid = CompactGrid::new(records, &dim, &cfg);

    println!("{grid}");
}

fn generate_table_config() -> CompactConfig {
    const STYLE: Borders<char> = Borders {
        top: Some('-'),
        top_left: Some('+'),
        top_right: Some('+'),
        top_intersection: Some('+'),
        bottom: Some('-'),
        bottom_left: Some('+'),
        bottom_right: Some('+'),
        bottom_intersection: Some('+'),
        horizontal: Some('-'),
        left_intersection: Some('+'),
        right_intersection: Some('+'),
        vertical: Some('|'),
        left: Some('|'),
        right: Some('|'),
        intersection: Some('+'),
    };

    CompactConfig::default()
        .set_borders(STYLE)
        .set_alignment_horizontal(AlignmentHorizontal::Center)
        .set_padding(Sides::new(
            Indent::spaced(1),
            Indent::spaced(1),
            Indent::spaced(0),
            Indent::spaced(0),
        ))
}