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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
//! This example demonstrates using the [`Extract`] [`TableOption`] to
//! produce a subsection of a [`Table`].
//!
//! * [`Extract`] can return a new [`Table`] with three functions:
//! * `rows()` | yields subset of the initial rows
//! * `columns()` | yields subset of the initial columns
//! * `segment()` | yields subsection of the initial table
//!
//! * Note how [`Extract`] methods accepts [`RangeBounds`] arguments,
//! making subset specifications concise.
use std::fmt::{Display, Formatter};
use tabled::{
settings::{
object::{Columns, Rows},
Alignment, Extract, Format, Modify, Style,
},
Table, Tabled,
};
#[derive(Tabled)]
struct Album {
artist: &'static str,
name: &'static str,
released: &'static str,
level_of_greatness: LevelOfGreatness,
}
impl Album {
fn new(
artist: &'static str,
name: &'static str,
released: &'static str,
level_of_greatness: LevelOfGreatness,
) -> Self {
Self {
artist,
name,
released,
level_of_greatness,
}
}
}
#[derive(Debug)]
enum LevelOfGreatness {
Supreme,
Outstanding,
Unparalleled,
}
impl Display for LevelOfGreatness {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
std::fmt::Debug::fmt(&self, f)
}
}
fn main() {
use LevelOfGreatness::*;
let data = [
Album::new(
"Pink Floyd",
"The Dark Side of the Moon",
"01 March 1973",
Unparalleled,
),
Album::new("Fleetwood Mac", "Rumours", "04 February 1977", Outstanding),
Album::new(
"Led Zeppelin",
"Led Zeppelin IV",
"08 November 1971",
Supreme,
),
];
println!("Full");
let mut table = Table::new(data);
table
.with(Style::modern())
.with(Modify::new(Rows::first()).with(Alignment::center()))
.with(Modify::new(Rows::new(1..)).with(Alignment::left()));
println!("{table}");
println!("Segment row: (1..=2) column: (1..)");
let table = table.with(Extract::segment(1..=2, 1..));
println!("{table}");
println!("Refinished segment");
let highlight = Format::content(|s| {
if s == "Outstanding" {
format!("+{s}+")
} else {
s.to_string()
}
});
let table = table
.with(Style::modern())
.with(Modify::new(Columns::new(1..)).with(highlight));
println!("{table}");
}
|