mirador 1.3.2

An opinionated personal dashboard for your terminal: world clocks, a calendar, weather, tasks, notes, a market watchlist, and live CPU and network graphs.
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//! A calculator, drawn as an adding machine's tape.
//!
//! The arithmetic lives in [`crate::calc`]; this is the instrument around it.
//!
//! # Why this panel exists
//!
//! It answers none of the four questions the dashboard was built for, which is
//! the same thing that is true of the pomodoro timer. Both are here because
//! they were asked for — this one by a reader, and endorsed. Recorded so the
//! next person does not conclude the filter was forgotten.
//!
//! There is an argument for it beyond that, and it is about *reaching* rather
//! than reading. mirador's whole premise is a tab left open all day; the thing
//! you actually reach for during that day is a quick sum, and the alternative
//! is `bc` or `python3 -c` in a shell you have to go and find. This is the
//! first panel that is purely an instrument, with no data source behind it at
//! all.
//!
//! # Why a tape and not a big number
//!
//! **The first version drew the answer in block numerals, and it was wrong.**
//! The reasoning behind it was "the clock and the pomodoro use them", which is
//! a resemblance rather than a reason. Those two show one continuously changing
//! value that you *glance* at, and the numerals exist so it reads from across
//! the room. A calculated answer is not glanced at — it is read once and
//! checked against the working that produced it. Different job, different
//! instrument.
//!
//! There was an arithmetic tell as well: block numerals cost six cells a digit,
//! so a twelve-digit result wants ninety-four columns. At any ordinary panel
//! width the numerals only ever appeared for answers small enough not to need
//! them.
//!
//! What replaced it is the adding machine's tape, and that is not nostalgia.
//! The reason accountants still buy printing calculators is *audit*: the tape
//! is what lets you check the entry you made three lines ago. It also belongs
//! to the design thesis, which is a watch station — chronometer, weather glass,
//! watch log. A tape sits on that bench; a seven-segment display does not.
//!
//! Built on [`crate::grid`] rather than hand-composed, which is what every
//! other list panel here does and what makes it look like part of the same
//! instrument. It also means "never show a truncated number" is enforced by the
//! shared column machinery rather than by hand.
//!
//! # The input problem, and why `captures_input` stays false
//!
//! A calculator needs the digits, and `1`–`9` are the shell's jump-to-panel
//! keys. The obvious fix is [`Panel::captures_input`], and it is a trap: that
//! is an *absolute* veto (invariant 2), meant for transient modal states like
//! typing a task title. A panel that captured permanently would kill `Tab`,
//! `q` and `?` for as long as it held focus — a room with no door.
//!
//! It is not needed. `App::dispatch_key` offers every key to the focused panel
//! *first* and consults the global table only if the panel returns `Ignored`.
//! So this panel consumes what it needs and ignores the rest, and every global
//! key except the digits keeps working.
//!
//! The price, stated plainly because it is a real one: **while this panel is
//! focused, `1`–`9` type digits instead of jumping to panels.** `Tab` still
//! cycles, which is the way out.

use ratatui::Frame;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;

use crate::calc::{self, CalcError};
use crate::config::CalculatorConfig;
use crate::frame::{Binding, FRAME_HEIGHT, FRAME_WIDTH};
use crate::grid::{Column, Grid, display_width};
use crate::panel::{KeyOutcome, Panel, RenderContext};

const BINDINGS: &[Binding] = &[
    Binding::primary("0-9 + - * /", "type"),
    Binding::primary("Enter", "keep"),
    Binding::primary("c", "clear"),
    Binding::primary("y", "copy"),
    Binding::extra("( )", "group"),
    Binding::extra("x", "multiply"),
    Binding::extra("C", "clear the tape too"),
    Binding::extra("Backspace", "rub out"),
    Binding::extra("↑ / ↓", "scroll the tape"),
];

/// Widest the result column is drawn.
///
/// Twelve significant digits plus a sign and a point is the most `calc` can
/// produce in decimal; past that it moves to scientific notation, which is
/// shorter. So this is a ceiling derived from the formatter rather than a
/// guess at what looks right.
const RESULT_WIDTH: u16 = 14;

/// The narrowest grid at which the working is still worth a column.
///
/// Below this the tape shows answers alone, which is the honest degradation:
/// an answer with no visible sum is still an answer, where a sum with no answer
/// is nothing at all.
const WORKING_MIN: u16 = RESULT_WIDTH + crate::grid::GUTTER + 8;

pub(crate) const COLUMNS: &[Column] = &[
    Column::flex("working", 1).drops_below(WORKING_MIN),
    Column::fixed("result", RESULT_WIDTH).right(),
];

/// Entries kept on the tape.
///
/// A bound rather than a target — the tape shows what fits, and this only stops
/// a long session growing without limit. Nothing reads past what is drawn.
const MAX_TAPE: usize = 200;

/// Interior width past which the panel gains nothing.
///
/// Past this, extra cells only widen the gap between a sum and its answer, and
/// taking room the task list could use would be worse. See invariant 15.
const USEFUL_WIDTH: u16 = 46;

/// Rows the panel always spends: the column header, the rule, the live entry.
const CHROME_ROWS: u16 = 3;

/// The live-entry marker, matching the task and notes lists.
const MARKER: &str = "\u{25b8} ";

/// One finished calculation.
#[derive(Debug, Clone, PartialEq)]
struct Entry {
    expression: String,
    result: f64,
}

pub struct CalculatorPanel {
    #[allow(dead_code)]
    config: CalculatorConfig,
    /// What is being typed.
    typing: String,
    /// What `typing` currently works out to, recomputed on every keystroke.
    ///
    /// Cached rather than evaluated at draw time: `render` runs every frame and
    /// nothing guards it, so parsing there would put the parser on the frame
    /// budget for a value that changes only when a key is pressed. Same
    /// reasoning as the agenda's cache.
    preview: Result<f64, CalcError>,
    /// Finished calculations, oldest first — the order a tape feeds.
    tape: Vec<Entry>,
    /// How far back through the tape the view is scrolled, in rows.
    scrolled: usize,
    /// Tape rows drawn last frame, so scrolling cannot run past what is there.
    drawn: usize,
    /// What the last `y` did. Cleared by the next keystroke.
    ///
    /// OSC 52 is write-only, so this says what was *sent* rather than claiming
    /// the clipboard changed — the same honesty the news panel's copy uses.
    action: Option<String>,
}

impl std::fmt::Debug for CalculatorPanel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CalculatorPanel")
            .field("typing", &self.typing)
            .field("tape", &self.tape.len())
            .finish_non_exhaustive()
    }
}

impl CalculatorPanel {
    pub fn new(config: CalculatorConfig) -> Self {
        Self {
            config,
            typing: String::new(),
            preview: Err(CalcError::Empty),
            tape: Vec::new(),
            scrolled: 0,
            drawn: 0,
            action: None,
        }
    }

    /// The answer on the tape's last line, which is what `y` copies.
    fn last_result(&self) -> Option<f64> {
        self.tape.last().map(|entry| entry.result)
    }

    /// Re-derive the preview. Called wherever `typing` changes, so the two
    /// cannot drift.
    fn reparse(&mut self) {
        self.preview = calc::evaluate(&self.typing);
    }

    /// Add a character to the expression being typed.
    ///
    /// An operator typed straight after an answer continues from it — `57`,
    /// then `+10`, reads as `57 + 10`. A digit starts fresh instead. That is
    /// how a desk calculator chains, and it is what removes the need for a
    /// memory key: "and now add the tax" costs no extra concept.
    fn push(&mut self, c: char) {
        if self.typing.is_empty()
            && let Some(value) = self.last_result()
            && matches!(c, '+' | '-' | '*' | '/' | 'x' | '\u{00D7}' | '\u{00F7}')
        {
            self.typing = calc::format_result(value);
        }
        // Past this the expression cannot be evaluated anyway.
        if self.typing.chars().count() < calc::MAX_LEN {
            self.typing.push(c);
        }
        self.reparse();
    }

    /// Commit the current entry to the tape, if it works out.
    fn keep(&mut self) {
        if let Ok(value) = self.preview {
            self.tape.push(Entry {
                expression: self.typing.trim().to_string(),
                result: value,
            });
            // Oldest goes first: the part of a tape you have already scrolled
            // past is the part you are least likely to want back.
            if self.tape.len() > MAX_TAPE {
                self.tape.remove(0);
            }
            self.typing.clear();
            self.reparse();
            // Back to the foot of the tape, where the new entry is.
            self.scrolled = 0;
        }
    }

    /// The rows of the tape currently in view, oldest first.
    ///
    /// Only what fits is produced. A panel may allocate in proportion to what
    /// is on screen and must not allocate in proportion to how much it holds.
    fn visible(&self, rows: usize) -> &[Entry] {
        if rows == 0 || self.tape.is_empty() {
            return &[];
        }
        let end = self.tape.len().saturating_sub(self.scrolled).max(1);
        let start = end.saturating_sub(rows);
        &self.tape[start..end]
    }

    /// How far back the view may be scrolled, given what fits.
    fn max_scroll(&self) -> usize {
        self.tape.len().saturating_sub(self.drawn)
    }
}

/// Results padded so their decimal points sit in the same column.
///
/// Right-alignment alone lines up the last *character*, which puts the `5` of
/// `7.5` where the units of `384` are and makes a column of answers unreadable
/// at a glance. Accounting tables have always aligned on the point, and this is
/// the detail that makes a tape scan.
///
/// Padding goes on the right and the grid then right-aligns the whole cell, so
/// the points land together whatever the column width.
fn align_points(results: &[String], column: usize) -> Vec<String> {
    let fraction = |text: &str| text.rfind('.').map_or(0, |at| text.len() - at - 1);
    let padding = |text: &str, widest: usize| {
        let pad = widest.saturating_sub(fraction(text));
        // A whole number needs a cell for the point it has not got, or it sits
        // one place right of everything that has a fraction.
        pad + usize::from(pad > 0 && !text.contains('.'))
    };

    // Scientific notation stays out of the reckoning: the point in `1.2e11`
    // means something else, and padding to it would shove every ordinary answer
    // sideways to line up with a case that is already exceptional.
    let ordinary = || results.iter().filter(|text| !text.contains('e'));
    let mut widest = ordinary().map(|text| fraction(text)).max().unwrap_or(0);

    // **Padding counts against the column.** The numbers arrive here already
    // fitted, so adding cells to line up the points can push one back out —
    // and the grid then ellipsises it, which undoes the whole reason
    // `fit_result` exists. A ten-cell answer beside a two-place fraction came
    // out as `123456789…`: the number rescued from truncation, truncated by
    // the thing meant to make it readable.
    //
    // So the alignment gives way rather than the number. Losing a place of
    // alignment costs neatness; losing a digit costs the answer.
    while widest > 0 && ordinary().any(|text| text.chars().count() + padding(text, widest) > column)
    {
        widest -= 1;
    }

    results
        .iter()
        .map(|text| {
            if text.contains('e') {
                return text.clone();
            }
            format!("{text}{}", " ".repeat(padding(text, widest)))
        })
        .collect()
}

impl Panel for CalculatorPanel {
    fn title(&self) -> String {
        "Calculator".to_string()
    }

    /// Deliberately `None`.
    ///
    /// A count of tape entries is a badge, and a badge that only goes up is the
    /// unread counter this dashboard turned down.
    fn counter(&self) -> Option<String> {
        None
    }

    fn bindings(&self) -> &'static [Binding] {
        BINDINGS
    }

    fn max_width(&self) -> Option<u16> {
        Some(USEFUL_WIDTH + FRAME_WIDTH)
    }

    /// Deliberately `None`: the tape is content, and rows are what it is made
    /// of. Same reasoning as the watch log.
    fn max_height(&self) -> Option<u16> {
        None
    }

    /// Nothing here changes on its own.
    fn tick(&mut self) -> bool {
        false
    }

    /// Never. See the module header: this panel gets the keys it needs by
    /// consuming them, not by vetoing the shell.
    fn captures_input(&self) -> bool {
        false
    }

    fn handle_key(&mut self, key: KeyEvent) -> KeyOutcome {
        // Ctrl and Alt belong to the shell — Ctrl+arrows resize, Ctrl+C quits.
        if key.modifiers.contains(KeyModifiers::CONTROL)
            || key.modifiers.contains(KeyModifiers::ALT)
        {
            return KeyOutcome::Ignored;
        }

        // Any key retires the copy notice; it has been read or it has not.
        let had_action = self.action.take().is_some();

        match key.code {
            KeyCode::Char(
                c @ ('0'..='9'
                | '.'
                | '+'
                | '-'
                | '*'
                | '/'
                | '('
                | ')'
                | 'x'
                | '\u{00D7}'
                | '\u{00F7}'),
            ) => self.push(c),
            KeyCode::Char('=') | KeyCode::Enter => self.keep(),
            KeyCode::Backspace => {
                self.typing.pop();
                self.reparse();
            }
            // `c` clears the entry and `C` the tape with it — the CE and AC of
            // every desk calculator, a convention worth inheriting rather than
            // inventing around. The first version had only `Esc`, listed as a
            // secondary binding, which is why nobody could find it.
            KeyCode::Char('c') => {
                self.typing.clear();
                self.reparse();
            }
            KeyCode::Char('C') => {
                self.typing.clear();
                self.tape.clear();
                self.scrolled = 0;
                self.reparse();
            }
            KeyCode::Esc => {
                // Consumed only while there is something to clear, so Esc on an
                // empty calculator falls through to the shell as it does
                // everywhere else.
                if self.typing.is_empty() && !had_action {
                    return KeyOutcome::Ignored;
                }
                self.typing.clear();
                self.reparse();
            }
            KeyCode::Char('y') => {
                let Some(value) = self.last_result() else {
                    return KeyOutcome::Consumed;
                };
                let text = calc::format_result(value);
                self.action = Some(match crate::clipboard::copy(&text) {
                    // OSC 52 is write-only: the terminal never answers, so this
                    // says what was sent rather than that anything was copied.
                    Ok(()) => format!("sent {text}"),
                    Err(e) => format!("clipboard failed: {e}"),
                });
            }
            KeyCode::Up => self.scrolled = (self.scrolled + 1).min(self.max_scroll()),
            KeyCode::Down => self.scrolled = self.scrolled.saturating_sub(1),
            _ => {
                // A key this panel does not want goes to the shell — unless it
                // only served to retire the copy notice, which is a visible
                // change and so counts as having been used.
                if had_action {
                    return KeyOutcome::Consumed;
                }
                return KeyOutcome::Ignored;
            }
        }
        KeyOutcome::Consumed
    }

    fn handle_mouse(&mut self, event: MouseEvent, _area: Rect) -> KeyOutcome {
        match event.kind {
            MouseEventKind::ScrollUp => self.scrolled = (self.scrolled + 1).min(self.max_scroll()),
            MouseEventKind::ScrollDown => self.scrolled = self.scrolled.saturating_sub(1),
            _ => return KeyOutcome::Ignored,
        }
        KeyOutcome::Consumed
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, ctx: RenderContext<'_>) {
        if area.width == 0 || area.height == 0 {
            self.drawn = 0;
            return;
        }
        let theme = ctx.theme;

        // The marker's width comes off the grid and goes back as an indent, so
        // the header lines up with the rows under it.
        let marker = u16::try_from(display_width(MARKER)).unwrap_or(2);
        let grid = Grid::new(COLUMNS, area.width.saturating_sub(marker));
        let indent = " ".repeat(usize::from(marker));

        let bottom = area.y + area.height;
        let mut cursor = area.y;

        // The header, in the utility face, exactly as the other list panels
        // label their columns. It is also what makes an untouched calculator
        // look ready rather than broken.
        if cursor < bottom {
            let mut spans = vec![Span::raw(indent.clone())];
            spans.extend(grid.header(theme).spans);
            frame.render_widget(
                Paragraph::new(Line::from(spans)),
                Rect::new(area.x, cursor, area.width, 1),
            );
            cursor += 1;
        }

        let room = usize::from(
            bottom
                .saturating_sub(cursor)
                .saturating_sub(CHROME_ROWS - 1),
        );
        // Cloned rather than borrowed: `self.drawn` has to be recorded before
        // the rows are drawn, and holding a slice of `self.tape` across that
        // would borrow the whole panel. The clone is of what is on screen, not
        // of the tape — the rule is that a panel may allocate in proportion to
        // what it displays.
        let entries: Vec<Entry> = self.visible(room).to_vec();
        self.drawn = entries.len();

        // Fitted to the column *before* the grid sees it. The grid ellipsises a
        // cell that will not fit, which is right for a name and wrong for a
        // number — a narrow panel drew `123456789…` for 123456789000, which is
        // the one thing this panel must never do. `fit_result` moves to
        // scientific notation instead, and the grid then has nothing to cut.
        let column = usize::from(grid.column_width("result"));
        // The live answer is aligned *with* the tape, not separately. Aligning
        // the two independently left the result being typed one cell right of
        // the column above it, which is exactly the misalignment the padding
        // exists to remove — and the most visible one, since those two rows sit
        // either side of the rule.
        let mut figures: Vec<String> = entries
            .iter()
            .map(|entry| calc::fit_result(entry.result, column))
            .collect();
        let live = self
            .preview
            .as_ref()
            .ok()
            .map(|v| calc::fit_result(*v, column));
        if let Some(value) = &live {
            figures.push(value.clone());
        }
        let mut aligned = align_points(&figures, column);
        let live = live.map(|_| aligned.pop().unwrap_or_default());

        // Attention runs *downwards*, to the line being typed. The tape recedes
        // to muted, the entry just kept stays at full body weight, and the live
        // row below is brighter than either. The first version had this
        // backwards and put the faintest thing on screen where the cursor was.
        let last = entries.len().saturating_sub(1);
        for (index, entry) in entries.iter().enumerate() {
            if cursor >= bottom {
                break;
            }
            let style = Style::default().fg(if index == last {
                theme.text
            } else {
                theme.muted
            });
            let mut spans = vec![Span::raw(indent.clone())];
            spans.extend(
                grid.row(&[
                    Span::styled(entry.expression.clone(), style),
                    Span::styled(aligned[index].clone(), style),
                ])
                .spans,
            );
            frame.render_widget(
                Paragraph::new(Line::from(spans)),
                Rect::new(area.x, cursor, area.width, 1),
            );
            cursor += 1;
        }

        // The live entry sits at the foot of the panel, where a tape feeds
        // from, rather than wandering up and down as the tape fills.
        cursor = bottom.saturating_sub(2).max(cursor);

        // The rule between what is kept and what is being typed. The notes
        // panel uses the same device between its list and its detail.
        if cursor < bottom {
            frame.render_widget(
                Paragraph::new(Span::styled(
                    "\u{2500}".repeat(usize::from(area.width)),
                    Style::default().fg(theme.rule),
                )),
                Rect::new(area.x, cursor, area.width, 1),
            );
            cursor += 1;
        }

        if cursor < bottom {
            self.draw_entry(frame, area, cursor, &grid, theme, live.as_deref());
        }
    }
}

impl CalculatorPanel {
    /// The live entry: the brightest row in the panel, because it is the one
    /// the cursor is on.
    fn draw_entry(
        &self,
        frame: &mut Frame,
        area: Rect,
        row: u16,
        grid: &Grid,
        theme: &crate::theme::Theme,
        live: Option<&str>,
    ) {
        // A half-typed sum is the state this panel spends most of its life in,
        // so those say nothing at all rather than complaining. Only a sum that
        // cannot ever work gets a word.
        let complaint = match &self.preview {
            Ok(_) | Err(CalcError::Empty | CalcError::Incomplete) => None,
            Err(error) => Some(error.message()),
        };

        let column = usize::from(grid.column_width("result"));
        let (working, result, style) = if let Some(notice) = &self.action {
            (
                notice.clone(),
                String::new(),
                Style::default().fg(theme.label),
            )
        } else if let Some(message) = complaint {
            (
                format!("{}\u{258f}", self.typing),
                crate::grid::truncate(&message, column),
                Style::default().fg(theme.error),
            )
        } else {
            // Already fitted and aligned with the tape by the caller.
            let answer = live.unwrap_or_default().to_string();
            (
                format!("{}\u{258f}", self.typing),
                answer,
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        };

        let mut spans = vec![Span::styled(MARKER, Style::default().fg(theme.accent))];
        spans.extend(
            grid.row(&[
                Span::styled(working, Style::default().fg(theme.text)),
                Span::styled(result, style),
            ])
            .spans,
        );
        frame.render_widget(
            Paragraph::new(Line::from(spans)),
            Rect::new(area.x, row, area.width, 1),
        );
    }
}

/// Rows the frame costs, named so `max_height`'s reasoning is checkable.
#[allow(dead_code)]
const _: u16 = FRAME_HEIGHT;

// Exact comparison is the point: these are answers a calculator must get
// exactly right, not measurements to be compared within a tolerance.
#[allow(clippy::float_cmp)]
#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    fn new_panel() -> CalculatorPanel {
        CalculatorPanel::new(CalculatorConfig::default())
    }

    fn press(panel: &mut CalculatorPanel, c: char) -> KeyOutcome {
        panel.handle_key(KeyEvent::from(KeyCode::Char(c)))
    }

    fn type_in(panel: &mut CalculatorPanel, text: &str) {
        for c in text.chars() {
            press(panel, c);
        }
    }

    fn enter(panel: &mut CalculatorPanel) {
        panel.handle_key(KeyEvent::from(KeyCode::Enter));
    }

    fn buffer_of(panel: &mut CalculatorPanel, width: u16, height: u16) -> ratatui::buffer::Buffer {
        let config = crate::config::Config::default();
        let gradients = config.theme.gradients();
        let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
        terminal
            .draw(|frame| {
                panel.render(
                    frame,
                    frame.area(),
                    RenderContext {
                        theme: &config.theme,
                        gradients: &gradients,
                        focused: true,
                        watch: &crate::watch::WatchLog::default(),
                    },
                );
            })
            .unwrap();
        terminal.backend().buffer().clone()
    }

    fn draw(panel: &mut CalculatorPanel, width: u16, height: u16) -> String {
        let buffer = buffer_of(panel, width, height);
        (0..height)
            .map(|y| {
                (0..width)
                    .map(|x| buffer[(x, y)].symbol().to_string())
                    .collect::<String>()
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// The whole input design in one test.
    ///
    /// Digits must reach the calculator, and everything the shell owns must
    /// still reach the shell. Get the first half wrong and the panel is
    /// useless; get the second half wrong and it is a room with no door.
    #[test]
    fn digits_are_taken_and_the_shell_keys_are_left_alone() {
        let mut panel = new_panel();
        for c in "0123456789.+-*/()x".chars() {
            assert_eq!(
                press(&mut panel, c),
                KeyOutcome::Consumed,
                "{c:?} has to reach the calculator"
            );
        }

        let mut panel = new_panel();
        for code in [
            KeyCode::Tab,
            KeyCode::BackTab,
            KeyCode::Char('q'),
            KeyCode::Char('?'),
            KeyCode::Char('w'),
            KeyCode::Char('t'),
            KeyCode::Char('m'),
        ] {
            assert_eq!(
                panel.handle_key(KeyEvent::from(code)),
                KeyOutcome::Ignored,
                "{code:?} belongs to the shell; consuming it locks the user in"
            );
        }
        assert!(!panel.captures_input());
    }

    #[test]
    fn the_answer_forms_as_you_type_and_enter_keeps_it() {
        let mut panel = new_panel();
        type_in(&mut panel, "2+3*4");
        assert_eq!(panel.preview, Ok(14.0), "the answer forms before Enter");
        assert!(panel.tape.is_empty(), "but nothing is kept until Enter");

        enter(&mut panel);
        assert_eq!(panel.tape.len(), 1);
        assert_eq!(panel.tape[0].expression, "2+3*4");
        assert!(panel.typing.is_empty());
    }

    /// The tape feeds like a tape: newest at the bottom, beside the cursor.
    #[test]
    fn the_newest_entry_is_at_the_foot_of_the_tape() {
        let mut panel = new_panel();
        for sum in ["1+1", "2+2", "3+3"] {
            type_in(&mut panel, sum);
            enter(&mut panel);
        }
        assert_eq!(panel.last_result(), Some(6.0));

        let screen = draw(&mut panel, 36, 9);
        let rows: Vec<&str> = screen.lines().collect();
        let at = |needle: &str| rows.iter().position(|r| r.contains(needle)).unwrap();
        assert!(
            at("1+1") < at("2+2") && at("2+2") < at("3+3"),
            "the tape must read oldest to newest downwards:\n{screen}"
        );
    }

    /// The correction that came out of looking at it on screen.
    ///
    /// The live row is where the cursor is, so it cannot be the faintest thing
    /// in the panel. Only that row carries the accent; the tape must not.
    #[test]
    fn only_the_live_row_is_accented() {
        let mut panel = new_panel();
        type_in(&mut panel, "1+1");
        enter(&mut panel);
        type_in(&mut panel, "9*9");

        let config = crate::config::Config::default();
        let buffer = buffer_of(&mut panel, 36, 9);

        let mut accented = Vec::new();
        for y in 0..9u16 {
            if (0..36u16).any(|x| buffer[(x, y)].style().fg == Some(config.theme.accent)) {
                accented.push(y);
            }
        }
        assert_eq!(
            accented.len(),
            1,
            "exactly one row should be accented, got {accented:?}"
        );
        assert_eq!(
            accented[0], 8,
            "and it should be the live entry at the foot of the panel"
        );
    }

    #[test]
    fn c_clears_the_entry_and_shift_c_clears_the_tape() {
        let mut panel = new_panel();
        type_in(&mut panel, "1+1");
        enter(&mut panel);
        type_in(&mut panel, "99");

        press(&mut panel, 'c');
        assert!(panel.typing.is_empty(), "c clears what is being typed");
        assert_eq!(panel.tape.len(), 1, "and leaves the tape alone");

        press(&mut panel, 'C');
        assert!(panel.tape.is_empty(), "C clears the tape as well");
    }

    #[test]
    fn an_operator_after_an_answer_carries_it_forward() {
        let mut panel = new_panel();
        type_in(&mut panel, "50+7");
        enter(&mut panel);
        press(&mut panel, '+');
        assert_eq!(panel.typing, "57+");
        type_in(&mut panel, "10");
        enter(&mut panel);
        assert_eq!(panel.last_result(), Some(67.0));
    }

    #[test]
    fn a_digit_after_an_answer_starts_over() {
        let mut panel = new_panel();
        type_in(&mut panel, "50+7");
        enter(&mut panel);
        press(&mut panel, '9');
        assert_eq!(panel.typing, "9");
    }

    #[test]
    fn a_sum_that_cannot_work_says_why_on_screen() {
        let mut panel = new_panel();
        type_in(&mut panel, "1/0");
        let screen = draw(&mut panel, 44, 9);
        assert!(
            screen.contains("\u{00f7} by zero"),
            "the reason has to be on screen, not just in the state:\n{screen}"
        );
        enter(&mut panel);
        assert!(panel.tape.is_empty(), "nothing that failed is kept");
        assert_eq!(panel.typing, "1/0", "and it stays there to be corrected");
    }

    /// Half-typed is the normal state, so it must not look like a failure.
    #[test]
    fn a_half_typed_sum_is_not_scolded() {
        let mut panel = new_panel();
        type_in(&mut panel, "2+");
        let screen = draw(&mut panel, 44, 9);
        assert!(
            !screen.contains("unfinished"),
            "an expression mid-typing must not be complained about:\n{screen}"
        );
    }

    /// Decimal points line up, which is what makes a column of answers scan.
    #[test]
    fn results_are_aligned_on_the_decimal_point() {
        let aligned = align_points(
            &[
                "384".to_string(),
                "7.5".to_string(),
                "0.25".to_string(),
                "60".to_string(),
            ],
            14,
        );
        for (text, original) in aligned.iter().zip(["384", "7.5", "0.25", "60"]) {
            assert!(
                text.starts_with(original),
                "{text:?} should keep {original:?}"
            );
        }
        // The property that matters is visual, so it is checked visually:
        // right-align the padded cells the way the grid will, then every units
        // digit must land in the same column. An earlier version of this test
        // compared fraction widths instead, and failed on `384` — which has no
        // decimal point at all, and was nonetheless aligned correctly.
        let width = aligned.iter().map(String::len).max().unwrap();
        let placed: Vec<String> = aligned.iter().map(|t| format!("{t:>width$}")).collect();
        let units = |row: &str| {
            row.rfind('.')
                .unwrap_or_else(|| row.trim_end().len())
                .saturating_sub(1)
        };
        let columns: Vec<usize> = placed.iter().map(|r| units(r)).collect();
        assert!(
            columns.windows(2).all(|w| w[0] == w[1]),
            "units digits land in different columns:\n{}",
            placed.join("\n")
        );
    }

    /// Alignment padding must not push a result back out of its column.
    ///
    /// `fit_result` fits the number, then `align_points` adds cells to line the
    /// decimal points up — and those cells count. A ten-cell answer beside a
    /// two-place fraction was padded to thirteen, which the grid then cut back
    /// to `123456789…`: the number rescued from truncation, truncated by the
    /// thing that was meant to make it readable.
    #[test]
    fn alignment_padding_never_pushes_a_result_out_of_its_column() {
        let mut panel = new_panel();
        type_in(&mut panel, "0.25*1");
        enter(&mut panel);
        type_in(&mut panel, "1234567890*1");
        enter(&mut panel);

        // The *result* is what may never be ellipsised. The working is prose
        // and is allowed to be cut — an earlier version of this assertion did
        // not distinguish them and failed on `1234567890*1` in the left column,
        // which was behaving correctly.
        for width in 12u16..=40 {
            for row in draw(&mut panel, width, 8).lines() {
                assert!(
                    !row.trim_end().ends_with('\u{2026}'),
                    "the result column was ellipsised at width {width}: {row:?}"
                );
            }
        }
    }

    #[test]
    fn scientific_results_are_left_out_of_the_alignment() {
        let aligned = align_points(&["1.2346e11".to_string(), "7.5".to_string()], 14);
        assert_eq!(
            aligned[0], "1.2346e11",
            "an exponent's point is not a place"
        );
    }

    /// The rule this panel is held to more strictly than any other.
    #[test]
    fn no_row_is_ever_wider_than_the_panel() {
        let mut panel = new_panel();
        for sum in ["123456789 * 1000", "1/3", "2+2", "0.1+0.2", "999999*999999"] {
            type_in(&mut panel, sum);
            enter(&mut panel);
        }
        type_in(&mut panel, "42*42");
        for width in 1u16..=50 {
            for row in draw(&mut panel, width, 10).lines() {
                assert!(
                    display_width(row) <= usize::from(width),
                    "a {}-cell row in a {width}-cell panel: {row:?}",
                    display_width(row)
                );
            }
        }
    }

    #[test]
    fn a_result_too_wide_is_never_shown_truncated() {
        let mut panel = new_panel();
        type_in(&mut panel, "123456789*1000");
        enter(&mut panel);
        for row in draw(&mut panel, 12, 8).lines() {
            let stripped: String = row.chars().filter(|c| !c.is_whitespace()).collect();
            assert!(
                !stripped.contains("123456789") || stripped.contains('e'),
                "a prefix of the answer reached the screen: {row:?}"
            );
        }
    }

    #[test]
    fn the_tape_is_bounded() {
        let mut panel = new_panel();
        for n in 0..MAX_TAPE + 50 {
            panel.typing = format!("{n}+1");
            panel.reparse();
            panel.keep();
        }
        assert_eq!(panel.tape.len(), MAX_TAPE);
        assert_eq!(
            panel.tape.last().unwrap().expression,
            format!("{}+1", MAX_TAPE + 49),
            "the newest survives; the oldest is dropped"
        );
    }

    #[test]
    fn only_the_rows_that_fit_are_built() {
        let mut panel = new_panel();
        for n in 0..MAX_TAPE {
            panel.typing = format!("{n}+1");
            panel.reparse();
            panel.keep();
        }
        draw(&mut panel, 36, 10);
        assert!(
            panel.drawn <= 10,
            "{} rows for a ten-row panel",
            panel.drawn
        );
        assert!(panel.drawn > 0);
    }

    /// Scrolling cannot run off either end of the tape.
    #[test]
    fn scrolling_is_bounded_at_both_ends() {
        let mut panel = new_panel();
        for n in 0..40 {
            panel.typing = format!("{n}+1");
            panel.reparse();
            panel.keep();
        }
        draw(&mut panel, 36, 12);
        for _ in 0..200 {
            panel.handle_key(KeyEvent::from(KeyCode::Up));
        }
        assert_eq!(panel.scrolled, panel.max_scroll());
        draw(&mut panel, 36, 12);
        for _ in 0..200 {
            panel.handle_key(KeyEvent::from(KeyCode::Down));
        }
        assert_eq!(panel.scrolled, 0, "back at the foot of the tape");
    }

    #[test]
    fn the_panel_draws_at_any_size_without_panicking() {
        let mut panel = new_panel();
        type_in(&mut panel, "12345+6789");
        enter(&mut panel);
        type_in(&mut panel, "1/0");
        for width in [1u16, 2, 3, 7, 12, 20, 36, 46, 120] {
            for height in [1u16, 2, 3, 5, 9, 30] {
                let _ = draw(&mut panel, width, height);
            }
        }
    }

    /// An empty panel must look ready rather than broken (invariant 11).
    #[test]
    fn an_untouched_calculator_shows_its_columns() {
        let mut panel = new_panel();
        let screen = draw(&mut panel, 36, 9);
        assert!(
            screen.contains("WORKING") && screen.contains("RESULT"),
            "the header is what says the panel is ready:\n{screen}"
        );
    }
}