demand 2.0.0

A CLI prompt library
Documentation
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
use std::sync::LazyLock;

use termcolor::{Color, ColorSpec};

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

#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum CursorShape {
    Block,
    Underline,
}

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

    /// 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,

    /// Char to use for the cursor
    pub cursor_shape: CursorShape,
    /// the color when there isn't text to get color from
    pub cursor_style: ColorSpec,
    /// use cursor_style even when there is text to get color from
    pub force_style: bool,

    /// 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 color
    pub focused_button: ColorSpec,
    /// Blurred button color
    pub blurred_button: ColorSpec,

    /// Error indicator color
    pub error_indicator: ColorSpec,

    // Breadcrumb navigation (for Wizard)
    /// Active/current breadcrumb section color
    pub breadcrumb_active: ColorSpec,
    /// Clickable/visited breadcrumb section color
    pub breadcrumb_clickable: ColorSpec,
    /// Future/not-yet-visited breadcrumb section color
    pub breadcrumb_future: ColorSpec,
    /// Breadcrumb separator string (e.g., " > " or " → ")
    pub breadcrumb_separator: String,
}

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)));

        // TODO: theme them
        let mut cursor_style = ColorSpec::new();
        cursor_style
            .set_fg(Some(Color::White))
            .set_bg(Some(Color::Black));

        Self {
            title: ColorSpec::new(),
            error_indicator: ColorSpec::new(),
            description: ColorSpec::new(),
            cursor: ColorSpec::new(),
            cursor_str: String::from(""),
            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,

            // TODO: theme these
            cursor_shape: CursorShape::Block,
            cursor_style,
            force_style: true,

            // Breadcrumb navigation
            breadcrumb_active: make_color(Color::Ansi256(7)),
            breadcrumb_clickable: make_color(Color::Ansi256(8)),
            breadcrumb_future: make_color(Color::Ansi256(8)),
            breadcrumb_separator: String::from(" > "),
        }
    }

    pub fn real_cursor_color(&self, other: Option<&ColorSpec>) -> ColorSpec {
        // let mut c = self.input_cursor.clone();
        let other = if self.force_style {
            &self.cursor_style
        } else {
            other.unwrap_or(&self.cursor_style)
        };

        let mut c = ColorSpec::new();
        match self.cursor_shape {
            CursorShape::Block => {
                c.set_bg(other.fg().copied());
                c.set_fg(other.bg().copied());
            }
            CursorShape::Underline => {
                c.set_bg(other.bg().copied());
                c.set_fg(other.fg().copied());
                c.set_underline(true);
            }
        }
        // c.set_fg(self.input_cursor.bg().copied())
        //     .set_bg(self.input_cursor.bg().copied());
        c
    }

    /// 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)));

        // TODO: theme them
        let mut cursor_style = ColorSpec::new();
        cursor_style
            .set_fg(Some(Color::White))
            .set_bg(Some(Color::Black));

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

            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,

            // TODO: theme these
            cursor_shape: CursorShape::Block,
            cursor_style,
            force_style: true,

            // Breadcrumb navigation
            breadcrumb_active: make_color(fuchsia),
            breadcrumb_clickable: make_color(indigo),
            breadcrumb_future: make_color(Color::Ansi256(243)),
            breadcrumb_separator: String::from(" > "),
        }
    }

    /// 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));

        // TODO: theme them
        let mut cursor_style = ColorSpec::new();
        cursor_style
            .set_fg(Some(Color::White))
            .set_bg(Some(Color::Black));

        Self {
            title,
            error_indicator: make_color(red),
            description: make_color(comment),
            cursor: make_color(yellow),
            cursor_str: String::from(""),

            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,

            // TODO: theme these
            cursor_shape: CursorShape::Block,
            cursor_style,
            force_style: true,

            // Breadcrumb navigation
            breadcrumb_active: make_color(yellow),
            breadcrumb_clickable: make_color(purple),
            breadcrumb_future: make_color(comment),
            breadcrumb_separator: String::from(" > "),
        }
    }

    /// 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)));

        // TODO: theme them
        let mut cursor_style = ColorSpec::new();
        cursor_style
            .set_fg(Some(Color::White))
            .set_bg(Some(Color::Black));

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

            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,

            // TODO: theme these
            cursor_shape: CursorShape::Block,
            cursor_style,
            force_style: true,

            // Breadcrumb navigation
            breadcrumb_active: make_color(Color::Ansi256(3)),
            breadcrumb_clickable: make_color(Color::Ansi256(6)),
            breadcrumb_future: make_color(Color::Ansi256(8)),
            breadcrumb_separator: String::from(" > "),
        }
    }

    /// 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));

        // TODO: theme them
        let mut cursor_style = ColorSpec::new();
        cursor_style
            .set_fg(Some(Color::White))
            .set_bg(Some(Color::Black));

        Self {
            title,
            error_indicator: make_color(red),
            description: make_color(subtext0),
            cursor: make_color(pink),
            cursor_str: String::from(""),

            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,

            // TODO: theme these
            cursor_shape: CursorShape::Block,
            cursor_style,
            force_style: true,

            // Breadcrumb navigation
            breadcrumb_active: make_color(pink),
            breadcrumb_clickable: make_color(mauve),
            breadcrumb_future: make_color(overlay0),
            breadcrumb_separator: String::from(" > "),
        }
    }

    /// 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
}