shuvarie 0.1.1

Blazingly fast AI coding TUI for chivalrous people
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use ratatui::layout::Alignment;
use ratatui::prelude::*;
use ratatui::widgets::{Block, Padding, Paragraph};
use shuvarie_core::QuestionPrompt;
use termina::event::{KeyCode, KeyEvent, Modifiers};

use super::components::InputBuffer;
use super::theme;
use super::utils::text::wrap_text;

pub enum QuestionMessage {
    Up,
    Down,
    Prev,
    Next,
    Number(usize),
    Toggle,
    Escape,
    CustomInput(char),
    CustomPaste(String),
    CustomBackspace,
    CustomDelete,
    CustomLeft,
    CustomRight,
    CustomHome,
    CustomEnd,
    CustomLeftWord,
    CustomRightWord,
}

pub enum QuestionEffect {
    Answer {
        id: u64,
        answers: Option<Vec<Vec<String>>>,
    },
}

pub struct QuestionUI {
    pub id: u64,
    pub open: bool,
    pub questions: Vec<QuestionPrompt>,
    pub current: usize,
    pub at_confirm: bool,
    pub cursor: usize,
    pub picked: Vec<std::collections::BTreeSet<usize>>,
    pub custom_answers: Vec<Option<String>>,
    pub typing_custom: bool,
    pub typing_buffer: InputBuffer,
}

const MAX_VIEW_ROWS: usize = 12;

/// Left indent of the description rows under an option label.
const DESC_INDENT: usize = 5;

impl QuestionUI {
    pub fn new() -> Self {
        Self {
            id: 0,
            open: false,
            questions: Vec::new(),
            current: 0,
            at_confirm: false,
            cursor: 0,
            picked: Vec::new(),
            custom_answers: Vec::new(),
            typing_custom: false,
            typing_buffer: InputBuffer::new(),
        }
    }

    pub fn open(&mut self, id: u64, questions: Vec<QuestionPrompt>) {
        self.id = id;
        self.open = true;
        self.questions = questions;
        self.current = 0;
        self.at_confirm = false;
        self.cursor = 0;
        self.picked = self.questions.iter().map(|_| Default::default()).collect();
        self.custom_answers = self.questions.iter().map(|_| None).collect();
        self.typing_custom = false;
        self.typing_buffer.clear();
    }

    pub fn close(&mut self) {
        self.open = false;
        self.questions.clear();
    }

    fn row_count(&self, question_index: usize) -> usize {
        let q = &self.questions[question_index];
        let mut rows = q.options.len();
        if q.custom {
            rows += 1;
        }
        rows
    }

    fn has_confirm_view(&self) -> bool {
        self.questions.len() > 1
    }

    fn all_answered(&self) -> bool {
        self.questions
            .iter()
            .zip(&self.picked)
            .zip(&self.custom_answers)
            .all(|((_, picked), custom)| !picked.is_empty() || custom.is_some())
    }

    fn build_answers(&self) -> Vec<Vec<String>> {
        self.questions
            .iter()
            .enumerate()
            .map(|(i, q)| {
                if let Some(text) = &self.custom_answers[i] {
                    return vec![text.clone()];
                }
                self.picked[i]
                    .iter()
                    .filter_map(|&idx| q.options.get(idx).map(|o| o.label.clone()))
                    .collect()
            })
            .collect()
    }

    fn advance_after_answer(&mut self) -> Option<QuestionEffect> {
        if self.questions.len() == 1 && !self.questions[0].multiple {
            let id = self.id;
            let answers = self.build_answers();
            self.close();
            return Some(QuestionEffect::Answer {
                id,
                answers: Some(answers),
            });
        }
        None
    }

    pub fn map_event(&self, key: &KeyEvent) -> Option<QuestionMessage> {
        if !self.open {
            return None;
        }
        if self.typing_custom {
            if ctrl_mod(key) {
                return match key.code {
                    KeyCode::Char('b') => Some(QuestionMessage::CustomLeft),
                    KeyCode::Char('f') => Some(QuestionMessage::CustomRight),
                    KeyCode::Char('a') => Some(QuestionMessage::CustomHome),
                    KeyCode::Char('e') => Some(QuestionMessage::CustomEnd),
                    KeyCode::Char('d') => Some(QuestionMessage::CustomDelete),
                    KeyCode::Char('h') => Some(QuestionMessage::CustomBackspace),
                    _ => None,
                };
            }
            if key.modifiers.contains(Modifiers::ALT) {
                return match key.code {
                    KeyCode::Char('b') => Some(QuestionMessage::CustomLeftWord),
                    KeyCode::Char('f') => Some(QuestionMessage::CustomRightWord),
                    _ => None,
                };
            }
            return match key.code {
                KeyCode::Enter => Some(QuestionMessage::Toggle),
                KeyCode::Backspace => Some(QuestionMessage::CustomBackspace),
                KeyCode::Delete => Some(QuestionMessage::CustomDelete),
                KeyCode::Left => Some(QuestionMessage::CustomLeft),
                KeyCode::Right => Some(QuestionMessage::CustomRight),
                KeyCode::Home => Some(QuestionMessage::CustomHome),
                KeyCode::End => Some(QuestionMessage::CustomEnd),
                KeyCode::Escape => Some(QuestionMessage::Escape),
                KeyCode::Char(c) => Some(QuestionMessage::CustomInput(c)),
                _ => None,
            };
        }
        if ctrl_mod(key) {
            return match key.code {
                KeyCode::Char('n') => Some(QuestionMessage::Down),
                KeyCode::Char('p') => Some(QuestionMessage::Up),
                _ => None,
            };
        }
        match key.code {
            KeyCode::Up => Some(QuestionMessage::Up),
            KeyCode::Down => Some(QuestionMessage::Down),
            KeyCode::Left => Some(QuestionMessage::Prev),
            KeyCode::Right | KeyCode::Tab => Some(QuestionMessage::Next),
            KeyCode::Enter => Some(QuestionMessage::Toggle),
            KeyCode::Escape => Some(QuestionMessage::Escape),
            KeyCode::Char(c @ '1'..='9') => Some(QuestionMessage::Number(
                c.to_digit(10).unwrap() as usize - 1,
            )),
            _ => None,
        }
    }

    pub fn update(&mut self, msg: QuestionMessage) -> Option<QuestionEffect> {
        if !self.open {
            return None;
        }
        match msg {
            QuestionMessage::CustomInput(c) => {
                if self.typing_custom {
                    self.typing_buffer.push(c);
                }
                None
            }
            QuestionMessage::CustomPaste(text) => {
                if self.typing_custom {
                    self.typing_buffer
                        .insert_str(&super::components::flatten_newlines(&text));
                }
                None
            }
            QuestionMessage::CustomBackspace => self.edit_custom(InputBuffer::backspace),
            QuestionMessage::CustomDelete => self.edit_custom(InputBuffer::delete),
            QuestionMessage::CustomLeft => self.edit_custom(InputBuffer::left),
            QuestionMessage::CustomRight => self.edit_custom(InputBuffer::right),
            QuestionMessage::CustomHome => self.edit_custom(InputBuffer::home),
            QuestionMessage::CustomEnd => self.edit_custom(InputBuffer::end),
            QuestionMessage::CustomLeftWord => self.edit_custom(InputBuffer::left_word),
            QuestionMessage::CustomRightWord => self.edit_custom(InputBuffer::right_word),
            QuestionMessage::Escape => {
                if self.typing_custom {
                    self.typing_custom = false;
                    self.typing_buffer.clear();
                    return None;
                }
                let id = self.id;
                self.close();
                Some(QuestionEffect::Answer { id, answers: None })
            }
            QuestionMessage::Up => {
                if self.typing_custom || self.at_confirm {
                    return None;
                }
                self.cursor = self.cursor.saturating_sub(1);
                None
            }
            QuestionMessage::Down => {
                if self.typing_custom || self.at_confirm {
                    return None;
                }
                let rows = self.row_count(self.current);
                self.cursor = (self.cursor + 1).min(rows.saturating_sub(1));
                None
            }
            QuestionMessage::Prev => {
                if self.typing_custom {
                    return None;
                }
                if self.at_confirm {
                    self.at_confirm = false;
                    self.current = self.questions.len().saturating_sub(1);
                    self.cursor = 0;
                    return None;
                }
                if self.current > 0 {
                    self.current -= 1;
                    self.cursor = 0;
                }
                None
            }
            QuestionMessage::Next => self.toggle(true),
            QuestionMessage::Toggle => self.toggle(false),
            QuestionMessage::Number(n) => {
                if self.typing_custom || self.at_confirm {
                    return None;
                }
                let rows = self.row_count(self.current);
                if n < rows {
                    self.cursor = n;
                    return self.toggle(false);
                }
                None
            }
        }
    }

    fn edit_custom(&mut self, edit: fn(&mut InputBuffer)) -> Option<QuestionEffect> {
        if self.typing_custom {
            edit(&mut self.typing_buffer);
        }
        None
    }

    fn toggle(&mut self, from_nav: bool) -> Option<QuestionEffect> {
        if self.typing_custom {
            if from_nav {
                return None;
            }
            let text = self.typing_buffer.value.trim().to_string();
            if !text.is_empty() {
                self.custom_answers[self.current] = Some(text);
                // The picked marker moves to the custom answer: drop any
                // picked options so the saved text is the only marked row.
                self.picked[self.current].clear();
                self.typing_custom = false;
                self.typing_buffer.clear();
                return self.advance_after_answer();
            }
            return None;
        }
        if self.at_confirm {
            if from_nav {
                return None;
            }
            if self.all_answered() {
                let id = self.id;
                let answers = self.build_answers();
                self.close();
                return Some(QuestionEffect::Answer {
                    id,
                    answers: Some(answers),
                });
            }
            return None;
        }
        if from_nav {
            if self.current + 1 < self.questions.len() {
                self.current += 1;
                self.cursor = 0;
                return None;
            }
            if self.all_answered() {
                if self.has_confirm_view() {
                    self.at_confirm = true;
                } else {
                    let id = self.id;
                    let answers = self.build_answers();
                    self.close();
                    return Some(QuestionEffect::Answer {
                        id,
                        answers: Some(answers),
                    });
                }
            }
            return None;
        }
        let q = &self.questions[self.current];
        let rows = self.row_count(self.current);
        if self.cursor >= rows {
            return None;
        }
        let custom_idx = q.options.len();
        if self.cursor == custom_idx && q.custom {
            self.typing_custom = true;
            self.typing_buffer.clear();
            // Re-editing a saved custom answer: preload it so it can be
            // edited instead of retyped.
            let saved = self.custom_answers[self.current].clone();
            if let Some(text) = saved {
                self.typing_buffer.insert_str(&text);
            }
            return None;
        }
        if q.multiple {
            if !self.picked[self.current].remove(&self.cursor) {
                self.picked[self.current].insert(self.cursor);
            }
            if !self.picked[self.current].is_empty() {
                self.custom_answers[self.current] = None;
            }
            return None;
        }
        self.picked[self.current].clear();
        self.picked[self.current].insert(self.cursor);
        self.custom_answers[self.current] = None;
        self.advance_after_answer()
    }

    /// Height the question UI wants at the given content width: measured at
    /// the inner width the symmetric(2, 1) padded block paints at.
    pub fn desired_height(&self, width: usize) -> u16 {
        let inner = (width.max(10) as u16).saturating_sub(4);
        let lines = self.build_lines(inner, usize::MAX).len();
        (lines.min(MAX_VIEW_ROWS) as u16) + 2
    }

    pub fn view(&self, frame: &mut Frame<'_>, area: Rect) {
        if !self.open {
            return;
        }
        let block = Block::new()
            .bg(theme::surface())
            .padding(Padding::symmetric(2, 1));
        let inner = block.inner(area);
        frame.render_widget(block, area);
        if inner.width == 0 || inner.height == 0 {
            return;
        }

        let lines = self.build_lines(inner.width, inner.height as usize);
        frame.render_widget(Paragraph::new(lines).alignment(Alignment::Left), inner);
    }

    fn build_lines(&self, width: u16, max_rows: usize) -> Vec<Line<'static>> {
        let width = width.max(10);
        if self.at_confirm {
            return self.confirm_lines();
        }
        let Some(q) = self.questions.get(self.current) else {
            return Vec::new();
        };
        let progress = if self.questions.len() > 1 {
            format!("{} / {}  ", self.current + 1, self.questions.len())
        } else {
            String::new()
        };
        let mut lines = vec![
            Line::from(vec![
                Span::raw("").fg(theme::accent()),
                Span::raw(format!("{progress}{}", q.header))
                    .fg(theme::accent())
                    .bold(),
            ]),
            truncate_line(&q.question, width).style(Style::new().fg(theme::text()).italic()),
            Line::from(""),
        ];
        if self.typing_custom {
            lines.push(
                self.typing_buffer
                    .cursor_line(theme::text(), theme::accent()),
            );
            lines.push(theme::help_line(&[
                ("←/→", "move"),
                ("Enter", "save"),
                ("Esc", "cancel"),
            ]));
            return lines;
        }
        for (i, option) in q.options.iter().enumerate() {
            if lines.len() + 2 > max_rows {
                break;
            }
            let picked = self.picked[self.current].contains(&i);
            let marker = if q.multiple {
                if picked { "[x] " } else { "[ ] " }
            } else if picked {
                ""
            } else {
                "  "
            };
            let number = format!("{} ", i + 1);
            let mut spans = Vec::new();
            if self.cursor == i {
                spans.push(Span::raw("").fg(theme::accent()));
            } else {
                spans.push(Span::raw("  "));
            }
            spans.push(Span::raw(marker).fg(if picked {
                theme::success()
            } else {
                theme::text_muted()
            }));
            spans.push(Span::raw(number).fg(theme::text_muted()));
            spans.push(Span::raw(option.label.clone()).fg(theme::text()));
            lines.push(Line::from(spans));
            if !option.description.is_empty() {
                let desc_width = (width as usize).saturating_sub(DESC_INDENT);
                for row in wrap_text(&option.description, desc_width) {
                    if lines.len() >= max_rows {
                        break;
                    }
                    lines.push(Line::from(
                        Span::raw(format!("{}{row}", " ".repeat(DESC_INDENT)))
                            .fg(theme::text_muted()),
                    ));
                }
            }
        }
        if q.custom && lines.len() < max_rows {
            let saved = self.custom_answers[self.current].clone();
            let selected = self.cursor == q.options.len();
            let mut spans = Vec::new();
            if selected {
                spans.push(Span::raw("").fg(theme::accent()));
            } else {
                spans.push(Span::raw("  "));
            }
            spans.push(Span::raw("").fg(theme::text_muted()));
            match saved {
                Some(text) => {
                    // A saved custom answer is the picked row: the green dot
                    // (or checkbox) lives here instead of on any option.
                    let marker = if q.multiple { "[x] " } else { "" };
                    let used = 4 + marker.chars().count();
                    spans.push(Span::raw(marker).fg(theme::success()));
                    spans.push(truncate_span(
                        &text,
                        width.saturating_sub(used as u16),
                        theme::text(),
                    ));
                }
                None => {
                    spans.push(Span::raw("Type your own answer").fg(theme::text_dim()));
                }
            }
            lines.push(Line::from(spans));
        }
        if lines.len() < max_rows {
            let hints: &[(&str, &str)] = if self.questions.len() > 1 {
                &[
                    ("↑/↓", "choose"),
                    ("←/→", "question"),
                    ("Enter", "pick/toggle"),
                    ("Esc", "dismiss"),
                ]
            } else if q.multiple {
                &[
                    ("↑/↓", "choose"),
                    ("Enter", "toggle"),
                    ("", "done"),
                    ("Esc", "dismiss"),
                ]
            } else {
                &[("↑/↓", "choose"), ("Enter", "pick"), ("Esc", "dismiss")]
            };
            lines.push(theme::help_line(hints));
        }
        lines
    }

    fn confirm_lines(&self) -> Vec<Line<'static>> {
        let ready = self.all_answered();
        let mut lines = vec![Line::from(
            Span::raw("Confirm answers").fg(theme::accent()).bold(),
        )];
        for (i, q) in self.questions.iter().enumerate() {
            let answer = if let Some(text) = &self.custom_answers[i] {
                text.clone()
            } else {
                let labels: Vec<String> = self.picked[i]
                    .iter()
                    .filter_map(|&idx| q.options.get(idx).map(|o| o.label.clone()))
                    .collect();
                labels.join(", ")
            };
            let answered = !answer.is_empty();
            lines.push(Line::from(vec![
                Span::raw(if answered { "" } else { "" }).fg(if answered {
                    theme::success()
                } else {
                    theme::warning()
                }),
                Span::raw(format!("{}: ", q.header)).fg(theme::text()),
                Span::raw(if answered {
                    answer
                } else {
                    "unanswered".into()
                })
                .fg(if answered {
                    theme::text_dim()
                } else {
                    theme::warning()
                }),
            ]));
        }
        lines.push(Line::from(""));
        lines.push(Line::from(vec![
            Span::raw(if ready { "" } else { "  " }).fg(theme::accent()),
            Span::raw("Confirm").fg(if ready {
                theme::text()
            } else {
                theme::text_muted()
            }),
        ]));
        lines
    }
}

fn ctrl_mod(key: &KeyEvent) -> bool {
    key.modifiers.contains(Modifiers::CONTROL)
}

fn truncate_line(text: &str, width: u16) -> Line<'static> {
    Line::from(truncate_span(text, width.saturating_sub(1), theme::text()))
}

fn truncate_span(text: &str, width: u16, color: Color) -> Span<'static> {
    let limited: String = take_chars(text, width as usize);
    Span::raw(limited).fg(color)
}

fn take_chars(text: &str, max: usize) -> String {
    if max == 0 {
        return String::new();
    }
    text.chars().take(max).collect()
}

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

    fn ui(descriptions: &[&str]) -> QuestionUI {
        let mut ui = QuestionUI::new();
        ui.open(
            1,
            vec![QuestionPrompt {
                question: "Which one?".into(),
                header: "Pick".into(),
                options: descriptions
                    .iter()
                    .map(|d| QuestionOption {
                        label: "label".into(),
                        description: (*d).into(),
                    })
                    .collect(),
                multiple: false,
                custom: false,
            }],
        );
        ui
    }

    fn custom_ui(multiple: bool, count: usize) -> QuestionUI {
        let mut ui = QuestionUI::new();
        ui.open(
            1,
            (0..count)
                .map(|_| QuestionPrompt {
                    question: "Which one?".into(),
                    header: "Pick".into(),
                    options: vec![QuestionOption {
                        label: "label".into(),
                        description: String::new(),
                    }],
                    multiple,
                    custom: true,
                })
                .collect(),
        );
        ui
    }

    fn texts(lines: &[Line<'static>]) -> Vec<String> {
        lines
            .iter()
            .map(|l| {
                l.spans
                    .iter()
                    .map(|s| s.content.to_string())
                    .collect::<Vec<_>>()
                    .join("")
            })
            .collect()
    }

    #[test]
    fn long_description_wraps_under_the_option() {
        let ui = ui(&["first row here second row here"]);
        let lines = ui.build_lines(30, usize::MAX);
        let texts = texts(&lines);
        assert_eq!(texts[4], "     first row here second row");
        assert_eq!(texts[5], "     here");
        assert_eq!(lines[4].spans[0].style.fg, Some(theme::text_muted()));
    }

    #[test]
    fn short_description_stays_one_row() {
        let ui = ui(&["a short note"]);
        let lines = ui.build_lines(40, usize::MAX);
        let texts = texts(&lines);
        assert_eq!(texts[4], "     a short note");
        assert_eq!(lines.len(), 6);
    }

    #[test]
    fn wrapped_rows_respect_max_rows() {
        let ui = ui(&["aaaa bbbb cccc dddd eeee ffff gggg hhhh"]);
        let lines = ui.build_lines(30, 6);
        let texts = texts(&lines);
        assert_eq!(lines.len(), 6);
        assert_eq!(texts[4], "     aaaa bbbb cccc dddd eeee");
        assert_eq!(texts[5], "     ffff gggg hhhh");
    }

    #[test]
    fn desired_height_measures_at_the_painted_inner_width() {
        let desc = format!("{} zzzz", "w".repeat(20));
        let ui = ui(&[&desc]);
        let inner_lines = ui.build_lines(26, usize::MAX).len();
        assert_eq!(inner_lines, 7);
        assert_eq!(
            ui.desired_height(30),
            (inner_lines.min(MAX_VIEW_ROWS) + 2) as u16
        );
    }

    #[test]
    fn newline_in_description_starts_a_new_row() {
        let ui = ui(&["one\ntwo"]);
        let texts = texts(&ui.build_lines(40, usize::MAX));
        assert_eq!(texts[4], "     one");
        assert_eq!(texts[5], "     two");
    }

    fn type_custom(ui: &mut QuestionUI, text: &str) {
        ui.update(QuestionMessage::Down);
        ui.update(QuestionMessage::Toggle);
        assert!(ui.typing_custom);
        for c in text.chars() {
            ui.update(QuestionMessage::CustomInput(c));
        }
        ui.update(QuestionMessage::Toggle);
        assert!(!ui.typing_custom);
    }

    #[test]
    fn saved_custom_answer_takes_the_picked_marker() {
        let mut ui = custom_ui(true, 1);
        ui.update(QuestionMessage::Toggle); // pick option 0
        type_custom(&mut ui, "hi");
        assert!(ui.picked[0].is_empty());
        assert_eq!(ui.custom_answers[0].as_deref(), Some("hi"));
        let texts = texts(&ui.build_lines(40, usize::MAX));
        assert_eq!(texts[3], "  [ ] 1 label");
        assert_eq!(texts[4], "▶ ✎ [x] hi");
    }

    #[test]
    fn saved_custom_answer_shows_the_green_dot() {
        let mut ui = custom_ui(false, 2);
        type_custom(&mut ui, "hi");
        let texts = texts(&ui.build_lines(40, usize::MAX));
        assert_eq!(texts[3], "    1 label");
        assert_eq!(texts[4], "▶ ✎ ● hi");
    }

    #[test]
    fn reselecting_custom_row_preloads_the_saved_answer() {
        let mut ui = custom_ui(true, 1);
        type_custom(&mut ui, "draft answer");
        ui.update(QuestionMessage::Toggle); // select the custom row again
        assert!(ui.typing_custom);
        assert_eq!(ui.typing_buffer.value, "draft answer");
        ui.update(QuestionMessage::Escape); // cancel keeps the save
        assert!(!ui.typing_custom);
        assert_eq!(ui.typing_buffer.value, "");
        assert_eq!(ui.custom_answers[0].as_deref(), Some("draft answer"));
    }

    #[test]
    fn picking_an_option_after_custom_clears_the_custom_answer() {
        let mut ui = custom_ui(false, 2);
        type_custom(&mut ui, "hi");
        ui.update(QuestionMessage::Up); // back to option 0
        ui.update(QuestionMessage::Toggle);
        assert!(ui.custom_answers[0].is_none());
        assert!(ui.picked[0].contains(&0));
        assert_eq!(ui.build_answers()[0], vec!["label".to_string()]);
        let texts = texts(&ui.build_lines(40, usize::MAX));
        assert_eq!(texts[3], "▶ ● 1 label");
        assert_eq!(texts[4], "  ✎ Type your own answer");
    }
}