support data with line breaks

This commit is contained in:
Abraham Toriz 2021-07-15 09:40:37 -05:00
parent c83552af61
commit f04bf6bb1f
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
1 changed files with 31 additions and 5 deletions

View File

@ -61,15 +61,41 @@ impl Tabulate {
}
pub fn feed(&mut self, data: Vec<String>) {
for (w, d) in self.widths.iter_mut().zip(data.iter()) {
let count = d.chars().count();
let mut lines: Vec<Vec<String>> = Vec::new();
if count > *w {
*w = count;
for (col, (w, d)) in self.widths.iter_mut().zip(data.iter()).enumerate() {
for (row, dl) in d.split("\n").enumerate() {
let count = dl.chars().count();
if count > *w {
*w = count;
}
if let Some(line) = lines.get_mut(row) {
if let Some(pos) = line.get_mut(col) {
*pos = dl.into();
} else {
line.push(dl.into());
}
} else {
lines.push({
let mut prev = if row == 0 {
data[..col].to_vec()
} else {
(0..col).map(|_| "".into()).collect()
};
prev.push(dl.into());
prev
});
}
}
}
self.data.push(DataOrSep::Data(data));
for line in lines {
self.data.push(DataOrSep::Data(line));
}
}
pub fn separator(&mut self, c: char) {