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
use std::{
fmt::Display,
io::{self, Write},
};
use termcolor::{Buffer, BufferWriter, ColorSpec, WriteColor};
use unicode_width::UnicodeWidthStr;
use crate::format::{Align, CellFormat, Justify, Padding};
#[derive(Debug)]
pub struct Cell {
pub(crate) data: Vec<String>,
pub(crate) format: CellFormat,
pub(crate) height: usize,
pub(crate) width: usize,
}
impl Cell {
pub fn new<T: Display + ?Sized>(data: &T, format: CellFormat) -> Self {
let data: Vec<String> = data.to_string().lines().map(ToString::to_string).collect();
let height = data.len() + format.padding.top + format.padding.bottom;
let width = data.iter().map(|x| x.width()).max().unwrap_or_default()
+ format.padding.left
+ format.padding.right;
Self {
data,
format,
height,
width,
}
}
pub(crate) fn buffers(
&self,
writer: &BufferWriter,
height: usize,
width: usize,
) -> io::Result<Vec<Option<Buffer>>> {
assert!(
height >= self.height,
"Provided height is less than that required by cell"
);
assert!(
width >= self.width,
"Provided width is less than that required by cell"
);
let color_spec = self.color_spec();
let mut buffers = Vec::with_capacity(height);
let blank_lines = match self.format.align {
Align::Top => self.format.padding.top,
Align::Bottom => (height - self.height) + self.format.padding.top,
Align::Center => ((height - self.height) / 2) + self.format.padding.top,
};
for _ in 0..blank_lines {
buffers.push(Some(get_buffer(
writer,
"",
width,
self.format.justify,
self.format.padding,
&color_spec,
)?));
}
for line in self.data.iter() {
buffers.push(Some(get_buffer(
writer,
line,
width,
self.format.justify,
self.format.padding,
&color_spec,
)?));
}
for _ in 0..(height - (self.data.len() + blank_lines)) {
buffers.push(Some(get_buffer(
writer,
"",
width,
self.format.justify,
self.format.padding,
&color_spec,
)?));
}
Ok(buffers)
}
fn color_spec(&self) -> ColorSpec {
let mut spec = ColorSpec::new();
spec.set_fg(self.format.foreground_color);
spec.set_bg(self.format.background_color);
spec.set_bold(self.format.bold);
spec.set_underline(self.format.underline);
spec
}
}
fn get_buffer(
writer: &BufferWriter,
data: &str,
width: usize,
justify: Justify,
padding: Padding,
color_spec: &ColorSpec,
) -> io::Result<Buffer> {
let mut buffer = writer.buffer();
buffer.set_color(&color_spec)?;
match justify {
Justify::Left => {
if padding.left != 0 {
write!(
&mut buffer,
"{space:padding$}{data:<width$}",
space = " ",
padding = padding.left,
data = data,
width = width - padding.left
)?
} else {
write!(&mut buffer, "{data:<width$}", data = data, width = width)?
}
}
Justify::Right => {
if padding.right != 0 {
write!(
&mut buffer,
"{data:>width$}{space:padding$}",
data = data,
width = width - padding.right,
space = " ",
padding = padding.right
)?
} else {
write!(&mut buffer, "{data:>width$}", data = data, width = width)?
}
}
Justify::Center => write!(&mut buffer, "{:^width$}", data, width = width)?,
}
Ok(buffer)
}