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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::table_cell::{string_width, Alignment, TableCell};
use crate::{RowPosition, TableStyle};
use std::cmp::max;
use unicode_width::UnicodeWidthChar;
#[derive(Debug, Clone)]
pub struct Row<'data> {
pub cells: Vec<TableCell<'data>>,
pub has_separator: bool,
}
impl<'data> Row<'data> {
pub fn new<I, T>(cells: I) -> Row<'data>
where
T: Into<TableCell<'data>>,
I: IntoIterator<Item = T>,
{
let mut row = Row {
cells: vec![],
has_separator: true,
};
for entry in cells.into_iter() {
row.cells.push(entry.into());
}
row
}
pub fn format(&self, column_widths: &[usize], style: &TableStyle) -> String {
let mut buf = String::new();
let mut spanned_columns = 0;
let mut row_height = 0;
let mut wrapped_cells = Vec::new();
for cell in &self.cells {
let mut width = 0;
for j in 0..cell.col_span {
width += column_widths[j + spanned_columns];
}
let wrapped_cell = cell.wrapped_content(width + cell.col_span - 1);
row_height = max(row_height, wrapped_cell.len());
wrapped_cells.push(wrapped_cell);
spanned_columns += cell.col_span;
}
spanned_columns = 0;
let mut lines = vec![String::new(); row_height];
for col_idx in 0..column_widths.len() {
if self.cells.len() > col_idx {
let mut cell_span = 0;
let cell = &self.cells[col_idx];
for c in 0..cell.col_span {
cell_span += column_widths[spanned_columns + c];
}
for (line_idx, line) in lines.iter_mut().enumerate().take(row_height) {
if wrapped_cells[col_idx].len() > line_idx {
let mut padding = 0;
let str_width = string_width(&wrapped_cells[col_idx][line_idx]);
if cell_span >= str_width {
padding += cell_span - str_width;
if cell.col_span > 1 {
padding += style.vertical.width().unwrap_or_default()
* (cell.col_span - 1);
}
}
line.push_str(
format!(
"{}{}",
style.vertical,
self.pad_string(
padding,
cell.alignment,
&wrapped_cells[col_idx][line_idx]
)
)
.as_str(),
);
} else {
line.push_str(
format!(
"{}{}",
style.vertical,
str::repeat(
" ",
column_widths[spanned_columns] * cell.col_span + cell.col_span
- 1
)
)
.as_str(),
);
}
}
spanned_columns += cell.col_span;
} else {
for line in lines.iter_mut().take(row_height) {
line.push_str(
format!(
"{}{}",
style.vertical,
str::repeat(" ", column_widths[spanned_columns])
)
.as_str(),
);
}
spanned_columns += 1;
}
if spanned_columns == column_widths.len() {
break;
}
}
for line in &lines {
buf.push_str(line.clone().as_str());
buf.push(style.vertical);
buf.push('\n');
}
buf.pop();
buf
}
pub fn gen_separator(
&self,
column_widths: &[usize],
style: &TableStyle,
row_position: RowPosition,
previous_separator: Option<String>,
) -> String {
let mut buf = String::new();
let mut next_intersection = match self.cells.first() {
Some(cell) => cell.col_span,
None => 1,
};
buf.push(style.start_for_position(row_position));
let mut current_column = 0;
for (i, column_width) in column_widths.iter().enumerate() {
if i == next_intersection {
buf.push(style.intersect_for_position(row_position));
current_column += 1;
if self.cells.len() > current_column {
next_intersection += self.cells[current_column].col_span;
} else {
next_intersection += 1;
}
} else if i > 0 {
buf.push(style.horizontal);
}
buf.push_str(
str::repeat(style.horizontal.to_string().as_str(), *column_width).as_str(),
);
}
buf.push(style.end_for_position(row_position));
let mut out = String::new();
match previous_separator {
Some(prev) => {
for pair in buf.chars().zip(prev.chars()) {
if pair.0 == style.outer_left_vertical || pair.0 == style.outer_right_vertical {
out.push(pair.0);
} else if pair.0 != style.horizontal || pair.1 != style.horizontal {
out.push(style.merge_intersection_for_position(
pair.1,
pair.0,
row_position,
));
} else {
out.push(style.horizontal);
}
}
out
}
None => buf,
}
}
pub fn split_column_widths(&self) -> Vec<(f32, usize)> {
let mut res = Vec::new();
for cell in &self.cells {
let val = cell.split_width();
let min = (cell.min_width() as f32 / cell.col_span as f32) as usize;
let add_one = cell.min_width() as f32 % cell.col_span as f32 > 0.001;
for i in 0..cell.col_span {
if add_one && i == cell.col_span - 1 {
res.push((val + 1.0, min + 1));
} else {
res.push((val, min));
}
}
}
res
}
pub fn num_columns(&self) -> usize {
self.cells.iter().map(|x| x.col_span).sum()
}
fn pad_string(&self, padding: usize, alignment: Alignment, text: &str) -> String {
match alignment {
Alignment::Left => return format!("{}{}", text, str::repeat(" ", padding)),
Alignment::Right => return format!("{}{}", str::repeat(" ", padding), text),
Alignment::Center => {
let half_padding = padding as f32 / 2.0;
return format!(
"{}{}{}",
str::repeat(" ", half_padding.ceil() as usize),
text,
str::repeat(" ", half_padding.floor() as usize)
);
}
}
}
}