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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#![deny(missing_docs, missing_debug_implementations, nonstandard_style)]
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
mod config;
pub use config::{Alignment, BorderColor, BorderComponents, BorderStyle, Config};
use console::{measure_text_width, pad_str, Style};
use std::{cmp, fmt::Display};
const SPACE: &str = " ";
const NEWLINE: &str = "\n";
#[derive(PartialEq, Clone, Debug)]
pub struct Billboard {
pub config: Config,
}
impl Billboard {
pub fn default() -> Billboard {
Config::default().build()
}
pub fn new(config: Config) -> Billboard {
Billboard { config }
}
pub fn builder() -> Config {
Config::default()
}
pub fn print(&self, content: impl Display) {
println!("{}", self.enclose(content));
}
pub fn eprint(&self, content: impl Display) {
eprintln!("{}", self.enclose(content));
}
pub fn enclose(&self, content: impl Display) -> String {
let content = content.to_string();
let border_color = match self.config.border_color {
Some(color) => Style::from_dotted_str(&format!("{:?}", color).to_lowercase()),
None => Style::default(),
};
let mut lines: Vec<String> = Vec::new();
lines.push(SPACE.repeat(self.config.padding.top));
for line in content.split_terminator(NEWLINE) {
lines.push(line.to_string());
}
lines.push(SPACE.repeat(self.config.padding.bottom));
let mut widest_length: usize = 0;
for line in lines.clone() {
widest_length = cmp::max(widest_length, measure_text_width(line.as_str()));
}
for (i, line) in lines.clone().iter().enumerate() {
lines[i] = pad_str(&line, widest_length, self.config.text_alignment, None).to_string();
}
let content_width = widest_length + self.config.padding.left + self.config.padding.right;
let output = if let Some((max_width, _)) = term_size::dimensions() {
if content_width > max_width {
return content.to_string();
}
let padding_left = SPACE.repeat(self.config.padding.left);
let margin_left = match &self.config.box_alignment {
Alignment::Left => SPACE.repeat(self.config.padding.left),
Alignment::Center => {
let space_width = cmp::max((max_width - content_width) / 2, 0);
SPACE.repeat(space_width)
}
Alignment::Right => {
let space_width =
cmp::max(max_width - content_width - self.config.margin.right - 2, 0);
SPACE.repeat(space_width)
}
};
let mut horizontal_str = self.config.border_components.horizontal.to_string();
let mut horizontal_str_width = measure_text_width(horizontal_str.as_str());
if horizontal_str_width == 0 {
horizontal_str.push_str(" ");
horizontal_str_width = 1;
}
let mut horizontal = horizontal_str.repeat(
(content_width % horizontal_str_width) + (content_width / horizontal_str_width),
);
while measure_text_width(horizontal.as_str()) > content_width {
horizontal.pop();
}
let top = format!(
"{}{}{}{}{}",
NEWLINE.repeat(self.config.margin.top),
margin_left,
border_color.apply_to(&self.config.border_components.top_left),
border_color.apply_to(&horizontal),
border_color.apply_to(&self.config.border_components.top_right)
);
let bottom = format!(
"{}{}{}{}{}",
margin_left,
border_color.apply_to(&self.config.border_components.bottom_left),
border_color.apply_to(&horizontal),
border_color.apply_to(&self.config.border_components.bottom_right),
NEWLINE.repeat(self.config.margin.bottom)
);
let mut middle = String::from("\n");
let mut vertical = self.config.border_components.vertical.to_string();
if measure_text_width(vertical.as_str()) == 0 {
vertical.push_str(" ");
}
let vertical = vertical.repeat(lines.len());
let mut vertical_chars = vertical.chars();
for line in lines {
let vertical = border_color
.apply_to(&vertical_chars.next().unwrap())
.to_string();
middle.push_str(&margin_left);
middle.push_str(&vertical);
middle.push_str(&padding_left);
middle.push_str(&line);
middle.push_str(&SPACE.repeat(
content_width - measure_text_width(line.as_str()) - &self.config.padding.left,
));
middle.push_str(&vertical);
middle.push_str(NEWLINE);
}
format!("{}{}{}", top, middle, bottom)
} else {
content.to_string()
};
output
}
}