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
use std::io::Result;
use termcolor::{Buffer, BufferWriter, ColorSpec};
use crate::{
buffers::Buffers,
cell::{Cell, CellStruct, Dimension as CellDimension},
table::{Dimension as TableDimension, TableFormat},
utils::{print_char, print_vertical_line, println_str, transpose},
};
pub struct RowStruct {
pub(crate) cells: Vec<CellStruct>,
}
impl RowStruct {
pub(crate) fn required_dimension(&self) -> Dimension {
let mut widths = Vec::with_capacity(self.cells.len());
let mut height = 0;
for cell in self.cells.iter() {
let cell_dimension = cell.required_dimension();
widths.push(cell_dimension.width);
height = std::cmp::max(cell_dimension.height, height);
}
Dimension { widths, height }
}
pub(crate) fn print_writer(
&self,
writer: &BufferWriter,
dimension: Dimension,
format: &TableFormat,
color_spec: &ColorSpec,
) -> Result<()> {
let buffers = self.buffers(&writer, dimension)?;
for line in buffers.into_iter() {
print_vertical_line(&writer, format.border.left.as_ref(), &color_spec)?;
let mut line_buffers = line.into_iter().peekable();
while let Some(buffer) = line_buffers.next() {
print_char(&writer, ' ', &color_spec)?;
writer.print(&buffer)?;
print_char(&writer, ' ', &color_spec)?;
match line_buffers.peek() {
Some(_) => {
print_vertical_line(&writer, format.separator.column.as_ref(), &color_spec)?
}
None => {
print_vertical_line(&writer, format.border.right.as_ref(), &color_spec)?
}
}
}
println_str(&writer, "", &color_spec)?;
}
Ok(())
}
}
impl Buffers for RowStruct {
type Dimension = Dimension;
type Buffers = Vec<Vec<Buffer>>;
fn buffers(
&self,
writer: &BufferWriter,
available_dimension: Self::Dimension,
) -> Result<Self::Buffers> {
let available_cell_dimensions: Vec<CellDimension> = available_dimension.into();
let cell_buffers = self
.cells
.iter()
.zip(available_cell_dimensions.into_iter())
.map(|(cell, available_dimension)| cell.buffers(writer, available_dimension))
.collect::<Result<Vec<Vec<Buffer>>>>()?;
Ok(transpose(cell_buffers))
}
}
pub trait Row {
fn row(self) -> RowStruct;
}
impl<T, C> Row for T
where
T: IntoIterator<Item = C>,
C: Cell,
{
fn row(self) -> RowStruct {
let cells = self.into_iter().map(|cell| cell.cell()).collect();
RowStruct { cells }
}
}
impl Row for RowStruct {
fn row(self) -> RowStruct {
self
}
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub(crate) struct Dimension {
pub(crate) widths: Vec<usize>,
pub(crate) height: usize,
}
impl From<TableDimension> for Vec<Dimension> {
fn from(table_dimension: TableDimension) -> Self {
let heights = table_dimension.heights;
let widths = table_dimension.widths;
heights
.into_iter()
.map(|height| Dimension {
widths: widths.clone(),
height,
})
.collect()
}
}
#[cfg(test)]
mod tests {
use crate::style::Style;
use super::*;
#[test]
fn test_into_row_with_style() {
let row = vec!["Hello".cell().bold(true), "World".cell()].row();
assert_eq!(2, row.cells.len());
}
#[test]
fn test_into_row() {
let row = &["Hello", "World"].row();
assert_eq!(2, row.cells.len());
}
}