Skip to main content

legume_tui/
look.rs

1//! The styles the save dialog, the saved-figures strip, the form's rows and
2//! the file browser draw in, so they need no map theme: the terminal's own
3//! colours by default, or a map viewer's [`crate::chrome::Chrome`] with
4//! feature `map`.
5
6use ratatui::style::{Color, Modifier, Style};
7
8/// Styles for a popup or a strip. Built with [`Look::new`] or
9/// [`Look::terminal`]: more styles may come, so not field by field.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11#[non_exhaustive]
12pub struct Look {
13    /// The page: text on the background.
14    pub page: Style,
15    /// What matters.
16    pub bold: Style,
17    /// Key hints and other secondary lines.
18    pub hint: Style,
19    /// The line under a cursor.
20    pub selected: Style,
21    /// Borders and rules.
22    pub border: Style,
23    /// What wants attention: a warning, a refusal, clap's complaint.
24    pub accent: Style,
25}
26
27/// [`Look::terminal`]'s accent: bold yellow.
28const ACCENT: Style = Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD);
29
30impl Look {
31    /// A look of these styles.
32    #[must_use]
33    pub const fn new(
34        page: Style,
35        bold: Style,
36        hint: Style,
37        selected: Style,
38        border: Style,
39        accent: Style,
40    ) -> Self {
41        Self {
42            page,
43            bold,
44            hint,
45            selected,
46            border,
47            accent,
48        }
49    }
50
51    /// The terminal's colours: bold, grey hints, the cursor's line
52    /// reversed (pinto's), warnings bold yellow.
53    #[must_use]
54    pub const fn terminal() -> Self {
55        let hint = Style::new().fg(Color::DarkGray);
56        Self::new(
57            Style::new(),
58            Style::new().add_modifier(Modifier::BOLD),
59            hint,
60            Style::new().add_modifier(Modifier::REVERSED),
61            hint,
62            ACCENT,
63        )
64    }
65
66    /// Key names, typed values, the value in focus: the accent in bold.
67    #[must_use]
68    pub const fn highlight(&self) -> Style {
69        self.accent.add_modifier(Modifier::BOLD)
70    }
71
72    /// This look with `accent` for what wants attention.
73    #[must_use]
74    pub const fn with_accent(mut self, accent: Style) -> Self {
75        self.accent = accent;
76        self
77    }
78
79    /// This look with `border` for borders and rules: a look cannot be
80    /// built field by field, so a change of one style goes through these.
81    #[must_use]
82    pub const fn with_border(mut self, border: Style) -> Self {
83        self.border = border;
84        self
85    }
86
87    /// This look with `page` for the page.
88    #[must_use]
89    pub const fn with_page(mut self, page: Style) -> Self {
90        self.page = page;
91        self
92    }
93
94    /// This look with `hint` for key hints and secondary lines.
95    #[must_use]
96    pub const fn with_hint(mut self, hint: Style) -> Self {
97        self.hint = hint;
98        self
99    }
100}
101
102impl Default for Look {
103    fn default() -> Self {
104        Self::terminal()
105    }
106}
107
108#[cfg(feature = "map")]
109impl From<crate::chrome::Chrome> for Look {
110    /// A screen's chrome: a theme's colours, or the terminal's; the
111    /// terminal's accent either way, as a chrome has none of its own.
112    fn from(c: crate::chrome::Chrome) -> Self {
113        Self::new(
114            c.page(),
115            c.bold(),
116            c.hint(),
117            c.selected(),
118            c.border(),
119            ACCENT,
120        )
121    }
122}
123
124#[cfg(test)]
125#[path = "tests/look.rs"]
126mod tests;