File: panel.rs

package info (click to toggle)
rust-tabled 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,408 kB
  • sloc: makefile: 4
file content (58 lines) | stat: -rw-r--r-- 1,743 bytes parent folder | download
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
//! This example demonstrates using the [`Panel`] [`TableOption`] to inject
//! table-length columns and rows into a [`Table`].
//!
//! * [`Panel`] supports four injection options:
//!     * Horizontal | manual index selection
//!     * Vertical | manual index selection
//!     * Header | before first row
//!     * Footer | after last row

use tabled::{
    settings::{
        object::Segment,
        style::{BorderSpanCorrection, Style},
        Alignment, Modify, Panel, Width,
    },
    Table, Tabled,
};

#[derive(Tabled)]
struct Release {
    version: &'static str,
    published_date: &'static str,
    is_active: bool,
    major_feature: &'static str,
}

impl Release {
    const fn new(v: &'static str, p: &'static str, active: bool, feature: &'static str) -> Self {
        Self {
            version: v,
            published_date: p,
            is_active: active,
            major_feature: feature,
        }
    }
}

const DATA: [Release; 3] = [
    Release::new("0.2.1", "2021-06-23", true, "#[header(inline)] attribute"),
    Release::new("0.2.0", "2021-06-19", false, "API changes"),
    Release::new("0.1.4", "2021-06-07", false, "display_with attribute"),
];

fn main() {
    let mut table = Table::new(DATA);
    table
        .with(Panel::header("Tabled Releases"))
        .with(Panel::footer(format!("N - {}", DATA.len())))
        .with(Panel::vertical(0, "Some text goes here"))
        .with(Panel::vertical(5, "Some text goes here"))
        .with(Modify::new((0, 0)).with(Width::wrap(1)))
        .with(Modify::new((0, 5)).with(Width::wrap(1)))
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .with(Style::modern())
        .with(BorderSpanCorrection);

    println!("{table}");
}