ratada 0.5.0

A ratatui widget toolkit: driver, modals, forms, pickers, theming
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
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
//! A multi-mode color picker modal: pick from named swatches, a hue/saturation
//! grid, a grayscale ramp or the theme palette.
//!
//! `m` cycles the modes (carrying the focused color over via perceptual
//! distance). `Space` returns the focused color directly; `Enter` hands it to
//! the full [`color_picker`] for editing. `y` copies its
//! hex. Each mode shows a focus preview (swatch, hex/hsl, nearest name,
//! light/dark contrast).

mod cells;
mod interaction;
mod named;
mod render;

pub use named::NAMED_COLORS;

use cells::{mode_cells, nearest, palette_entries};
use interaction::handle;
use render::{box_size, render_box};

use std::{cell::Cell, io};

use ratatui::Frame;

use super::{
    color_picker, layout::centered_rect, modal::ModalSignal, overlay::popup,
    terminal::Tui,
};
use crate::theme::{Color, Skin};

/// The grid's starting lightness plane and the `[`/`]` step.
const GRID_LIGHT_DEFAULT: f32 = 0.5;
const GRID_LIGHT_STEP: f32 = 0.08;

impl Mode {
    const ALL: [Mode; 4] =
        [Mode::Names, Mode::Grid, Mode::Grays, Mode::Palette];

    fn label(self) -> &'static str {
        match self {
            Mode::Names => "Names",
            Mode::Grid => "Grid",
            Mode::Grays => "Grays",
            Mode::Palette => "Palette",
        }
    }

    fn next(self) -> Mode {
        match self {
            Mode::Names => Mode::Grid,
            Mode::Grid => Mode::Grays,
            Mode::Grays => Mode::Palette,
            Mode::Palette => Mode::Names,
        }
    }

    /// Whether this mode renders as a named list (vs. a color grid).
    fn is_list(self) -> bool {
        matches!(self, Mode::Names | Mode::Palette)
    }
}

/// Grid dimensions (hue columns × saturation rows) and grayscale steps.
const GRID_COLS: usize = 18;
const GRID_ROWS: usize = 8;
const GRAY_STEPS: usize = 16;

/// Visible list rows before a list-style mode scrolls.
const VISIBLE_ROWS: u16 = 12;

/// The view a swatch picker is showing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
    Names,
    Grid,
    Grays,
    Palette,
}

/// A single selectable color, optionally named (named ones render list-style).
#[derive(Clone)]
struct Swatch {
    color: Color,
    name: Option<String>,
}

/// What `Enter`/`Space` produced: edit in the color picker, or take as-is.
enum Choice {
    Pick(Color),
    Edit(Color),
}

/// The picker state (threaded through [`popup`]).
struct State {
    mode: Mode,
    cursor: usize,
    offset: Cell<usize>,
    cells: Vec<Swatch>,
    cols: usize,
    palette: Vec<(&'static str, Color)>,
    grid_light: f32,
    filter: String,
    filtering: bool,
}

impl State {
    /// Rebuilds the current mode's cells (after a mode/filter/lightness change).
    fn rebuild(&mut self) {
        let (cells, cols) =
            mode_cells(self.mode, &self.palette, self.grid_light, &self.filter);
        self.cells = cells;
        self.cols = cols.max(1);
        if self.cursor >= self.cells.len() {
            self.cursor = self.cells.len().saturating_sub(1);
        }
    }

    fn focus_color(&self) -> Color {
        self.cells
            .get(self.cursor)
            .map_or(Color::Default, |cell| cell.color)
    }

    /// Cycles to the next mode, carrying the focused color to its nearest cell.
    fn switch_mode(&mut self) {
        let current = self.focus_color();
        self.mode = self.mode.next();
        self.filter.clear();
        self.filtering = false;
        self.rebuild();
        self.cursor = nearest(&self.cells, current);
    }
}

/// Which view the [`color_chooser`] starts in (also its current view).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Start {
    /// Open on the swatch grid.
    Swatches,
    /// Open on the channel picker.
    Picker,
}

/// How the swatch view was left.
enum SwatchExit {
    Pick(Color),
    Edit(Color),
    Cancel,
    Quit,
}

/// The swatch view state that survives a trip through the picker, so returning
/// (`Esc`/`s`) restores the same mode and grid lightness.
#[derive(Clone, Copy)]
struct SwatchMemory {
    mode: Mode,
    grid_light: f32,
}

impl Default for SwatchMemory {
    fn default() -> Self {
        Self {
            mode: Mode::Names,
            grid_light: GRID_LIGHT_DEFAULT,
        }
    }
}

/// Opens the color chooser starting in the swatch view. See [`color_chooser`].
pub fn swatch_picker(
    tui: &mut Tui,
    skin: &Skin,
    title: &str,
    initial: Option<Color>,
    render_bg: impl Fn(&mut Frame),
) -> io::Result<ModalSignal<Color>> {
    color_chooser(tui, skin, title, initial, Start::Swatches, render_bg)
}

/// A combined color chooser that alternates between the swatch view and the full
/// [`color_picker`]. Swatch view: `↑`/`↓`/`←`/`→` (or `k`/`j`/`h`/`l`) move, `m`
/// cycles the mode, `[`/`]` shift the grid lightness, `/` filters the named list,
/// `y` copies the hex, `Space` returns the color directly, `Enter` opens it in the
/// picker, `Esc` cancels. Picker view: `Enter` confirms, `s` switches to the
/// swatch view, `Esc` steps back to it (or cancels if the picker was opened
/// first and no swatch view has been shown). The focused color carries across
/// both ways, and the swatch view keeps its mode/lightness across a round trip.
/// `start` picks the initial view; `initial` highlights its nearest color.
pub fn color_chooser(
    tui: &mut Tui,
    skin: &Skin,
    title: &str,
    initial: Option<Color>,
    start: Start,
    render_bg: impl Fn(&mut Frame),
) -> io::Result<ModalSignal<Color>> {
    let mut view = start;
    let mut color = initial;
    let mut memory = SwatchMemory::default();
    // Whether the swatch view has been shown, so the picker's `Esc` (back) knows
    // whether there is a swatch view to return to (else it cancels).
    let mut swatch_seen = matches!(start, Start::Swatches);
    loop {
        match view {
            Start::Swatches => {
                swatch_seen = true;
                let (exit, updated) =
                    run_swatch(tui, skin, title, color, memory, &render_bg)?;
                memory = updated;
                match exit {
                    SwatchExit::Pick(chosen) => {
                        return Ok(ModalSignal::Value(chosen));
                    }
                    SwatchExit::Edit(chosen) => {
                        color = Some(chosen);
                        view = Start::Picker;
                    }
                    SwatchExit::Cancel => return Ok(ModalSignal::Cancelled),
                    SwatchExit::Quit => return Ok(ModalSignal::Quit),
                }
            }
            Start::Picker => {
                let exit = color_picker::color_picker(
                    tui, skin, title, color, &render_bg,
                )?;
                match exit {
                    color_picker::ColorExit::Done(chosen) => {
                        return Ok(ModalSignal::Value(chosen));
                    }
                    color_picker::ColorExit::Swatches(chosen) => {
                        color = Some(chosen);
                        view = Start::Swatches;
                    }
                    color_picker::ColorExit::Back(chosen) => {
                        // Back to swatches if we came from there, else cancel.
                        if swatch_seen {
                            color = Some(chosen);
                            view = Start::Swatches;
                        } else {
                            return Ok(ModalSignal::Cancelled);
                        }
                    }
                    color_picker::ColorExit::Quit => {
                        return Ok(ModalSignal::Quit);
                    }
                }
            }
        }
    }
}

/// Runs one pass of the swatch view, seeded from and reporting back `memory`
/// (mode + grid lightness) so a round-trip through the picker is seamless.
fn run_swatch(
    tui: &mut Tui,
    skin: &Skin,
    title: &str,
    initial: Option<Color>,
    memory: SwatchMemory,
    render_bg: impl Fn(&mut Frame),
) -> io::Result<(SwatchExit, SwatchMemory)> {
    let mut state = State {
        mode: memory.mode,
        cursor: 0,
        offset: Cell::new(0),
        cells: Vec::new(),
        cols: 1,
        palette: palette_entries(&skin.palette),
        grid_light: memory.grid_light,
        filter: String::new(),
        filtering: false,
    };
    state.rebuild();
    if let Some(color) = initial {
        state.cursor = nearest(&state.cells, color);
    }
    let signal = popup(
        tui,
        &mut state,
        |area, state: &State| {
            centered_rect(box_size(state).0, box_size(state).1, area)
        },
        |frame, _| render_bg(frame),
        |frame, rect, state: &State| {
            render_box(frame, rect, skin, title, state);
        },
        handle,
    )?;
    let memory = SwatchMemory {
        mode: state.mode,
        grid_light: state.grid_light,
    };
    let exit = match signal {
        ModalSignal::Value(Choice::Pick(color)) => SwatchExit::Pick(color),
        ModalSignal::Value(Choice::Edit(color)) => SwatchExit::Edit(color),
        ModalSignal::Cancelled => SwatchExit::Cancel,
        ModalSignal::Quit => SwatchExit::Quit,
    };
    Ok((exit, memory))
}

#[cfg(test)]
mod tests {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    use super::{
        interaction::{done, move_horizontal, move_vertical},
        *,
    };
    use crate::overlay::PopupFlow;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn ctrl(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::CONTROL)
    }

    fn state() -> State {
        let mut state = State {
            mode: Mode::Names,
            cursor: 0,
            offset: Cell::new(0),
            cells: Vec::new(),
            cols: 1,
            palette: vec![("accent", Color::hex("#8bd3cd"))],
            grid_light: GRID_LIGHT_DEFAULT,
            filter: String::new(),
            filtering: false,
        };
        state.rebuild();
        state
    }

    #[test]
    fn names_mode_matches_named_colors() {
        let state = state();
        assert_eq!(state.cells.len(), NAMED_COLORS.len());
        assert_eq!(state.cells[0].color, NAMED_COLORS[0].1);
    }

    #[test]
    fn m_cycles_all_modes() {
        let mut state = state();
        let seen: Vec<Mode> = (0..4)
            .map(|_| {
                let mode = state.mode;
                handle(&mut state, key(KeyCode::Char('m')));
                mode
            })
            .collect();
        assert_eq!(seen, Mode::ALL);
        assert_eq!(state.mode, Mode::Names);
    }

    #[test]
    fn switching_mode_carries_the_color_close() {
        let mut state = state();
        state.cursor = 6; // Crimson
        let before = state.focus_color();
        handle(&mut state, key(KeyCode::Char('m'))); // -> Grid
        assert_eq!(state.mode, Mode::Grid);
        assert!(state.focus_color().distance(before) < 0.2);
    }

    #[test]
    fn grid_left_wraps_hue_but_up_clamps() {
        let mut state = state();
        state.mode = Mode::Grid;
        state.rebuild();
        state.cursor = 0; // top-left
        move_horizontal(&mut state, -1);
        assert_eq!(state.cursor, GRID_COLS - 1); // wrapped to row end
        state.cursor = 0;
        move_vertical(&mut state, -1);
        assert_eq!(state.cursor, 0); // clamped at the top
    }

    #[test]
    fn list_up_wraps_to_the_end() {
        let mut state = state();
        handle(&mut state, key(KeyCode::Up));
        assert_eq!(state.cursor, NAMED_COLORS.len() - 1);
    }

    #[test]
    fn brackets_change_the_grid_lightness() {
        let mut state = state();
        state.mode = Mode::Grid;
        state.rebuild();
        let before = state.grid_light;
        let color_before = state.focus_color();
        handle(&mut state, key(KeyCode::Char(']')));
        assert!(state.grid_light > before);
        assert_ne!(state.focus_color(), color_before);
    }

    #[test]
    fn filter_narrows_the_named_list() {
        let mut state = state();
        handle(&mut state, key(KeyCode::Char('/')));
        assert!(state.filtering);
        for ch in "crim".chars() {
            handle(&mut state, key(KeyCode::Char(ch)));
        }
        assert!(state.cells.len() < NAMED_COLORS.len());
        assert_eq!(state.cursor, 0);
        assert!(
            state
                .cells
                .iter()
                .any(|cell| cell.name.as_deref() == Some("Crimson"))
        );
    }

    #[test]
    fn space_picks_and_enter_edits() {
        let mut state = state();
        state.cursor = 3;
        match done(&state, true) {
            PopupFlow::Done(Choice::Pick(color)) => {
                assert_eq!(color, NAMED_COLORS[3].1);
            }
            _ => panic!("expected a direct pick"),
        }
        match done(&state, false) {
            PopupFlow::Done(Choice::Edit(color)) => {
                assert_eq!(color, NAMED_COLORS[3].1);
            }
            _ => panic!("expected an edit hand-off"),
        }
    }

    #[test]
    fn ctrl_chords_do_not_pick_move_or_switch_mode() {
        let mut state = state();
        state.cursor = 3;
        // Ctrl+Space would otherwise pick the focused color and close.
        assert!(matches!(
            handle(&mut state, ctrl(KeyCode::Char(' '))),
            PopupFlow::Continue,
        ));
        // Crossterm reports Ctrl+J/Ctrl+H as plain characters, so a bare match
        // on the code alone would move the cursor.
        handle(&mut state, ctrl(KeyCode::Char('j')));
        assert_eq!(state.cursor, 3);
        handle(&mut state, ctrl(KeyCode::Char('m')));
        assert_eq!(state.mode, Mode::Names);
    }

    #[test]
    fn a_ctrl_chord_does_not_type_into_the_filter() {
        let mut state = state();
        handle(&mut state, key(KeyCode::Char('/')));
        handle(&mut state, ctrl(KeyCode::Char('u')));
        assert_eq!(state.filter, "", "Ctrl+U typed a character");
        // A bare character still reaches the filter.
        handle(&mut state, key(KeyCode::Char('u')));
        assert_eq!(state.filter, "u");
    }

    /// The other half of the rule: `AltGr` is reported as `Ctrl+Alt` yet types
    /// a real character, so the filter must accept it. Guarding that arm with
    /// `is_bare_character` instead of `!is_command` would make `@`, `\` and `[`
    /// untypeable on a German keyboard.
    #[test]
    fn altgr_characters_still_reach_the_filter() {
        let mut state = state();
        handle(&mut state, key(KeyCode::Char('/')));
        for ch in ['@', '\\', '['] {
            handle(
                &mut state,
                KeyEvent::new(
                    KeyCode::Char(ch),
                    KeyModifiers::CONTROL | KeyModifiers::ALT,
                ),
            );
        }
        assert_eq!(state.filter, "@\\[");
    }
}