libtableformat/table/
border.rs

1use colored::Color;
2
3#[derive(Debug)]
4pub struct Border {
5    pub top_left: char,
6    pub top: char,
7    pub top_right: char,
8    pub top_split: char,
9    pub bottom_left: char,
10    pub bottom: char,
11    pub bottom_right: char,
12    pub bottom_split: char,
13    pub left: char,
14    pub left_split: char,
15    pub right: char,
16    pub right_split: char,
17    pub vertical_split: char,
18    pub vertical_split_intersect_left: char,
19    pub vertical_split_intersect_right: char,
20    pub vertical_split_intersect_both: char,
21    pub horizontal_split: char,
22    pub horizontal_split_intersect_top: char,
23    pub horizontal_split_intersect_bottom: char,
24    pub horizontal_split_intersect_both: char,
25    pub color: Color
26}
27
28impl Border {
29    #[must_use]
30    pub fn default() -> Border {
31        Border {
32            top_left: '+',
33            top: '-',
34            top_right: '+',
35            top_split: '+',
36            bottom_left: '+',
37            bottom: '-',
38            bottom_right: '+',
39            bottom_split: '+',
40            left: '|',
41            left_split: '+',
42            right: '|',
43            right_split: '+',
44            vertical_split: '|',
45            vertical_split_intersect_left: '+',
46            vertical_split_intersect_right: '+',
47            vertical_split_intersect_both: '+',
48            horizontal_split: '-',
49            horizontal_split_intersect_top: '+',
50            horizontal_split_intersect_bottom: '+',
51            horizontal_split_intersect_both: '+',
52            color: Color::Cyan
53        }
54    }
55
56    /// Formats the top border
57    #[must_use]
58    pub fn format_top(
59        self: &Border,
60        widths: &[usize]
61    ) -> String {
62        let mut result: String = String::from(self.top_left);
63        for ix in 0..widths.len() {
64            result.push_str(
65              &(0..widths[ix])
66                  .map(|_| self.top)
67                  .collect::<String>()
68            );
69            if ix < widths.len() - 1 {
70               result.push_str(String::from(self.top_split).as_str());
71            }
72        }
73        result.push_str(String::from(self.top_right).as_str());
74        result
75    }
76
77    /// Formats the bottom border
78    #[must_use]
79    pub fn format_bottom(
80        self: &Border,
81        widths: &[usize]
82    ) -> String {
83        let mut result: String = String::from(self.bottom_left);
84        for ix in 0..widths.len() {
85            result.push_str(
86                &(0..widths[ix])
87                    .map(|_| self.bottom)
88                    .collect::<String>()
89            );
90            if ix < widths.len() - 1 {
91                result.push_str(String::from(self.bottom_split).as_str());
92            }
93        }
94        result.push_str(String::from(self.bottom_right).as_str());
95        result
96    }
97
98    /// Formats the left border.
99    #[must_use]
100    pub fn format_left(
101        self: &Border
102    ) -> String {
103        format!("{}", self.left)
104    }
105
106    /// Formats the right border.
107    #[must_use]
108    pub fn format_right(
109        self: &Border
110    ) -> String {
111        format!("{}", self.right)
112    }
113
114    /// Formats a horizontal split.
115    #[must_use]
116    pub fn format_horizontal_split(
117        self: &Border,
118        widths: &[usize]
119    ) -> String {
120        let mut result: String = String::from(self.left_split);
121        for ix in 0..widths.len() {
122            result.push_str(
123                &(0..widths[ix])
124                    .map(|_| self.horizontal_split)
125                    .collect::<String>()
126            );
127            if ix < widths.len() - 1 {
128                result.push_str(
129                    String::from(self.horizontal_split_intersect_both).as_str()
130                );
131            }
132        }
133        result.push_str(String::from(self.right_split).as_str());
134        result
135    }
136
137    /// Formats a vertical split.
138    #[must_use]
139    pub fn format_vertical_split(
140        self: &Border
141    ) -> String {
142        format!("{}", self.vertical_split)
143    }
144}