pub struct TableFormatter<'src, T: DefaultTableStyle<N>, const N: usize> { /* private fields */ }Expand description
The main structure used for table printing. This stores information about columns and has methods for writing table results to a Write object.
Implementations§
Source§impl<T: DefaultTableStyle<N>, const N: usize> TableFormatter<'static, T, N>
impl<T: DefaultTableStyle<N>, const N: usize> TableFormatter<'static, T, N>
Sourcepub fn from_style() -> Self
pub fn from_style() -> Self
Creates a new TableFormatter from a type that implements DefaultTableStyle. This will automatically generate the columns of this formatter suited to the type.
Examples found in repository?
examples/writer.rs (line 16)
15pub fn main() {
16 let table = TableFormatter::from_style();
17
18 let data = [
19 [1, 0, 0, 0],
20 [0, 1, 0, 0],
21 [0, 0, 1, 0],
22 [0, 0, 0, 1]
23 ];
24
25 let mut out = stdout().lock();
26
27 if let Err(err) = table.write(&mut out, data) {
28 panic!("Failed to print table due to {}.", err.kind());
29 }
30
31 let _ = out.flush();
32}More examples
examples/array.rs (line 8)
5pub fn main() {
6 // Create a table using the default formatter for [&str; 3], the
7 // type we are supplying into it.
8 let table = TableFormatter::from_style();
9
10 // Put the data we want to print into a collection.
11 let data = [
12 ["These", "arrays", "need"],
13 ["to", "be", "the"],
14 ["same", "size", ":)"]
15 ];
16
17 // Call debug_print to write the output to console— use
18 // "let _ = ..." to ignore the output.
19 let _ = table.debug_print(data);
20}examples/tuple.rs (line 9)
6pub fn main() {
7 // Create a table using the default formatter for (&str, i32, bool), the
8 // type we are supplying into it.
9 let table = TableFormatter::from_style();
10
11 // Put the data we want to print into a collection.
12 let data = [
13 ("Hello, world!", 2, true),
14 ("hello", 0, true),
15 ("meow", 123, false),
16 ];
17
18 // Call debug_print to write the output to console— use
19 // "let _ = ..." to ignore the output.
20 let _ = table.debug_print(data);
21}examples/derive.rs (line 24)
21fn dictionary_struct() {
22 // Create a table using the default formatter for MyDictionaryStruct.
23 // This default formatter was created by deriving DefaultTableStyle above.
24 let table = TableFormatter::from_style();
25
26 // Don't format this into a million lines please it's fine like this
27 #[rustfmt::skip]
28 let data = [
29 MyDictionaryStruct { name: "Susan", age: 42, adult: true },
30 MyDictionaryStruct { name: "Ashli", age: 19, adult: true },
31 MyDictionaryStruct { name: "Groot", age: 5, adult: false },
32 MyDictionaryStruct { name: "Henry", age: 53, adult: true },
33 MyDictionaryStruct { name: "Whatever", age: 17, adult: false },
34 ];
35
36 // Print our results. We are able to use MyDictionaryStruct here
37 // because we derived DebugTableRow earlier.
38 let _ = table.debug_print(data);
39}
40
41fn tuple_struct() {
42 // Create a table using the default formatter for MyTupleStruct. This
43 // default formatter was created by deriving DefaultTableStyle above.
44 let table = TableFormatter::from_style();
45
46 let data = [
47 MyTupleStruct(7, -10, -3),
48 MyTupleStruct(6, -7, -1),
49 MyTupleStruct(5, -4, 1),
50 MyTupleStruct(4, -1, 3),
51 MyTupleStruct(3, 2, 5),
52 MyTupleStruct(2, 5, 7),
53 MyTupleStruct(1, 8, 9),
54 MyTupleStruct(0, 11, 11),
55 MyTupleStruct(-1, 14, 13),
56 MyTupleStruct(-2, 17, 15),
57 ];
58
59 // Print our results. We are able to use MyTupleStruct here
60 // because we derived DebugTableRow earlier.
61 let _ = table.debug_print(data);
62}Source§impl<'src, T: DefaultTableStyle<N>, const N: usize> TableFormatter<'src, T, N>
impl<'src, T: DefaultTableStyle<N>, const N: usize> TableFormatter<'src, T, N>
Sourcepub const fn new(columns: [Column<'src>; N]) -> Self
pub const fn new(columns: [Column<'src>; N]) -> Self
Create a new TableFormatter from an array of columns.
Examples found in repository?
examples/custom.rs (lines 10-31)
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}Source§impl<'src, T: DefaultTableStyle<N> + DebugTableRow<N>, const N: usize> TableFormatter<'src, T, N>
impl<'src, T: DefaultTableStyle<N> + DebugTableRow<N>, const N: usize> TableFormatter<'src, T, N>
Sourcepub fn debug_write(
&self,
out: &mut impl Write,
rows: impl IntoIterator<Item = T>,
) -> Result<()>
pub fn debug_write( &self, out: &mut impl Write, rows: impl IntoIterator<Item = T>, ) -> Result<()>
Sourcepub fn debug_print(&self, rows: impl IntoIterator<Item = T>) -> Result<()>
pub fn debug_print(&self, rows: impl IntoIterator<Item = T>) -> Result<()>
Same as debug_write but always writes to stdout.
Examples found in repository?
examples/array.rs (line 19)
5pub fn main() {
6 // Create a table using the default formatter for [&str; 3], the
7 // type we are supplying into it.
8 let table = TableFormatter::from_style();
9
10 // Put the data we want to print into a collection.
11 let data = [
12 ["These", "arrays", "need"],
13 ["to", "be", "the"],
14 ["same", "size", ":)"]
15 ];
16
17 // Call debug_print to write the output to console— use
18 // "let _ = ..." to ignore the output.
19 let _ = table.debug_print(data);
20}More examples
examples/tuple.rs (line 20)
6pub fn main() {
7 // Create a table using the default formatter for (&str, i32, bool), the
8 // type we are supplying into it.
9 let table = TableFormatter::from_style();
10
11 // Put the data we want to print into a collection.
12 let data = [
13 ("Hello, world!", 2, true),
14 ("hello", 0, true),
15 ("meow", 123, false),
16 ];
17
18 // Call debug_print to write the output to console— use
19 // "let _ = ..." to ignore the output.
20 let _ = table.debug_print(data);
21}examples/derive.rs (line 38)
21fn dictionary_struct() {
22 // Create a table using the default formatter for MyDictionaryStruct.
23 // This default formatter was created by deriving DefaultTableStyle above.
24 let table = TableFormatter::from_style();
25
26 // Don't format this into a million lines please it's fine like this
27 #[rustfmt::skip]
28 let data = [
29 MyDictionaryStruct { name: "Susan", age: 42, adult: true },
30 MyDictionaryStruct { name: "Ashli", age: 19, adult: true },
31 MyDictionaryStruct { name: "Groot", age: 5, adult: false },
32 MyDictionaryStruct { name: "Henry", age: 53, adult: true },
33 MyDictionaryStruct { name: "Whatever", age: 17, adult: false },
34 ];
35
36 // Print our results. We are able to use MyDictionaryStruct here
37 // because we derived DebugTableRow earlier.
38 let _ = table.debug_print(data);
39}
40
41fn tuple_struct() {
42 // Create a table using the default formatter for MyTupleStruct. This
43 // default formatter was created by deriving DefaultTableStyle above.
44 let table = TableFormatter::from_style();
45
46 let data = [
47 MyTupleStruct(7, -10, -3),
48 MyTupleStruct(6, -7, -1),
49 MyTupleStruct(5, -4, 1),
50 MyTupleStruct(4, -1, 3),
51 MyTupleStruct(3, 2, 5),
52 MyTupleStruct(2, 5, 7),
53 MyTupleStruct(1, 8, 9),
54 MyTupleStruct(0, 11, 11),
55 MyTupleStruct(-1, 14, 13),
56 MyTupleStruct(-2, 17, 15),
57 ];
58
59 // Print our results. We are able to use MyTupleStruct here
60 // because we derived DebugTableRow earlier.
61 let _ = table.debug_print(data);
62}examples/custom.rs (line 45)
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}Source§impl<'src, T: DefaultTableStyle<N> + DisplayTableRow<N>, const N: usize> TableFormatter<'src, T, N>
impl<'src, T: DefaultTableStyle<N> + DisplayTableRow<N>, const N: usize> TableFormatter<'src, T, N>
Sourcepub fn write(
&self,
out: &mut impl Write,
rows: impl IntoIterator<Item = T>,
) -> Result<()>
pub fn write( &self, out: &mut impl Write, rows: impl IntoIterator<Item = T>, ) -> Result<()>
Takes a Write (such as Stdout) and data that implements [DisplayTable]— prints a table of the input data to the Write object. The resulting table will appear messed up if the cursor is not at the beginning of a new line.
For debugging, use debug_write.
Examples found in repository?
examples/writer.rs (line 27)
15pub fn main() {
16 let table = TableFormatter::from_style();
17
18 let data = [
19 [1, 0, 0, 0],
20 [0, 1, 0, 0],
21 [0, 0, 1, 0],
22 [0, 0, 0, 1]
23 ];
24
25 let mut out = stdout().lock();
26
27 if let Err(err) = table.write(&mut out, data) {
28 panic!("Failed to print table due to {}.", err.kind());
29 }
30
31 let _ = out.flush();
32}