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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! The box around the text: padding, margins, the border and its
//! sides, the size, and how much room the frame takes.
use super::{Border, Color, SIDES_SET, Style};
impl Style {
/// Padding: `vertical` blank lines above and below, `horizontal`
/// columns left and right, in the background colour. Lip Gloss's
/// `Padding(vertical, horizontal)`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().padding(0, 2).render("hi"), " hi ");
/// assert_eq!(Style::new().padding(1, 1).render("hi"), " \n hi \n ");
/// ```
#[must_use]
pub const fn padding(mut self, vertical: u16, horizontal: u16) -> Self {
self.frame.padding = [vertical, horizontal, vertical, horizontal];
self
}
/// Padding on each side: top, right, bottom, left, as CSS orders them.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(
/// Style::new().padding_sides(0, 1, 0, 3).render("hi"),
/// " hi "
/// );
/// ```
#[must_use]
pub const fn padding_sides(mut self, top: u16, right: u16, bottom: u16, left: u16) -> Self {
self.frame.padding = [top, right, bottom, left];
self
}
/// Padding on the left only changed.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(
/// Style::new().padding(0, 2).padding_left(0).render("hi"),
/// "hi "
/// );
/// ```
#[must_use]
pub const fn padding_left(mut self, columns: u16) -> Self {
self.frame.padding[3] = columns;
self
}
/// Margins: blank space outside the border, `vertical` lines above
/// and below and `horizontal` columns left and right.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().margin(0, 1).render("hi"), " hi ");
/// assert_eq!(Style::new().margin(1, 0).render("hi"), " \nhi\n ");
/// ```
#[must_use]
pub const fn margin(mut self, vertical: u16, horizontal: u16) -> Self {
self.frame.margin = [vertical, horizontal, vertical, horizontal];
self
}
/// Margins on each side: top, right, bottom, left.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().margin_sides(0, 0, 0, 2).render("hi"), " hi");
/// ```
#[must_use]
pub const fn margin_sides(mut self, top: u16, right: u16, bottom: u16, left: u16) -> Self {
self.frame.margin = [top, right, bottom, left];
self
}
/// The top margin only changed.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(
/// Style::new().margin(0, 1).margin_top(1).render("hi"),
/// " \n hi "
/// );
/// ```
#[must_use]
pub const fn margin_top(mut self, rows: u16) -> Self {
self.frame.margin[0] = rows;
self
}
/// The top margin's rows.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().margin(2, 1).get_margin_top(), 2);
/// assert_eq!(Style::new().get_margin_top(), 0);
/// ```
#[must_use]
pub fn get_margin_top(&self) -> usize {
usize::from(self.frame.margin[0])
}
/// A border on every side, unless [`Style::border_sides`] picks some.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Style};
///
/// assert_eq!(
/// Style::new().border(Border::NORMAL).render("hi"),
/// "┌──┐\n│hi│\n└──┘"
/// );
/// ```
#[must_use]
pub const fn border(mut self, border: Border) -> Self {
self.frame.border = Some(border);
self
}
/// Which sides have the border: top, right, bottom, left, as Lip
/// Gloss's `Border(border, top, right, bottom, left)` sets them.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Style};
///
/// let rules = Style::new()
/// .border(Border::NORMAL)
/// .border_sides([true, false, true, false]);
/// assert_eq!(rules.render("hi"), "──\nhi\n──");
/// ```
#[must_use]
pub fn border_sides(mut self, [top, right, bottom, left]: [bool; 4]) -> Self {
let bit = |on: bool, n: u8| u8::from(on) << n;
self.frame.sides = SIDES_SET | bit(top, 0) | bit(right, 1) | bit(bottom, 2) | bit(left, 3);
self
}
/// The border's colour, as Lip Gloss's `BorderForeground`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Color, Style};
///
/// let boxed = Style::new()
/// .border(Border::NORMAL)
/// .border_foreground(Color::RED)
/// .render("hi");
/// assert!(boxed.starts_with("\x1b[31m┌"));
/// assert_eq!(tea::style::strip_ansi(&boxed), "┌──┐\n│hi│\n└──┘");
/// ```
#[must_use]
pub const fn border_foreground(mut self, color: Color) -> Self {
self.frame.border_fg = Some(color);
self
}
/// At least `columns` wide, border included; longer lines are
/// wrapped at the width less the padding, as Lip Gloss's `Width`.
/// 0 is no width.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().width(6).render("hi"), "hi ");
/// assert_eq!(Style::new().width(4).render("one two"), "one \ntwo ");
/// ```
#[must_use]
pub const fn width(mut self, columns: u16) -> Self {
self.frame.width = columns;
self
}
/// At least `rows` high, border included; blank lines are added
/// below. 0 is no height.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().height(3).render("hi"), "hi\n \n ");
/// ```
#[must_use]
pub const fn height(mut self, rows: u16) -> Self {
self.frame.height = rows;
self
}
/// The padding's columns on the left.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(Style::new().padding_sides(0, 1, 0, 3).get_padding_left(), 3);
/// ```
#[must_use]
pub fn get_padding_left(&self) -> usize {
usize::from(self.frame.padding[3])
}
/// The padding's columns on the right.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::Style;
///
/// assert_eq!(
/// Style::new().padding_sides(0, 1, 0, 3).get_padding_right(),
/// 1
/// );
/// ```
#[must_use]
pub fn get_padding_right(&self) -> usize {
usize::from(self.frame.padding[1])
}
/// The columns padding, border and margins take, left and right
/// together: Lip Gloss's `GetHorizontalFrameSize`.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Style};
///
/// let style = Style::new()
/// .padding(0, 1)
/// .border(Border::NORMAL)
/// .margin(0, 2);
/// assert_eq!(style.horizontal_frame(), 8);
/// assert_eq!(tea::style::block_width(&style.render("hi")), 2 + 8);
/// ```
#[must_use]
pub fn horizontal_frame(&self) -> usize {
let [_, right, _, left] = self.frame.padding;
let [_, m_right, _, m_left] = self.frame.margin;
let [_, b_right, _, b_left] = self.border_widths();
usize::from(right + left + m_right + m_left) + b_right + b_left
}
/// The columns left of the content and the rows above it that
/// margins, border and padding take: where a block's text starts.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Style};
///
/// let style = Style::new()
/// .margin(1, 2)
/// .border(Border::NORMAL)
/// .padding(0, 1);
/// assert_eq!(style.frame_offset(), (4, 2));
/// let lines: Vec<_> = style.render("hi").lines().map(String::from).collect();
/// assert_eq!(&lines[2][4 + 2..], "hi │ ");
/// ```
#[must_use]
pub fn frame_offset(&self) -> (usize, usize) {
let [top, _, _, left] = self.frame.padding;
let [m_top, _, _, m_left] = self.frame.margin;
let [b_top, _, _, b_left] = self.border_widths();
(
usize::from(left + m_left) + b_left,
usize::from(top + m_top) + b_top,
)
}
/// The rows padding, border and margins take, above and below.
///
/// ```
/// use petitcarnets as tea;
/// use tea::style::{Border, Style};
///
/// let style = Style::new().padding(1, 0).border(Border::NORMAL);
/// assert_eq!(style.vertical_frame(), 4);
/// assert_eq!(style.render("hi").lines().count(), 1 + 4);
/// ```
#[must_use]
pub fn vertical_frame(&self) -> usize {
let [top, _, bottom, _] = self.frame.padding;
let [m_top, _, m_bottom, _] = self.frame.margin;
let [b_top, _, b_bottom, _] = self.border_widths();
usize::from(top + bottom + m_top + m_bottom) + b_top + b_bottom
}
}