1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#[cfg(feature = "tty")]
use crossterm::style::{style, Stylize};
use unicode_width::UnicodeWidthStr;
use super::content_split::split_line;
use crate::cell::Cell;
use crate::row::Row;
use crate::style::CellAlignment;
use crate::table::Table;
use crate::utils::ColumnDisplayInfo;
pub fn delimiter(cell: &Cell, info: &ColumnDisplayInfo, table: &Table) -> char {
if let Some(delimiter) = cell.delimiter {
delimiter
} else if let Some(delimiter) = info.delimiter {
delimiter
} else if let Some(delimiter) = table.delimiter {
delimiter
} else {
' '
}
}
pub fn format_content(table: &Table, display_info: &[ColumnDisplayInfo]) -> Vec<Vec<Vec<String>>> {
let mut table_content = Vec::with_capacity(table.rows.len() + 1);
if let Some(header) = table.header() {
table_content.push(format_row(header, display_info, table));
}
for row in table.rows.iter() {
table_content.push(format_row(row, display_info, table));
}
table_content
}
pub fn format_row(
row: &Row,
display_infos: &[ColumnDisplayInfo],
table: &Table,
) -> Vec<Vec<String>> {
let mut temp_row_content = Vec::with_capacity(display_infos.len());
let mut cell_iter = row.cells.iter();
for info in display_infos.iter() {
if info.is_hidden {
cell_iter.next();
continue;
}
let mut cell_lines = Vec::new();
let cell = if let Some(cell) = cell_iter.next() {
cell
} else {
cell_lines.push(" ".repeat(info.width().into()));
temp_row_content.push(cell_lines);
continue;
};
let delimiter = delimiter(cell, info, table);
for line in cell.content.iter() {
if line.width() > info.content_width.into() {
let mut splitted = split_line(line, info, delimiter);
cell_lines.append(&mut splitted);
} else {
cell_lines.push(line.into());
}
}
if let Some(lines) = row.max_height {
if cell_lines.len() > lines {
let _ = cell_lines.split_off(lines);
let last_line = cell_lines
.get_mut(lines - 1)
.expect("We know it's this long.");
let width: usize = info.content_width.into();
if width >= 6 {
if last_line.width() >= width - 3 {
let surplus = (last_line.width() + 3) - width;
last_line.truncate(last_line.width() - surplus);
}
last_line.push_str("...");
}
}
}
let cell_lines = cell_lines
.iter()
.map(|line| align_line(table, info, cell, line.to_string()));
temp_row_content.push(cell_lines.collect());
}
let max_lines = temp_row_content.iter().map(Vec::len).max().unwrap_or(0);
let mut row_content = Vec::with_capacity(max_lines * display_infos.len());
for index in 0..max_lines {
let mut line = Vec::with_capacity(display_infos.len());
let mut cell_iter = temp_row_content.iter();
for info in display_infos.iter() {
if info.is_hidden {
continue;
}
let cell = cell_iter.next().unwrap();
match cell.get(index) {
Some(content) => line.push(content.clone()),
None => line.push(" ".repeat(info.width().into())),
}
}
row_content.push(line);
}
row_content
}
#[allow(unused_variables)]
fn align_line(table: &Table, info: &ColumnDisplayInfo, cell: &Cell, mut line: String) -> String {
let content_width = info.content_width;
let remaining: usize = usize::from(content_width).saturating_sub(line.width());
#[cfg(feature = "tty")]
if table.should_style() && table.style_text_only {
line = style_line(line, cell);
}
let alignment = if let Some(alignment) = cell.alignment {
alignment
} else if let Some(alignment) = info.cell_alignment {
alignment
} else {
CellAlignment::Left
};
match alignment {
CellAlignment::Left => {
line += &" ".repeat(remaining);
}
CellAlignment::Right => {
line = " ".repeat(remaining) + &line;
}
CellAlignment::Center => {
let left_padding = (remaining as f32 / 2f32).ceil() as usize;
let right_padding = (remaining as f32 / 2f32).floor() as usize;
line = " ".repeat(left_padding) + &line + &" ".repeat(right_padding);
}
}
line = pad_line(&line, info);
#[cfg(feature = "tty")]
if table.should_style() && !table.style_text_only {
return style_line(line, cell);
}
line
}
fn pad_line(line: &str, info: &ColumnDisplayInfo) -> String {
let mut padded_line = String::new();
padded_line += &" ".repeat(info.padding.0.into());
padded_line += line;
padded_line += &" ".repeat(info.padding.1.into());
padded_line
}
#[cfg(feature = "tty")]
fn style_line(line: String, cell: &Cell) -> String {
if cell.fg.is_none() && cell.bg.is_none() && cell.attributes.is_empty() {
return line;
}
let mut content = style(line);
if let Some(color) = cell.fg {
content = content.with(color);
}
if let Some(color) = cell.bg {
content = content.on(color);
}
for attribute in cell.attributes.iter() {
content = content.attribute(*attribute);
}
content.to_string()
}