support data with line breaks
This commit is contained in:
parent
c83552af61
commit
f04bf6bb1f
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue