Debian Codegraph @

benches/build_tables.rs

raw symbols SCIP
1 use comfy_table::{ColumnConstraint::*, Width::*, presets::UTF8_FULL, *};
2 use criterion::{Criterion, criterion_group, criterion_main};
3
4 /// Build the readme table
5 #[cfg(feature = "tty")]
6 fn build_readme_table() {
7 let mut table = Table::new();
8 table.load_preset(UTF8_FULL)
9 .set_content_arrangement(ContentArrangement::Dynamic)
10 .set_width(80)
11 .set_header(vec![
12 Cell::new("Header1").add_attribute(Attribute::Bold),
13 Cell::new("Header2").fg(Color::Green),
14 Cell::new("Header3"),
15 ])
16 .add_row(vec![
17 Cell::new("This is a bold text").add_attribute(Attribute::Bold),
18 Cell::new("This is a green text").fg(Color::Green),
19 Cell::new("This one has black background").bg(Color::Black),
20 ])
21 .add_row(vec![
22 Cell::new("Blinky boi").add_attribute(Attribute::SlowBlink),
23 Cell::new("This table's content is dynamically arranged. The table is exactly 80 characters wide.\nHere comes a reallylongwordthatshoulddynamicallywrap"),
24 Cell::new("COMBINE ALL THE THINGS")
25 .fg(Color::Green)
26 .bg(Color::Black)
27 .add_attributes(vec![
28 Attribute::Bold,
29 Attribute::SlowBlink,
30 ])
31 ]);
32
33 // Build the table.
34 let _ = table.lines();
35 }
36
37 #[cfg(not(feature = "tty"))]
38 fn build_readme_table() {
39 let mut table = Table::new();
40 table.load_preset(UTF8_FULL)
41 .set_content_arrangement(ContentArrangement::Dynamic)
42 .set_width(80)
43 .set_header(vec![
44 Cell::new("Header1"),
45 Cell::new("Header2"),
46 Cell::new("Header3"),
47 ])
48 .add_row(vec![
49 Cell::new("This is a bold text"),
50 Cell::new("This is a green text"),
51 Cell::new("This one has black background"),
52 ])
53 .add_row(vec![
54 Cell::new("Blinky boi"),
55 Cell::new("This table's content is dynamically arranged. The table is exactly 80 characters wide.\nHere comes a reallylongwordthatshoulddynamicallywrap"),
56 Cell::new("COMBINE ALL THE THINGS"),
57 ]);
58
59 // Build the table.
60 let _ = table.lines();
61 }
62
63 /// Create a dynamic 10x10 Table with width 400 and unevenly distributed content.
64 /// On top of that, most of the columns have some kind of constraint.
65 fn build_big_table() {
66 let mut table = Table::new();
67 table
68 .load_preset(UTF8_FULL)
69 .set_content_arrangement(ContentArrangement::DynamicFullWidth)
70 .set_width(400)
71 .set_header(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
72
73 // Create a 10x10 grid
74 for row_index in 0..10 {
75 let mut row = Vec::new();
76 for column in 0..10 {
77 row.push("SomeWord ".repeat((column + row_index * 2) % 10));
78 }
79 table.add_row(row);
80 }
81
82 table.set_constraints(vec![
83 UpperBoundary(Fixed(20)),
84 LowerBoundary(Fixed(40)),
85 Absolute(Fixed(5)),
86 Absolute(Percentage(3)),
87 Absolute(Percentage(3)),
88 Boundaries {
89 lower: Fixed(30),
90 upper: Percentage(10),
91 },
92 ]);
93
94 // Build the table.
95 let _ = table.lines();
96 }
97
98 pub fn build_tables(crit: &mut Criterion) {
99 crit.bench_function("Readme table", |b| b.iter(build_readme_table));
100
101 crit.bench_function("Big table", |b| b.iter(build_big_table));
102 }
103
104 criterion_group!(benches, build_tables);
105 criterion_main!(benches);