libtableformat/
lib.rs

1mod content;
2mod content_iterator;
3mod data_item;
4mod data_source;
5mod table_data_source;
6pub mod table;
7
8pub use content::ContentStyle;
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13    use std::env;
14    use table::{Border, Table};
15    use table::row::Row;
16    use table::cell::Cell;
17    use colored::Color;
18
19    #[test]
20    fn test_simple_table() {
21        let table =
22            table!(
23                "{^:10:}" => "Food", "{^:10:}" => "Count";
24                "Fish", "15", "Pizza", "10", "Steak", "6"
25            );
26
27        let output = table.format();
28        println!("{}", output);
29
30        let expected = "+----------+----------+\n|   Food   |  Count   |\n+----------+----------+\n|Fish      |15        |\n+----------+----------+\n|Pizza     |10        |\n+----------+----------+\n|Steak     |6         |\n+----------+----------+\n";
31
32        assert_eq!(output, expected);
33    }
34
35    #[test]
36    fn test_simple_vector_table() {
37        let table = table!(
38            "{c^:15:}" => "Food", "{c^:10:}" => "Count";
39            "Fish", "3", "Pears", "5", "Pizza", "13"
40        );
41
42        let output = table.format();
43        println!("{}", output);
44
45        let expected =
46            match env::var("NO_COLOR") {
47                Ok(_) => "+---------------+----------+\n|     Food      |  Count   |\n+---------------+----------+\n|Fish           |3         |\n+---------------+----------+\n|Pears          |5         |\n+---------------+----------+\n|Pizza          |13        |\n+---------------+----------+\n",
48                Err(_) => "+---------------+----------+\n|\u{1b}[36m     Food      \u{1b}[0m|\u{1b}[36m  Count   \u{1b}[0m|\n+---------------+----------+\n|Fish           |3         |\n+---------------+----------+\n|Pears          |5         |\n+---------------+----------+\n|Pizza          |13        |\n+---------------+----------+\n",
49            };
50
51        assert_eq!(output, expected);
52    }
53
54    #[test]
55    fn test_custom_border_table() {
56        let mut table = table!(
57            "{R^:10:}" => "Custom", "{C^:20:}" => "Borders";
58            "are", "super fun", "and", "super awesome"
59        );
60
61        table.border = Border {
62            top_left: '┌',
63            top: '─',
64            top_right: '┐',
65            top_split: '┬',
66            bottom_left: '└',
67            bottom: '─',
68            bottom_right: '┘',
69            bottom_split: '┴',
70            left: '│',
71            left_split:'├',
72            right: '│',
73            right_split: '┤',
74            vertical_split: '│',
75            vertical_split_intersect_left: '┤',
76            vertical_split_intersect_right: '├',
77            vertical_split_intersect_both: '┼',
78            horizontal_split: '─',
79            horizontal_split_intersect_top: '┴',
80            horizontal_split_intersect_bottom: '┬',
81            horizontal_split_intersect_both: '┼',
82            color: Color::Red
83        };
84
85        let output = table.format();
86        println!("{}", output);
87
88        let expected =
89            match env::var("NO_COLOR") {
90                Ok(_) => "┌──────────┬────────────────────┐\n│  Custom  │      Borders       │\n├──────────┼────────────────────┤\n│are       │super fun           │\n├──────────┼────────────────────┤\n│and       │super awesome       │\n└──────────┴────────────────────┘\n",
91                Err(_) => "┌──────────┬────────────────────┐\n│\u{1b}[91m  Custom  \u{1b}[0m│\u{1b}[96m      Borders       \u{1b}[0m│\n├──────────┼────────────────────┤\n│are       │super fun           │\n├──────────┼────────────────────┤\n│and       │super awesome       │\n└──────────┴────────────────────┘\n",
92            };
93
94        assert_eq!(output, expected);
95    }
96}