Skip to main content

tui/rendering/
style.rs

1use crossterm::style::Color;
2
3/// Text styling: foreground/background colors and attributes (bold, italic, etc.).
4#[allow(clippy::struct_excessive_bools)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub struct Style {
7    pub fg: Option<Color>,
8    pub bg: Option<Color>,
9    pub bold: bool,
10    pub italic: bool,
11    pub underline: bool,
12    pub dim: bool,
13    pub strikethrough: bool,
14}
15
16impl Style {
17    pub fn fg(color: Color) -> Self {
18        Self::default().color(color)
19    }
20
21    pub fn color(mut self, color: Color) -> Self {
22        self.fg = Some(color);
23        self
24    }
25
26    pub fn bg_color(mut self, color: Color) -> Self {
27        self.bg = Some(color);
28        self
29    }
30
31    pub fn bold(mut self) -> Self {
32        self.bold = true;
33        self
34    }
35
36    pub fn italic(mut self) -> Self {
37        self.italic = true;
38        self
39    }
40
41    pub fn underline(mut self) -> Self {
42        self.underline = true;
43        self
44    }
45
46    #[allow(dead_code)]
47    pub fn dim(mut self) -> Self {
48        self.dim = true;
49        self
50    }
51
52    pub fn strikethrough(mut self) -> Self {
53        self.strikethrough = true;
54        self
55    }
56
57    /// Merge `other` on top of `self`. Booleans are OR'd, `Option` fields
58    /// prefer `other` when `Some`.
59    pub fn merge(self, other: Self) -> Self {
60        Self {
61            fg: other.fg.or(self.fg),
62            bg: other.bg.or(self.bg),
63            bold: self.bold || other.bold,
64            italic: self.italic || other.italic,
65            underline: self.underline || other.underline,
66            dim: self.dim || other.dim,
67            strikethrough: self.strikethrough || other.strikethrough,
68        }
69    }
70}