File: format.rs

package info (click to toggle)
rustc-web 1.85.0%2Bdfsg3-1~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 1,759,988 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,056; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,566 bytes parent folder | download | duplicates (18)
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
//! This example demonstrates using the [`Format`] [`CellOption`] factory to alter
//! the cells of a [`Table`].
//!
//! * Note how [`Format::content()`] gives access to the respective cell content for replacement.
//! And [`Format::positioned()`] additionally provides the index coordinates of that cell.
//!
//! * Note how the [std] [`format!`] macro is used to update the values of the affected cells.

use tabled::{
    settings::{
        object::{Columns, Object, Rows},
        Format, Style,
    },
    Table, Tabled,
};

#[derive(Tabled)]
struct Commit {
    id: &'static str,
    header: &'static str,
    message: &'static str,
}

fn main() {
    let data = [
        Commit {
            header: "bypass open-source transmitter",
            message: "index neural panel",
            id: "8ae4e8957caeaa467acbce963701e227af00a1c7",
        },
        Commit {
            header: "program online alarm",
            message: "copy bluetooth card",
            id: "48c76de71bd685486d97dc8f4f05aa6fcc0c3f86",
        },
        Commit {
            header: "CSV",
            message: "reboot mobile capacitor",
            id: "6ffc2a2796229fc7bf59471ad907f58b897005d0",
        },
    ];

    let table = Table::new(data)
        .with(Style::psql())
        .modify(
            Rows::first(),
            Format::positioned(|_, (_, column)| column.to_string()),
        )
        .modify(
            Columns::first().not(Rows::first()),
            Format::content(|s| format!("{s}...")),
        )
        .to_string();

    println!("{table}");
}