ratada 0.2.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
483
484
485
486
487
488
//! A schema-driven form modal.
//!
//! Build it from a list of [`Field`]s, run it, then read the values back. All
//! fields are visible; `Tab`/`BackTab` step between them (wrapping); the
//! focused row is tinted, changed fields get a `*` marker, `Ctrl+S` saves and
//! `Esc` cancels. Field behaviour: text edits inline; multiline edits inline
//! and opens `$EDITOR` with `Ctrl+G`; bool toggles with `Space`; choice cycles
//! with `←`/`→`; date opens the calendar with `Enter`.

use std::io;

use chrono::NaiveDate;
use crossterm::event::{KeyCode, KeyModifiers};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, Clear, Paragraph},
};

use super::{
    date_picker, editor,
    input::InputField,
    layout::centered_rect,
    modal::ModalSignal,
    nav, overlay, shortcut_hints, style,
    terminal::{Tui, TuiEvent},
    textarea::TextArea,
};
use crate::theme::Skin;

const MULTILINE_ROWS: u16 = 4;
const LABEL_WIDTH: u16 = 14;

/// A read-only snapshot of a field's value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldValue {
    /// A single-line text value.
    Text(String),
    /// A multi-line text value.
    Multiline(String),
    /// A checkbox value.
    Bool(bool),
    /// The selected choice label.
    Choice(String),
    /// A date, or `None` when cleared.
    Date(Option<NaiveDate>),
}

enum FieldState {
    Text {
        input: InputField,
        initial: String,
    },
    Multiline {
        area: TextArea,
        initial: String,
    },
    Bool {
        value: bool,
        initial: bool,
    },
    Choice {
        index: usize,
        initial: usize,
        options: Vec<String>,
    },
    Date {
        value: Option<NaiveDate>,
        initial: Option<NaiveDate>,
    },
}

/// One labelled form field.
pub struct Field {
    label: String,
    state: FieldState,
}

impl Field {
    /// A single-line text field pre-filled with `initial`.
    pub fn text(label: impl Into<String>, initial: &str) -> Self {
        Self {
            label: label.into(),
            state: FieldState::Text {
                input: InputField::new(initial),
                initial: initial.to_string(),
            },
        }
    }

    /// A multi-line text field pre-filled with `initial`.
    pub fn multiline(label: impl Into<String>, initial: &str) -> Self {
        Self {
            label: label.into(),
            state: FieldState::Multiline {
                area: TextArea::new(initial),
                initial: initial.to_string(),
            },
        }
    }

    /// A checkbox field starting at `initial`.
    pub fn checkbox(label: impl Into<String>, initial: bool) -> Self {
        Self {
            label: label.into(),
            state: FieldState::Bool {
                value: initial,
                initial,
            },
        }
    }

    /// A cycling choice field over `options`, starting at index `initial`.
    pub fn choice(
        label: impl Into<String>,
        options: Vec<String>,
        initial: usize,
    ) -> Self {
        Self {
            label: label.into(),
            state: FieldState::Choice {
                index: initial.min(options.len().saturating_sub(1)),
                initial,
                options,
            },
        }
    }

    /// A date field opening a calendar picker, pre-selecting `initial`.
    pub fn date(label: impl Into<String>, initial: Option<NaiveDate>) -> Self {
        Self {
            label: label.into(),
            state: FieldState::Date {
                value: initial,
                initial,
            },
        }
    }

    /// The field's label.
    pub fn label(&self) -> &str {
        &self.label
    }

    /// The current value of the field.
    pub fn value(&self) -> FieldValue {
        match &self.state {
            FieldState::Text { input, .. } => {
                FieldValue::Text(input.value().to_string())
            }
            FieldState::Multiline { area, .. } => {
                FieldValue::Multiline(area.text().to_string())
            }
            FieldState::Bool { value, .. } => FieldValue::Bool(*value),
            FieldState::Choice { index, options, .. } => {
                FieldValue::Choice(options[*index].clone())
            }
            FieldState::Date { value, .. } => FieldValue::Date(*value),
        }
    }

    /// Whether the value changed from its initial.
    pub fn is_dirty(&self) -> bool {
        match &self.state {
            FieldState::Text { input, initial } => input.value() != initial,
            FieldState::Multiline { area, initial } => area.text() != initial,
            FieldState::Bool { value, initial } => value != initial,
            FieldState::Choice { index, initial, .. } => index != initial,
            FieldState::Date { value, initial } => value != initial,
        }
    }

    fn height(&self) -> u16 {
        match self.state {
            FieldState::Multiline { .. } => MULTILINE_ROWS + 1,
            _ => 1,
        }
    }
}

/// How the form was closed.
pub enum FormOutcome {
    /// The user saved (`Ctrl+S`).
    Saved,
    /// The user cancelled (`Esc`).
    Cancelled,
    /// The global quit chord was pressed.
    Quit,
}

/// A modal form over a set of [`Field`]s.
pub struct Form {
    title: String,
    fields: Vec<Field>,
    focus: usize,
}

impl Form {
    /// A form titled `title` over `fields`, focused on the first field.
    pub fn new(title: impl Into<String>, fields: Vec<Field>) -> Self {
        Self {
            title: title.into(),
            fields,
            focus: 0,
        }
    }

    /// The fields, for reading values after [`FormOutcome::Saved`].
    pub fn fields(&self) -> &[Field] {
        &self.fields
    }

    /// Runs the form until the user saves, cancels or quits.
    ///
    /// # Errors
    ///
    /// Propagates terminal I/O errors.
    pub fn run(
        &mut self,
        tui: &mut Tui,
        skin: &Skin,
        render_bg: impl Fn(&mut Frame),
    ) -> io::Result<FormOutcome> {
        if self.fields.is_empty() {
            return Ok(FormOutcome::Cancelled);
        }
        loop {
            tui.draw(|frame| {
                render_bg(frame);
                overlay::dim(frame, overlay::SCRIM_FACTOR);
                self.render(frame, skin);
            })?;
            match tui.read_event()? {
                TuiEvent::Quit => return Ok(FormOutcome::Quit),
                TuiEvent::Resize => {}
                TuiEvent::Key(key) => {
                    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
                    match key.code {
                        KeyCode::Esc => return Ok(FormOutcome::Cancelled),
                        KeyCode::Char('s') if ctrl => {
                            return Ok(FormOutcome::Saved);
                        }
                        KeyCode::Tab => {
                            self.focus =
                                nav::cycle(self.focus, self.fields.len(), 1);
                        }
                        KeyCode::BackTab => {
                            self.focus =
                                nav::cycle(self.focus, self.fields.len(), -1);
                        }
                        _ => {
                            if let Some(outcome) =
                                self.handle_field_key(key, tui, skin)?
                            {
                                return Ok(outcome);
                            }
                        }
                    }
                }
            }
        }
    }

    fn handle_field_key(
        &mut self,
        key: crossterm::event::KeyEvent,
        tui: &mut Tui,
        skin: &Skin,
    ) -> io::Result<Option<FormOutcome>> {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);

        // Date opens a sub-modal that renders the form as its background, so it
        // is handled with whole-self borrows before the per-field borrow.
        if key.code == KeyCode::Enter
            && matches!(self.fields[self.focus].state, FieldState::Date { .. })
        {
            return self.open_date_picker(tui, skin);
        }
        if ctrl && key.code == KeyCode::Char('g') {
            self.open_editor_for_focus(tui)?;
            return Ok(None);
        }

        match &mut self.fields[self.focus].state {
            FieldState::Text { input, .. } => {
                input.handle_key(key);
            }
            FieldState::Multiline { area, .. } => {
                area.handle_key(key);
            }
            FieldState::Bool { value, .. } => {
                if matches!(key.code, KeyCode::Char(' ') | KeyCode::Enter) {
                    *value = !*value;
                }
            }
            FieldState::Choice { index, options, .. } => match key.code {
                KeyCode::Left => *index = nav::cycle(*index, options.len(), -1),
                KeyCode::Right => *index = nav::cycle(*index, options.len(), 1),
                _ => {}
            },
            FieldState::Date { .. } => {}
        }
        Ok(None)
    }

    fn open_date_picker(
        &mut self,
        tui: &mut Tui,
        skin: &Skin,
    ) -> io::Result<Option<FormOutcome>> {
        let current = match &self.fields[self.focus].state {
            FieldState::Date { value, .. } => *value,
            _ => None,
        };
        let signal = {
            let form: &Form = self;
            date_picker::date_picker(
                tui,
                skin,
                " Date ",
                current,
                true,
                |frame| form.render(frame, skin),
            )?
        };
        match signal {
            ModalSignal::Quit => return Ok(Some(FormOutcome::Quit)),
            ModalSignal::Cancelled => {}
            ModalSignal::Value(date) => {
                if let FieldState::Date { value, .. } =
                    &mut self.fields[self.focus].state
                {
                    *value = date;
                }
            }
        }
        Ok(None)
    }

    fn open_editor_for_focus(&mut self, tui: &mut Tui) -> io::Result<()> {
        let FieldState::Multiline { area, .. } = &self.fields[self.focus].state
        else {
            return Ok(());
        };
        let initial = area.text().to_string();
        let command = editor::resolve_editor();
        if let Some(text) = editor::edit_in_editor(tui, &command, &initial)?
            && let FieldState::Multiline { area, .. } =
                &mut self.fields[self.focus].state
        {
            area.set_text(text);
        }
        Ok(())
    }

    fn render(&self, frame: &mut Frame, skin: &Skin) {
        let palette = &skin.palette;
        let outer = frame.area();
        let body: u16 = self.fields.iter().map(Field::height).sum();
        let width = (outer.width * 2 / 3).clamp(40, outer.width);
        let height = (body + 3).min(outer.height);
        let area = centered_rect(width, height, outer);
        frame.render_widget(Clear, area);
        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(style::border(palette))
            .style(style::bg(palette.background))
            .title(Span::styled(
                format!("\u{2500} {} ", self.title),
                style::fg(palette.accent)
                    .add_modifier(ratatui::style::Modifier::BOLD),
            ));
        let inner = block.inner(area);
        frame.render_widget(block, area);

        let mut constraints: Vec<Constraint> = self
            .fields
            .iter()
            .map(|f| Constraint::Length(f.height()))
            .collect();
        constraints.push(Constraint::Min(1));
        let rects = Layout::default()
            .direction(Direction::Vertical)
            .constraints(constraints)
            .split(inner);

        for (index, field) in self.fields.iter().enumerate() {
            self.render_field(frame, rects[index], skin, index, field);
        }
        if let Some(footer_rect) = rects.last() {
            let hints = shortcut_hints::lines(
                &[("tab", "next"), ("ctrl+s", "save"), ("esc", "cancel")],
                palette.accent_dim,
                footer_rect.width as usize,
            );
            frame.render_widget(Paragraph::new(hints), *footer_rect);
        }
    }

    fn render_field(
        &self,
        frame: &mut Frame,
        rect: Rect,
        skin: &Skin,
        index: usize,
        field: &Field,
    ) {
        let palette = &skin.palette;
        let focused = index == self.focus;
        if focused {
            frame.render_widget(
                Block::default().style(style::bg(palette.selection)),
                rect,
            );
        }
        let marker = if field.is_dirty() { "*" } else { " " };
        let label = format!(
            "{marker} {:<width$}",
            field.label,
            width = LABEL_WIDTH as usize - 2
        );

        if let FieldState::Multiline { area, .. } = &field.state {
            let rows = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(1), Constraint::Min(1)])
                .split(rect);
            frame.render_widget(
                Paragraph::new(Line::from(Span::styled(
                    label,
                    style::secondary(palette),
                ))),
                rows[0],
            );
            area.render(frame, rows[1], skin, focused);
            return;
        }

        let columns = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Length(LABEL_WIDTH), Constraint::Min(1)])
            .split(rect);
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                label,
                style::secondary(palette),
            ))),
            columns[0],
        );
        let value_width = columns[1].width as usize;
        let value: Line = match &field.state {
            FieldState::Text { input, .. } => {
                input.render_line(palette, value_width, focused)
            }
            FieldState::Bool { value, .. } => {
                Line::from(if *value { "[x]" } else { "[ ]" })
            }
            FieldState::Choice { index, options, .. } => {
                Line::from(format!("\u{2039} {} \u{203a}", options[*index]))
            }
            FieldState::Date { value, .. } => Line::from(
                value.map_or_else(|| "\u{2014}".to_string(), |d| d.to_string()),
            ),
            FieldState::Multiline { .. } => Line::from(""),
        };
        frame.render_widget(Paragraph::new(value), columns[1]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dirty_tracks_changes() {
        let field = Field::checkbox("done", false);
        assert!(!field.is_dirty());
        let mut field = Field::choice("p", vec!["a".into(), "b".into()], 0);
        assert!(!field.is_dirty());
        if let FieldState::Choice { index, .. } = &mut field.state {
            *index = 1;
        }
        assert!(field.is_dirty());
        assert_eq!(field.value(), FieldValue::Choice("b".to_string()));
    }
}