summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/examples/span.rs
blob: e6a75576a66c29e8158389dd46826fd67781a2ed (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
//! This example demonstrates using the [`Span`] [`CellOption`] to
//! extend [Cells](Cell) over a specified number of columns/rows.
//!
//! * Note how [`Span`] is available for [`Cell`] modifications
//! after the [`Modify`] [`TableOption`] is applied.
//!
//! * ⚠️ `with()` is a reused pattern within [`tabled`] for both [`Table`]
//! and [`Cell`] modifications. It can be easy for beginners to mistakenly
//! try to pass [`settings`] intended for one to the other.

use tabled::{
    settings::{
        object::Cell,
        style::{BorderSpanCorrection, Style},
        Alignment, Modify, Span,
    },
    Table,
};

fn main() {
    let data = [["just 1 column"; 5]; 5];

    let h_span = |r, c, span| Modify::new(Cell::new(r, c)).with(Span::row(span));
    let v_span = |r, c, span| Modify::new(Cell::new(r, c)).with(Span::column(span));

    let table = Table::new(data)
        .with(h_span(0, 0, 5).with("span all 5 columns"))
        .with(h_span(1, 0, 4).with("span 4 columns"))
        .with(h_span(2, 0, 2).with("span 2 columns"))
        .with(v_span(2, 4, 4).with("just 1 column\nspan\n4\ncolumns"))
        .with(v_span(3, 1, 2).with("span 2 columns\nspan\n2\ncolumns"))
        .with(v_span(2, 3, 3).with("just 1 column\nspan\n3\ncolumns"))
        .with(h_span(3, 1, 2))
        .with(Style::modern())
        .with(BorderSpanCorrection)
        .with(Alignment::center_vertical())
        .to_string();

    println!("{table}");
}