formatting/
formatting.rs

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