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
use std::{
fmt::Display,
io::{Result, Write},
};
use termcolor::{Buffer, BufferWriter, ColorSpec, WriteColor};
use unicode_width::UnicodeWidthStr;
use crate::format::{Align, CellFormat, Justify};
#[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();
let width = data.iter().map(|x| x.width()).max().unwrap_or_default();
Self {
data,
format,
height,
width,
}
}
pub(crate) fn buffers(
&self,
writer: &BufferWriter,
height: usize,
width: usize,
) -> 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 => 0,
Align::Bottom => (height - self.height),
Align::Center => (height - self.height) / 2,
};
for _ in 0..blank_lines {
buffers.push(Some(get_buffer(
writer,
"",
width,
self.format.justify,
&color_spec,
)?));
}
for line in self.data.iter() {
buffers.push(Some(get_buffer(
writer,
line,
width,
self.format.justify,
&color_spec,
)?));
}
for _ in 0..(height - (self.height + blank_lines)) {
buffers.push(Some(get_buffer(
writer,
"",
width,
self.format.justify,
&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,
color_spec: &ColorSpec,
) -> Result<Buffer> {
let mut buffer = writer.buffer();
buffer.set_color(&color_spec)?;
match justify {
Justify::Left => write!(&mut buffer, "{:<width$}", data, width = width)?,
Justify::Right => write!(&mut buffer, "{:>width$}", data, width = width)?,
Justify::Center => write!(&mut buffer, "{:^width$}", data, width = width)?,
}
Ok(buffer)
}