custom/custom.rs
1//! This example shows how to use a custom formatter configuration.
2
3use kitty_table::{Column, ColumnAlign, ColumnSize, TableFormatter};
4
5pub fn main() {
6 // Import these two fields for brevity in the following section.
7 use ColumnAlign::*;
8 use ColumnSize::*;
9
10 let formatter = TableFormatter::new([
11 // Column 1 will use the default settings. These are used by tuples.
12 Column::default(),
13 // This is the same as column 1, but written more explicitly.
14 // It has no title, will fit the data in its column, is left-aligned,
15 // and has a padding of 1 space
16 Column::new(None, Contain, Left, 1),
17 // Similar to column 2, but has a title.
18 // This column will grow to accomodate its data, but only up to 7
19 // characters— data will be split with newlines if needed.
20 //
21 // Because this column has a title, all columns without titles in
22 // this graph will get a blank title when printed.
23 Column::new(Some(("Fruit", Left)), ContainMax(5), Left, 1),
24 // This column has a fixed width of 32, and has 5 spaces
25 // of padding around the data, which is centered. Note that the
26 // actual width of this column will be 42 (32 + 5 + 5) due
27 // to the padding.
28 Column::new(None, Fixed(12), Centered, 5),
29 // You should be able to tell how this one will look by this point.
30 Column::new(Some(("Data", Centered)), Fixed(8), Right, 1),
31 ]);
32
33 // The above formatter has 5 columns specified, so any data we put in
34 // must turn into a [String; 5] with DebugTableRow— this is true
35 // for length-5 tuples.
36 let data = [
37 (1, 'H', "Apple", true, 2.3),
38 (2, 'E', "Pineapple", false, 5.5),
39 (3, 'L', "Kiwi", false, 11.5),
40 (4, 'L', "Banana", true, 0.5),
41 (5, 'O', "Strawberry", false, 0.25)
42 ];
43
44 // Print data just as normal.
45 let _ = formatter.debug_print(data);
46}
47
48/*
49Expected result:
50
51┏━━━┳━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
52┃ ┃ ┃ Fruit ┃ ┃ Data ┃
53┣━━━╇━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┫
54┃ 1 │ 'H' │ "Appl │ true │ 2.3 ┃
55┃ │ │ e" │ │ ┃
56┠───┼─────┼───────┼──────────────────────┼──────────┨
57┃ 2 │ 'E' │ "Pine │ false │ 5.5 ┃
58┃ │ │ apple │ │ ┃
59┃ │ │ " │ │ ┃
60┠───┼─────┼───────┼──────────────────────┼──────────┨
61┃ 3 │ 'L' │ "Kiwi │ false │ 11.5 ┃
62┃ │ │ " │ │ ┃
63┠───┼─────┼───────┼──────────────────────┼──────────┨
64┃ 4 │ 'L' │ "Bana │ true │ 0.5 ┃
65┃ │ │ na" │ │ ┃
66┠───┼─────┼───────┼──────────────────────┼──────────┨
67┃ 5 │ 'O' │ "Stra │ false │ 0.25 ┃
68┃ │ │ wberr │ │ ┃
69┃ │ │ y" │ │ ┃
70┗━━━┷━━━━━┷━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┛
71*/