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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Style definitions for terminal rendering.
use crate::border::{Border, BorderColors};
use crate::position::Position;
use glyphs::Color;
use unicode_width::UnicodeWidthStr;
/// A style definition for rendering terminal content.
///
/// Style uses a builder pattern for easy chaining:
///
/// ```rust
/// use lacquer::{Style, Border, Position, Color};
///
/// let style = Style::new()
/// .padding(1, 2)
/// .margin(0, 1)
/// .border(Border::Rounded)
/// .foreground(Color::from_hex("#F97316"))
/// .background(Color::from_hex("#0A0A0A"))
/// .bold()
/// .width(40)
/// .align(Position::Center);
/// ```
#[derive(Debug, Clone, Default)]
pub struct Style {
// Box model
padding_top: u16,
padding_right: u16,
padding_bottom: u16,
padding_left: u16,
margin_top: u16,
margin_right: u16,
margin_bottom: u16,
margin_left: u16,
// Dimensions
width: Option<u16>,
height: Option<u16>,
max_width: Option<u16>,
max_height: Option<u16>,
// Border
border: Border,
border_colors: BorderColors,
// Colors
foreground: Option<Color>,
background: Option<Color>,
// Text styling
bold: bool,
italic: bool,
underline: bool,
strikethrough: bool,
dim: bool,
reverse: bool,
// Alignment
horizontal_align: Position,
vertical_align: Position,
// Inline mode (no block rendering)
inline: bool,
}
impl Style {
/// Create a new empty style.
pub const fn new() -> Self {
Self {
padding_top: 0,
padding_right: 0,
padding_bottom: 0,
padding_left: 0,
margin_top: 0,
margin_right: 0,
margin_bottom: 0,
margin_left: 0,
width: None,
height: None,
max_width: None,
max_height: None,
border: Border::None,
border_colors: BorderColors {
top: None,
right: None,
bottom: None,
left: None,
},
foreground: None,
background: None,
bold: false,
italic: false,
underline: false,
strikethrough: false,
dim: false,
reverse: false,
horizontal_align: Position::Start,
vertical_align: Position::Start,
inline: false,
}
}
// ─────────────────────────────────────────────────────────────
// Padding
// ─────────────────────────────────────────────────────────────
/// Set uniform padding on all sides.
pub const fn padding_all(mut self, padding: u16) -> Self {
self.padding_top = padding;
self.padding_right = padding;
self.padding_bottom = padding;
self.padding_left = padding;
self
}
/// Set vertical and horizontal padding.
pub const fn padding(mut self, vertical: u16, horizontal: u16) -> Self {
self.padding_top = vertical;
self.padding_bottom = vertical;
self.padding_left = horizontal;
self.padding_right = horizontal;
self
}
/// Set padding for each side individually.
pub const fn padding_sides(
mut self,
top: u16,
right: u16,
bottom: u16,
left: u16,
) -> Self {
self.padding_top = top;
self.padding_right = right;
self.padding_bottom = bottom;
self.padding_left = left;
self
}
/// Set top padding.
pub const fn padding_top(mut self, padding: u16) -> Self {
self.padding_top = padding;
self
}
/// Set right padding.
pub const fn padding_right(mut self, padding: u16) -> Self {
self.padding_right = padding;
self
}
/// Set bottom padding.
pub const fn padding_bottom(mut self, padding: u16) -> Self {
self.padding_bottom = padding;
self
}
/// Set left padding.
pub const fn padding_left(mut self, padding: u16) -> Self {
self.padding_left = padding;
self
}
// ─────────────────────────────────────────────────────────────
// Margin
// ─────────────────────────────────────────────────────────────
/// Set uniform margin on all sides.
pub const fn margin_all(mut self, margin: u16) -> Self {
self.margin_top = margin;
self.margin_right = margin;
self.margin_bottom = margin;
self.margin_left = margin;
self
}
/// Set vertical and horizontal margin.
pub const fn margin(mut self, vertical: u16, horizontal: u16) -> Self {
self.margin_top = vertical;
self.margin_bottom = vertical;
self.margin_left = horizontal;
self.margin_right = horizontal;
self
}
/// Set margin for each side individually.
pub const fn margin_sides(mut self, top: u16, right: u16, bottom: u16, left: u16) -> Self {
self.margin_top = top;
self.margin_right = right;
self.margin_bottom = bottom;
self.margin_left = left;
self
}
// ─────────────────────────────────────────────────────────────
// Dimensions
// ─────────────────────────────────────────────────────────────
/// Set fixed width.
pub const fn width(mut self, width: u16) -> Self {
self.width = Some(width);
self
}
/// Set fixed height.
pub const fn height(mut self, height: u16) -> Self {
self.height = Some(height);
self
}
/// Set maximum width.
pub const fn max_width(mut self, max: u16) -> Self {
self.max_width = Some(max);
self
}
/// Set maximum height.
pub const fn max_height(mut self, max: u16) -> Self {
self.max_height = Some(max);
self
}
// ─────────────────────────────────────────────────────────────
// Border
// ─────────────────────────────────────────────────────────────
/// Set border style.
pub const fn border(mut self, border: Border) -> Self {
self.border = border;
self
}
/// Set uniform border color.
pub fn border_foreground(mut self, color: Color) -> Self {
self.border_colors = BorderColors::all(color);
self
}
/// Set border color from a hex string.
pub fn border_foreground_hex(self, hex: &str) -> Self {
self.border_foreground(Color::from_hex(hex))
}
// ─────────────────────────────────────────────────────────────
// Colors
// ─────────────────────────────────────────────────────────────
/// Set foreground (text) color.
///
/// Accepts a `Color`, hex string, or RGB values.
///
/// ```rust
/// use lacquer::{Style, Color};
///
/// let s1 = Style::new().foreground(Color::Red);
/// let s2 = Style::new().foreground(Color::from_hex("#F97316"));
/// let s3 = Style::new().foreground(Color::rgb(249, 115, 22));
/// ```
pub fn foreground(mut self, color: Color) -> Self {
self.foreground = Some(color);
self
}
/// Set foreground color from a hex string.
pub fn foreground_hex(self, hex: &str) -> Self {
self.foreground(Color::from_hex(hex))
}
/// Set background color.
pub fn background(mut self, color: Color) -> Self {
self.background = Some(color);
self
}
/// Set background color from a hex string.
pub fn background_hex(self, hex: &str) -> Self {
self.background(Color::from_hex(hex))
}
// ─────────────────────────────────────────────────────────────
// Text Styling
// ─────────────────────────────────────────────────────────────
/// Enable bold text.
pub const fn bold(mut self) -> Self {
self.bold = true;
self
}
/// Enable italic text.
pub const fn italic(mut self) -> Self {
self.italic = true;
self
}
/// Enable underlined text.
pub const fn underline(mut self) -> Self {
self.underline = true;
self
}
/// Enable strikethrough text.
pub const fn strikethrough(mut self) -> Self {
self.strikethrough = true;
self
}
/// Enable dim/faint text.
pub const fn dim(mut self) -> Self {
self.dim = true;
self
}
/// Enable reverse video (swap fg/bg).
pub const fn reverse(mut self) -> Self {
self.reverse = true;
self
}
// ─────────────────────────────────────────────────────────────
// Alignment
// ─────────────────────────────────────────────────────────────
/// Set horizontal alignment.
pub const fn align(mut self, position: Position) -> Self {
self.horizontal_align = position;
self
}
/// Set vertical alignment.
pub const fn vertical_align(mut self, position: Position) -> Self {
self.vertical_align = position;
self
}
// ─────────────────────────────────────────────────────────────
// Mode
// ─────────────────────────────────────────────────────────────
/// Enable inline mode (no block wrapping).
pub const fn inline(mut self) -> Self {
self.inline = true;
self
}
// ─────────────────────────────────────────────────────────────
// Rendering
// ─────────────────────────────────────────────────────────────
/// Render content with this style applied.
pub fn render(&self, content: &str) -> String {
if self.inline {
return self.render_inline(content);
}
let lines: Vec<&str> = content.lines().collect();
let content_width = self.calculate_content_width(&lines);
let content_height = lines.len();
// Calculate total dimensions
let inner_width = content_width + self.padding_left as usize + self.padding_right as usize;
let inner_height =
content_height + self.padding_top as usize + self.padding_bottom as usize;
let has_border = !matches!(self.border, Border::None);
let border_width = if has_border { 2 } else { 0 };
let _total_width = inner_width + border_width;
let _total_height = inner_height + border_width;
// Build output
let mut output = String::new();
// Top margin
for _ in 0..self.margin_top {
output.push('\n');
}
let left_margin = " ".repeat(self.margin_left as usize);
let border_style = self.border.style();
// Top border
if has_border {
output.push_str(&left_margin);
output.push(border_style.top_left);
output.push_str(&border_style.top.to_string().repeat(inner_width));
output.push(border_style.top_right);
output.push('\n');
}
// Content rows
let total_content_rows = inner_height;
let vertical_offset = self
.vertical_align
.offset(content_height, total_content_rows);
for row in 0..total_content_rows {
output.push_str(&left_margin);
// Left border
if has_border {
output.push(border_style.left);
}
// Row content
let content_row = row
.checked_sub(self.padding_top as usize)
.and_then(|r| {
if r >= vertical_offset && r < vertical_offset + content_height {
Some(r - vertical_offset)
} else {
None
}
})
.and_then(|r| lines.get(r).copied());
let row_str = self.render_row(content_row, inner_width);
output.push_str(&row_str);
// Right border
if has_border {
output.push(border_style.right);
}
output.push('\n');
}
// Bottom border
if has_border {
output.push_str(&left_margin);
output.push(border_style.bottom_left);
output.push_str(&border_style.bottom.to_string().repeat(inner_width));
output.push(border_style.bottom_right);
output.push('\n');
}
// Bottom margin
for _ in 0..self.margin_bottom {
output.push('\n');
}
// Trim trailing newline if present
if output.ends_with('\n') {
output.pop();
}
output
}
fn render_inline(&self, content: &str) -> String {
let mut styled = glyphs::style(content);
if let Some(fg) = &self.foreground {
styled = styled.fg(*fg);
}
if let Some(bg) = &self.background {
styled = styled.bg(*bg);
}
if self.bold {
styled = styled.bold();
}
if self.italic {
styled = styled.italic();
}
if self.underline {
styled = styled.underline();
}
if self.strikethrough {
styled = styled.strikethrough();
}
if self.dim {
styled = styled.dim();
}
if self.reverse {
styled = styled.reverse();
}
styled.to_string()
}
fn render_row(&self, content: Option<&str>, width: usize) -> String {
let text = content.unwrap_or("");
let text_width = UnicodeWidthStr::width(text);
let available_width = width
.saturating_sub(self.padding_left as usize)
.saturating_sub(self.padding_right as usize);
let horizontal_offset = self.horizontal_align.offset(text_width, available_width);
let mut row = String::new();
// Left padding
row.push_str(&" ".repeat(self.padding_left as usize));
// Left alignment space
row.push_str(&" ".repeat(horizontal_offset));
// Apply text styling
if content.is_some() {
let mut styled = glyphs::style(text);
if let Some(fg) = &self.foreground {
styled = styled.fg(*fg);
}
if let Some(bg) = &self.background {
styled = styled.bg(*bg);
}
if self.bold {
styled = styled.bold();
}
if self.italic {
styled = styled.italic();
}
if self.underline {
styled = styled.underline();
}
if self.strikethrough {
styled = styled.strikethrough();
}
row.push_str(&styled.to_string());
}
// Right alignment space
let used = horizontal_offset + text_width;
let remaining = available_width.saturating_sub(used);
row.push_str(&" ".repeat(remaining));
// Right padding
row.push_str(&" ".repeat(self.padding_right as usize));
row
}
fn calculate_content_width(&self, lines: &[&str]) -> usize {
let max_line_width = lines
.iter()
.map(|line| UnicodeWidthStr::width(*line))
.max()
.unwrap_or(0);
if let Some(width) = self.width {
let border_width = if matches!(self.border, Border::None) {
0
} else {
2
};
let padding_width = self.padding_left as usize + self.padding_right as usize;
(width as usize)
.saturating_sub(border_width)
.saturating_sub(padding_width)
} else if let Some(max_width) = self.max_width {
max_line_width.min(max_width as usize)
} else {
max_line_width
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_render() {
let style = Style::new();
let output = style.render("Hello");
assert_eq!(output, "Hello");
}
#[test]
fn test_padding() {
let style = Style::new().padding(1, 2);
let output = style.render("Hi");
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 3); // 1 top + content + 1 bottom
assert!(lines[1].contains("Hi"));
}
#[test]
fn test_border_rounded() {
let style = Style::new().border(Border::Rounded);
let output = style.render("X");
assert!(output.contains('â•'));
assert!(output.contains('â•°'));
assert!(output.contains('│'));
}
#[test]
fn test_border_double() {
let style = Style::new().border(Border::Double);
let output = style.render("X");
assert!(output.contains('â•”'));
assert!(output.contains('╚'));
assert!(output.contains('â•‘'));
}
#[test]
fn test_inline_mode() {
let style = Style::new().inline().bold();
let output = style.render("Bold");
// Should contain ANSI codes but no newlines/borders
assert!(output.contains("\x1b["));
assert!(!output.contains('\n'));
}
#[test]
fn test_alignment_center() {
let style = Style::new().width(20).align(Position::Center);
let output = style.render("Hi");
// "Hi" is 2 chars, width is 20, so ~9 spaces on each side
let trimmed = output.trim();
assert_eq!(trimmed, "Hi");
}
#[test]
fn test_multiline() {
let style = Style::new().border(Border::Normal);
let output = style.render("Line 1\nLine 2");
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 4); // top border + 2 lines + bottom border
}
}