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
use once_cell::sync::Lazy;
use termcolor::{Color, ColorSpec};

pub(crate) static DEFAULT: Lazy<Theme> = Lazy::new(Theme::default);

/// Theme for styling the UI.
///
/// # Example
///
/// ```
/// use demand::Theme;
///
/// let custom_theme = Theme {
///   selected_prefix: String::from(" •"),
///   unselected_prefix: String::from("  "),
/// ..Theme::default()
/// };
/// ```
#[derive(Clone, Debug)]
pub struct Theme {
    /// Prompt title color
    pub title: ColorSpec,
    /// Prompt description color
    pub description: ColorSpec,
    /// Cursor color
    pub cursor: ColorSpec,

    /// Selected option color
    pub selected_option: ColorSpec,
    /// Selected option prefix color
    pub selected_prefix: String,
    /// Selected prefix foreground color
    pub selected_prefix_fg: ColorSpec,
    /// Unselected option color
    pub unselected_option: ColorSpec,
    /// Unselected option prefix color
    pub unselected_prefix: String,
    /// Unselected prefix foreground color
    pub unselected_prefix_fg: ColorSpec,

    /// Input cursor color
    pub input_cursor: ColorSpec,
    /// Input placeholder color
    pub input_placeholder: ColorSpec,
    /// Input prompt color
    pub input_prompt: ColorSpec,

    /// Help item key color
    pub help_key: ColorSpec,
    /// Help item description color
    pub help_desc: ColorSpec,
    /// Help item separator color
    pub help_sep: ColorSpec,

    /// Focused button
    pub focused_button: ColorSpec,
    /// Blurred button
    pub blurred_button: ColorSpec,

    /// Error indicator color
    pub error_indicator: ColorSpec,
}

impl Theme {
    /// Create a new theme with no colors.
    pub fn new() -> Self {
        let placeholder = Color::Ansi256(8);

        let mut focused_button = make_color(Color::Ansi256(0));
        focused_button.set_bg(Some(Color::Ansi256(7)));

        let mut blurred_button = make_color(Color::Ansi256(7));
        blurred_button.set_bg(Some(Color::Ansi256(0)));

        Self {
            title: ColorSpec::new(),
            error_indicator: ColorSpec::new(),
            description: ColorSpec::new(),
            cursor: ColorSpec::new(),
            selected_prefix: String::from("[•]"),
            selected_prefix_fg: ColorSpec::new(),
            selected_option: ColorSpec::new(),
            unselected_prefix: String::from("[ ]"),
            unselected_prefix_fg: ColorSpec::new(),
            unselected_option: ColorSpec::new(),
            input_cursor: ColorSpec::new(),
            input_placeholder: make_color(placeholder),
            input_prompt: ColorSpec::new(),
            help_key: ColorSpec::new(),
            help_desc: ColorSpec::new(),
            help_sep: ColorSpec::new(),
            focused_button,
            blurred_button,
        }
    }

    /// Create a new theme with the charm color scheme
    pub fn charm() -> Self {
        let normal = Color::Ansi256(252);
        let indigo = Color::Rgb(117, 113, 249);
        let red = Color::Rgb(255, 70, 114);
        let fuchsia = Color::Rgb(247, 128, 226);
        let green = Color::Rgb(2, 191, 135);
        let cream = Color::Rgb(255, 253, 245);

        let mut title = make_color(indigo);
        title.set_bold(true);

        let mut focused_button = make_color(cream);
        focused_button.set_bg(Some(fuchsia));

        let mut blurred_button = make_color(normal);
        blurred_button.set_bg(Some(Color::Ansi256(238)));

        Self {
            title,
            error_indicator: make_color(red),
            description: make_color(Color::Ansi256(243)),
            cursor: make_color(fuchsia),

            selected_prefix: String::from(" ✓"),
            selected_prefix_fg: make_color(Color::Rgb(2, 168, 119)),
            selected_option: make_color(green),
            unselected_prefix: String::from(" •"),
            unselected_prefix_fg: make_color(Color::Ansi256(243)),
            unselected_option: make_color(normal),

            input_cursor: make_color(green),
            input_placeholder: make_color(Color::Ansi256(238)),
            input_prompt: make_color(fuchsia),

            help_key: make_color(Color::Rgb(98, 98, 98)),
            help_desc: make_color(Color::Rgb(74, 74, 74)),
            help_sep: make_color(Color::Rgb(60, 60, 60)),

            focused_button,
            blurred_button,
        }
    }

    /// Create a new theme with the dracula color scheme
    pub fn dracula() -> Self {
        let background = Color::Rgb(40, 42, 54); // #282a36
        let foreground = Color::Rgb(248, 248, 242); // #f8f8f2
        let comment = Color::Rgb(98, 114, 164); // #6272a4
        let green = Color::Rgb(80, 250, 123); // #50fa7b
        let purple = Color::Rgb(189, 147, 249); // #bd93f9
        let red = Color::Rgb(255, 85, 85); // ff5555
        let yellow = Color::Rgb(241, 250, 140); // f1fa8c

        let mut title = make_color(purple);
        title.set_bold(true);

        let mut focused_button = make_color(yellow);
        focused_button.set_bg(Some(purple));

        let mut blurred_button = make_color(foreground);
        blurred_button.set_bg(Some(background));

        Self {
            title,
            error_indicator: make_color(red),
            description: make_color(comment),
            cursor: make_color(yellow),

            selected_prefix: String::from(" [•]"),
            selected_prefix_fg: make_color(green),
            selected_option: make_color(green),
            unselected_prefix: String::from(" [ ]"),
            unselected_prefix_fg: make_color(comment),
            unselected_option: make_color(foreground),

            input_cursor: make_color(yellow),
            input_placeholder: make_color(comment),
            input_prompt: make_color(yellow),

            help_key: make_color(Color::Rgb(98, 98, 98)),
            help_desc: make_color(Color::Rgb(74, 74, 74)),
            help_sep: make_color(Color::Rgb(60, 60, 60)),

            focused_button,
            blurred_button,
        }
    }

    /// Create a new theme with the base16 color scheme
    pub fn base16() -> Self {
        let mut title = make_color(Color::Ansi256(6));
        title.set_bold(true);

        let mut focused_button = make_color(Color::Ansi256(7));
        focused_button.set_bg(Some(Color::Ansi256(5)));

        let mut blurred_button = make_color(Color::Ansi256(7));
        blurred_button.set_bg(Some(Color::Ansi256(0)));

        Self {
            title,
            error_indicator: make_color(Color::Ansi256(9)),
            description: make_color(Color::Ansi256(8)),
            cursor: make_color(Color::Ansi256(3)),

            selected_prefix: String::from(" [•]"),
            selected_prefix_fg: make_color(Color::Ansi256(2)),
            selected_option: make_color(Color::Ansi256(2)),
            unselected_prefix: String::from(" [ ]"),
            unselected_prefix_fg: make_color(Color::Ansi256(7)),
            unselected_option: make_color(Color::Ansi256(7)),

            input_cursor: make_color(Color::Ansi256(5)),
            input_placeholder: make_color(Color::Ansi256(8)),
            input_prompt: make_color(Color::Ansi256(3)),

            help_key: make_color(Color::Rgb(98, 98, 98)),
            help_desc: make_color(Color::Rgb(74, 74, 74)),
            help_sep: make_color(Color::Rgb(60, 60, 60)),

            focused_button,
            blurred_button,
        }
    }

    /// Create a new theme with the catppuccin color scheme
    pub fn catppuccin() -> Self {
        let base = Color::Rgb(30, 30, 46);
        let text = Color::Rgb(205, 214, 244);
        let subtext0 = Color::Rgb(166, 173, 200);
        let overlay0 = Color::Rgb(108, 112, 134);
        let overlay1 = Color::Rgb(127, 132, 156);
        let green = Color::Rgb(166, 227, 161);
        let red = Color::Rgb(243, 139, 168);
        let pink = Color::Rgb(245, 194, 231);
        let mauve = Color::Rgb(203, 166, 247);
        let cursor = Color::Rgb(245, 224, 220);

        let mut title = make_color(mauve);
        title.set_bold(true);

        let mut focused_button = make_color(base);
        focused_button.set_bg(Some(pink));

        let mut blurred_button = make_color(text);
        blurred_button.set_bg(Some(base));

        Self {
            title,
            error_indicator: make_color(red),
            description: make_color(subtext0),
            cursor: make_color(pink),

            selected_prefix: String::from(" [•]"),
            selected_prefix_fg: make_color(green),
            selected_option: make_color(green),
            unselected_prefix: String::from(" [ ]"),
            unselected_prefix_fg: make_color(text),
            unselected_option: make_color(text),

            input_cursor: make_color(cursor),
            input_placeholder: make_color(overlay0),
            input_prompt: make_color(pink),

            help_key: make_color(subtext0),
            help_desc: make_color(overlay1),
            help_sep: make_color(subtext0),

            focused_button,
            blurred_button,
        }
    }

    /// Create a new color with foreground color from an RGB value.
    pub fn color_rgb(r: u8, g: u8, b: u8) -> ColorSpec {
        make_color(Color::Rgb(r, g, b))
    }

    /// Create a new color with foreground color from an ANSI 256 color code.
    pub fn color_ansi256(n: u8) -> ColorSpec {
        make_color(Color::Ansi256(n))
    }
}

impl Default for Theme {
    fn default() -> Self {
        if console::colors_enabled_stderr() {
            Theme::charm()
        } else {
            Theme::new()
        }
    }
}

fn make_color(color: Color) -> ColorSpec {
    let mut spec = ColorSpec::new();
    spec.set_fg(Some(color));
    spec
}