-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathtable.rs
36 lines (34 loc) · 918 Bytes
/
table.rs
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
use pad::PadStr;
use std::cmp::max;
pub type Table = Vec<Row>;
pub type Row = Vec<Column>;
pub type Column = String;
pub fn to_markdown(data: &[Row]) -> String {
let lengths = data.iter().fold(vec![1; data[0].len()], |lens, row| {
row.iter()
.zip(lens)
.map(|(s, len)| max(s.len(), len))
.collect()
});
let rows = data
.iter()
.map(|row| {
row.iter()
.zip(&lengths)
.map(|(s, len)| s.pad_to_width(*len))
.collect::<Vec<_>>()
.join(" | ")
})
.collect::<Vec<_>>();
let separator = lengths
.iter()
.map(|len| "-".repeat(*len))
.collect::<Vec<_>>()
.join("-|-");
[
format!("| {} |", rows[0]),
format!("|-{}-|", separator),
format!("| {} |", rows[1..].join(" |\n| ")),
]
.join("\n")
}