term_data_table/style.rs
1use unicode_width::UnicodeWidthChar;
2
3/// A set of characters which make up a table style
4///
5///# Example
6///
7///```
8/// term_data_table::TableStyle {
9/// top_left_corner: '╔',
10/// top_right_corner: '╗',
11/// bottom_left_corner: '╚',
12/// bottom_right_corner: '╝',
13/// outer_left_vertical: '╠',
14/// outer_right_vertical: '╣',
15/// outer_bottom_horizontal: '╩',
16/// outer_top_horizontal: '╦',
17/// intersection: '╬',
18/// vertical: '║',
19/// horizontal: '═',
20/// };
21///```
22#[derive(Debug, Clone, Copy)]
23pub struct TableStyle {
24 pub top_left_corner: char,
25 pub top_right_corner: char,
26 pub bottom_left_corner: char,
27 pub bottom_right_corner: char,
28 pub outer_left_vertical: char,
29 pub outer_right_vertical: char,
30 pub outer_bottom_horizontal: char,
31 pub outer_top_horizontal: char,
32 pub intersection: char,
33 pub vertical: char,
34 pub horizontal: char,
35}
36
37impl TableStyle {
38 /// Basic terminal table style
39 ///
40 ///# Example
41 ///
42 ///<pre>
43 /// +---------------------------------------------------------------------------------+
44 /// | This is some centered text |
45 /// +----------------------------------------+----------------------------------------+
46 /// | This is left aligned text | This is right aligned text |
47 /// +----------------------------------------+----------------------------------------+
48 /// | This is left aligned text | This is right aligned text |
49 /// +----------------------------------------+----------------------------------------+
50 /// | This is some really really really really really really really really really tha |
51 /// | t is going to wrap to the next line |
52 /// +---------------------------------------------------------------------------------+
53 ///</pre>
54 pub const SIMPLE: TableStyle = TableStyle {
55 top_left_corner: '+',
56 top_right_corner: '+',
57 bottom_left_corner: '+',
58 bottom_right_corner: '+',
59 outer_left_vertical: '+',
60 outer_right_vertical: '+',
61 outer_bottom_horizontal: '+',
62 outer_top_horizontal: '+',
63 intersection: '+',
64 vertical: '|',
65 horizontal: '-',
66 };
67
68 /// Table style using extended character set
69 ///
70 ///# Example
71 ///
72 ///<pre>
73 /// ╔═════════════════════════════════════════════════════════════════════════════════╗
74 /// ║ This is some centered text ║
75 /// ╠════════════════════════════════════════╦════════════════════════════════════════╣
76 /// ║ This is left aligned text ║ This is right aligned text ║
77 /// ╠════════════════════════════════════════╬════════════════════════════════════════╣
78 /// ║ This is left aligned text ║ This is right aligned text ║
79 /// ╠════════════════════════════════════════╩════════════════════════════════════════╣
80 /// ║ This is some really really really really really really really really really tha ║
81 /// ║ t is going to wrap to the next line ║
82 /// ╚═════════════════════════════════════════════════════════════════════════════════╝
83 ///</pre>
84 pub const EXTENDED: TableStyle = TableStyle {
85 top_left_corner: '╔',
86 top_right_corner: '╗',
87 bottom_left_corner: '╚',
88 bottom_right_corner: '╝',
89 outer_left_vertical: '╠',
90 outer_right_vertical: '╣',
91 outer_bottom_horizontal: '╩',
92 outer_top_horizontal: '╦',
93 intersection: '╬',
94 vertical: '║',
95 horizontal: '═',
96 };
97
98 /// <pre>
99 /// ┌─────────────────────────────────────────────────────────────────────────────────┐
100 /// │ This is some centered text │
101 /// ├────────────────────────────────────────┬────────────────────────────────────────┤
102 /// │ This is left aligned text │ This is right aligned text │
103 /// ├────────────────────────────────────────┼────────────────────────────────────────┤
104 /// │ This is left aligned text │ This is right aligned text │
105 /// ├────────────────────────────────────────┴────────────────────────────────────────┤
106 /// │ This is some really really really really really really really really really tha │
107 /// │ t is going to wrap to the next line │
108 /// └─────────────────────────────────────────────────────────────────────────────────┘
109 /// </pre>
110 pub const THIN: TableStyle = TableStyle {
111 top_left_corner: '┌',
112 top_right_corner: '┐',
113 bottom_left_corner: '└',
114 bottom_right_corner: '┘',
115 outer_left_vertical: '├',
116 outer_right_vertical: '┤',
117 outer_bottom_horizontal: '┴',
118 outer_top_horizontal: '┬',
119 intersection: '┼',
120 vertical: '│',
121 horizontal: '─',
122 };
123
124 /// <pre>
125 /// ╭─────────────────────────────────────────────────────────────────────────────────╮
126 /// │ This is some centered text │
127 /// ├────────────────────────────────────────┬────────────────────────────────────────┤
128 /// │ This is left aligned text │ This is right aligned text │
129 /// ├────────────────────────────────────────┼────────────────────────────────────────┤
130 /// │ This is left aligned text │ This is right aligned text │
131 /// ├────────────────────────────────────────┴────────────────────────────────────────┤
132 /// │ This is some really really really really really really really really really tha │
133 /// │ t is going to wrap to the next line │
134 /// ╰─────────────────────────────────────────────────────────────────────────────────╯
135 /// </pre>
136 pub const ROUNDED: TableStyle = TableStyle {
137 top_left_corner: '╭',
138 top_right_corner: '╮',
139 bottom_left_corner: '╰',
140 bottom_right_corner: '╯',
141 outer_left_vertical: '├',
142 outer_right_vertical: '┤',
143 outer_bottom_horizontal: '┴',
144 outer_top_horizontal: '┬',
145 intersection: '┼',
146 vertical: '│',
147 horizontal: '─',
148 };
149
150 /// <pre>
151 /// ╔─────────────────────────────────────────────────────────────────────────────────╗
152 /// │ This is some centered text │
153 /// ╠────────────────────────────────────────╦────────────────────────────────────────╣
154 /// │ This is left aligned text │ This is right aligned text │
155 /// ╠────────────────────────────────────────┼────────────────────────────────────────╣
156 /// │ This is left aligned text │ This is right aligned text │
157 /// ╠────────────────────────────────────────╩────────────────────────────────────────╣
158 /// │ This is some really really really really really really really really really tha │
159 /// │ t is going to wrap to the next line │
160 /// ╚─────────────────────────────────────────────────────────────────────────────────╝
161 /// </pre>
162
163 pub const ELEGANT: TableStyle = TableStyle {
164 top_left_corner: '╔',
165 top_right_corner: '╗',
166 bottom_left_corner: '╚',
167 bottom_right_corner: '╝',
168 outer_left_vertical: '╠',
169 outer_right_vertical: '╣',
170 outer_bottom_horizontal: '╩',
171 outer_top_horizontal: '╦',
172 intersection: '┼',
173 vertical: '│',
174 horizontal: '─',
175 };
176
177 /// Table style comprised of null characters
178 ///
179 ///# Example
180 ///
181 ///<pre>
182 /// This is some centered text
183 ///
184 /// This is left aligned text This is right aligned text
185 ///
186 /// This is left aligned text This is right aligned text
187 ///
188 /// This is some really really really really really really really really really tha
189 /// t is going to wrap to the next line
190 ///</pre>
191 pub const BLANK: TableStyle = TableStyle {
192 top_left_corner: '\0',
193 top_right_corner: '\0',
194 bottom_left_corner: '\0',
195 bottom_right_corner: '\0',
196 outer_left_vertical: '\0',
197 outer_right_vertical: '\0',
198 outer_bottom_horizontal: '\0',
199 outer_top_horizontal: '\0',
200 intersection: '\0',
201 vertical: '\0',
202 horizontal: '\0',
203 };
204
205 /// Table style comprised of empty characters for compatibility with terminals
206 /// that don't handle null characters appropriately
207 ///
208 ///# Example
209 ///
210 ///<pre>
211 /// This is some centered text
212 ///
213 /// This is left aligned text This is right aligned text
214 ///
215 /// This is left aligned text This is right aligned text
216 ///
217 /// This is some really really really really really really really really really tha
218 /// t is going to wrap to the next line
219 ///</pre>
220 pub const EMPTY: TableStyle = TableStyle {
221 top_left_corner: ' ',
222 top_right_corner: ' ',
223 bottom_left_corner: ' ',
224 bottom_right_corner: ' ',
225 outer_left_vertical: ' ',
226 outer_right_vertical: ' ',
227 outer_bottom_horizontal: ' ',
228 outer_top_horizontal: ' ',
229 intersection: ' ',
230 vertical: ' ',
231 horizontal: ' ',
232 };
233
234 pub(crate) fn border_width(&self) -> usize {
235 self.vertical.width().unwrap_or(0)
236 }
237}