formatting/
formatting.rs

1use prettytable::{format, row, table};
2
3fn main() {
4    let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
5    table.set_titles(row!["Title 1", "Title 2"]);
6
7    // Print
8    // +-------------+------------+
9    // | Title 1     | Title 2    |
10    // +-------------+------------+
11    // | Value 1     | Value 2    |
12    // | Value three | Value four |
13    // +-------------+------------+
14    println!("FORMAT_NO_LINESEP_WITH_TITLE :");
15    table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
16    table.printstd();
17    println!("");
18
19    // Print
20    // -------------------------
21    //  Title 1      Title 2
22    // =========================
23    //  Value 1      Value 2
24    // -------------------------
25    //  Value three  Value four
26    // -------------------------
27    println!("FORMAT_NO_COLSEP :");
28    table.set_format(*format::consts::FORMAT_NO_COLSEP);
29    table.printstd();
30    println!("");
31
32    // Print
33    // +-------------------------+
34    // | Title 1      Title 2    |
35    // +=========================+
36    // | Value 1      Value 2    |
37    // | Value three  Value four |
38    // +-------------------------+
39    println!("FORMAT_BORDERS_ONLY :");
40    table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
41    table.printstd();
42    println!("");
43
44    // Custom format can be implemented using `prettytable::format::FormatBuilder`
45    // Example to print
46    // +-------------+------------+
47    // | Title 1     | Title 2    |
48    // | Value 1     | Value 2    |
49    // | Value three | Value four |
50    // +-------------+------------+
51    println!("Custom :");
52    table.set_format(
53        format::FormatBuilder::new()
54            .column_separator('|')
55            .borders('|')
56            .separators(
57                &[format::LinePosition::Top, format::LinePosition::Bottom],
58                format::LineSeparator::new('-', '+', '+', '+'),
59            )
60            .padding(1, 1)
61            .build(),
62    );
63    table.printstd();
64
65    // Customized format with unicode
66    // Example to print
67    // ┌─────────────┬────────────┐
68    // │ Title 1     │ Title 2    │
69    // ├─────────────┼────────────┤
70    // │ Value 1     │ Value 2    │
71    // ├─────────────┼────────────┤
72    // │ Value three │ Value four │
73    // └─────────────┴────────────┘
74    println!("With unicode:");
75    table.set_format(
76        format::FormatBuilder::new()
77            .column_separator('│')
78            .borders('│')
79            .separators(
80                &[format::LinePosition::Top],
81                format::LineSeparator::new('─', '┬', '┌', '┐'),
82            )
83            .separators(
84                &[format::LinePosition::Intern],
85                format::LineSeparator::new('─', '┼', '├', '┤'),
86            )
87            .separators(
88                &[format::LinePosition::Bottom],
89                format::LineSeparator::new('─', '┴', '└', '┘'),
90            )
91            .padding(1, 1)
92            .build(),
93    );
94    table.printstd();
95
96    // Customized format with unicode and different padding
97    // Example to print
98    // ┌───────────────┬──────────────┐
99    // │  Title 1      │  Title 2     │
100    // ├───────────────┼──────────────┤
101    // │  Value 1      │  Value 2     │
102    // ├───────────────┼──────────────┤
103    // │  Value three  │  Value four  │
104    // └───────────────┴──────────────┘
105    // Change individual format settings
106    println!("With unicode and padding:");
107    table.get_format().padding(2, 2);
108    table.printstd();
109}