summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/examples/formatting_settings.rs
blob: 19c8a480635a18a75e2d77335ca56e9e4986eed2 (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
//! This example demonstrates using the [`Alignment`], [`AlignmentStrategy`], and [`TrimStrategy`] [`CellOptions`]
//! to align the content of a [`Table`] in several nuanced ways.
//!
//! * Note how [`AlignmentStrategy`] and [`TrimStrategy`] provide useful tools for managing multiline cells and
//! cell values that are bloated with whitespace.

use tabled::{
    settings::{
        formatting::{AlignmentStrategy, TrimStrategy},
        object::Segment,
        Alignment, Modify, Style,
    },
    Table,
};

fn main() {
    let some_json = r#"
[
    "foo",
    {
        "bar": 1,
        "baz": [
            2,
            3
        ]
    }
]"#;

    let mut table = Table::new([some_json]);
    table
        .with(Style::rounded())
        .with(Modify::new(Segment::all()).with(Alignment::center()));

    println!("A default Alignment settings\n{table}");

    table.with(Modify::new(Segment::all()).with(AlignmentStrategy::PerLine));

    println!("Per line Alignment strategy\n{table}");

    table.with(
        Modify::new(Segment::all())
            .with(AlignmentStrategy::PerCell)
            .with(TrimStrategy::Both),
    );

    println!("A default Alignment; allowing vertical and horizontal trim\n{table}");
}