kodact-cli 0.1.0

The kodact binary: interactive and print modes, the stream-json host protocol, subcommands.
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
//! The full-screen front page, one per style (`look.rs`): what the session
//! has, laid out before the first message. Pixel art (the wordmark in four
//! looks, the mark) is drawn in half blocks, two square pixels per cell.

use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};

use super::brand::{centred, GAP, GLYPHS, SHORT_TAGLINE, TAGLINE, WORDMARK_W};
use super::look::Look;
use super::palette;
use super::render::fit;
use super::text::{width, Theme};

/// One pixel: its colour (hex) and the ANSI colour for 16-colour terminals.
type Px = Option<(u32, Color)>;

/// How the wordmark is drawn.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Letters {
    /// "Kod" in paper, "Act" in lime.
    Flat,
    /// Paper and lime letters over a dark olive shadow, one pixel down and right.
    Shadow,
    /// Lit from above: bright lime at the top, a deeper green at the foot.
    Glow,
    /// The letters' edges only, in lime.
    Outline,
}

/// What the front page shows about the session.
#[derive(Debug, Clone, Default)]
pub struct FrontInfo {
    pub version: String,
    pub model: String,
    pub endpoint: String,
    /// The folder, as people write it (`~/proj`).
    pub folder: String,
    pub branch: Option<String>,
    /// MCP servers: (name, status).
    pub mcp: Vec<(String, String)>,
    pub tools: usize,
    /// The newest other conversations here: (what, how long ago).
    pub recent: Vec<(String, String)>,
}

/// A front page for `full_view`: rows pinned to the top, rows above and below
/// the input, where the input sits and how wide it is.
#[derive(Debug, Clone, Default)]
pub struct FrontPage {
    /// Rows at the top of the screen, whatever its height.
    pub header: Vec<Line<'static>>,
    pub above: Vec<Line<'static>>,
    pub below: Vec<Line<'static>>,
    /// The input's first column and width.
    pub box_left: usize,
    pub box_w: usize,
    /// The mode, model and endpoint on a line under the input.
    pub under: bool,
    /// The input sits at the bottom of the screen, not with the page.
    pub dock: bool,
    /// The page starts at the top (under the header) instead of centred down the screen.
    pub top: bool,
}

fn ink(hex: u32, ansi: Color, t: &Theme) -> Color {
    if !t.color {
        return Color::Reset;
    }
    palette::ink(hex, t.depth, ansi)
}

/// A grid of pixels in half blocks: one line per two rows.
fn half_blocks(grid: &[Vec<Px>], t: &Theme) -> Vec<Line<'static>> {
    let colour = |p: Px| p.map(|(hex, ansi)| ink(hex, ansi, t));
    let fg = |c: Color| if c == Color::Reset { Style::default() } else { Style::default().fg(c) };
    let w = grid.iter().map(Vec::len).max().unwrap_or(0);
    let mut out = vec![];
    for pair in grid.chunks(2) {
        let mut spans: Vec<Span<'static>> = vec![];
        for x in 0..w {
            let u = pair[0].get(x).copied().flatten().and_then(|p| colour(Some(p)));
            let l = pair.get(1).and_then(|r| r.get(x).copied().flatten()).and_then(|p| colour(Some(p)));
            let (text, style) = match (u, l) {
                (None, None) => (" ", Style::default()),
                (Some(u), None) => ("โ–€", fg(u)),
                (None, Some(l)) => ("โ–„", fg(l)),
                (Some(u), Some(l)) if u == l => ("โ–ˆ", fg(u)),
                (Some(u), Some(l)) => ("โ–€", fg(u).bg(l)),
            };
            match spans.last_mut() {
                Some(s) if s.style == style => s.content.to_mut().push_str(text),
                _ => spans.push(Span::styled(text.to_string(), style)),
            }
        }
        while spans.last().is_some_and(|s| s.content.trim().is_empty() && s.style.bg.is_none()) {
            spans.pop();
        }
        out.push(Line::from(spans));
    }
    out
}

/// The wordmark's pixels in one of its looks: 52 by 10, 53 by 11 with a
/// shadow. "Kod" is paper (ink on a light background) and "Act" lime.
fn wordmark_grid(look: Letters, light: bool) -> Vec<Vec<Px>> {
    let (paper, lime, shadow) = if light {
        ((0x101214, Color::Black), (0x4a7200, Color::Green), (0xa9c172, Color::Gray))
    } else {
        ((0xf2f0e9, Color::White), (0xc6ff3d, Color::LightGreen), (0x3d5a10, Color::Green))
    };
    // The glow: bright at the top, settling to a deeper green at the foot.
    const GLOW: [(u32, Color); 10] = [
        (0xf1ffc4, Color::White),
        (0xe4ff9a, Color::LightGreen),
        (0xd6ff6e, Color::LightGreen),
        (0xc6ff3d, Color::LightGreen),
        (0xb8f034, Color::LightGreen),
        (0xa8de2c, Color::Green),
        (0x97c925, Color::Green),
        (0x86b31e, Color::Green),
        (0x749c18, Color::Green),
        (0x628512, Color::Green),
    ];
    let step = 7 + GAP;
    let base = 6 * 7 + 5 * GAP;
    let (w, h) = if look == Letters::Shadow { (base + 1, 11) } else { (base, 10) };
    let mut letter = vec![vec![None::<usize>; w]; h];
    for (gi, rows) in GLYPHS.iter().enumerate() {
        for (y, row) in rows.iter().enumerate() {
            for (x, c) in row.chars().enumerate() {
                if c != '.' {
                    letter[y][gi * step + x] = Some(gi);
                }
            }
        }
    }
    let at = |x: isize, y: isize| -> Option<usize> {
        (x >= 0 && y >= 0).then(|| letter.get(y as usize).and_then(|r| r.get(x as usize)).copied().flatten()).flatten()
    };
    let mut grid = vec![vec![None; w]; h];
    for (y, row) in grid.iter_mut().enumerate() {
        for (x, cell) in row.iter_mut().enumerate() {
            let (xi, yi) = (x as isize, y as isize);
            *cell = match at(xi, yi) {
                Some(gi) => match look {
                    Letters::Glow => Some(GLOW[y.min(9)]),
                    Letters::Outline => {
                        let inside =
                            [(-1, 0), (1, 0), (0, -1), (0, 1)].iter().all(|(dx, dy)| at(xi + dx, yi + dy).is_some());
                        (!inside).then_some(lime)
                    }
                    _ if gi < 3 => Some(paper),
                    _ => Some(lime),
                },
                None if look == Letters::Shadow && at(xi - 1, yi - 1).is_some() => Some(shadow),
                None => None,
            };
        }
    }
    grid
}

/// `grid` with each pixel drawn `k` by `k`.
fn scaled(grid: &[Vec<Px>], k: usize) -> Vec<Vec<Px>> {
    grid.iter()
        .flat_map(|row| {
            let wide: Vec<Px> = row.iter().flat_map(|p| std::iter::repeat_n(*p, k)).collect();
            std::iter::repeat_n(wide, k)
        })
        .collect()
}

/// The wordmark in `look`: 5 rows (6 with a shadow).
pub fn wordmark(t: &Theme, look: Letters) -> Vec<Line<'static>> {
    half_blocks(&wordmark_grid(look, t.light), t)
}

/// The wordmark's width in columns.
pub fn wordmark_w(look: Letters) -> usize {
    WORDMARK_W + usize::from(look == Letters::Shadow)
}

/// The mark's size on the front pages: 16 columns by 8 rows.
pub const MARK_W: usize = 16;

/// KodAct's mark (a cursor bar and a run triangle on a lime tile), front-page size.
pub fn mark_art(t: &Theme) -> Vec<Line<'static>> {
    super::brand::mark(t, MARK_W)
}

// ---- layout helpers ----

fn line_w(l: &Line) -> usize {
    l.spans.iter().map(|s| width(&s.content)).sum()
}

/// Art `w` columns wide, centred in `cols` as one block, so its rows stay aligned.
fn centre_block(lines: Vec<Line<'static>>, w: usize, cols: usize) -> Vec<Line<'static>> {
    let x = cols.saturating_sub(w) / 2;
    lines.into_iter().map(|l| at(x, l)).collect()
}

/// `l` starting at column `x`.
fn at(x: usize, l: Line<'static>) -> Line<'static> {
    let mut spans = vec![Span::raw(" ".repeat(x))];
    spans.extend(l.spans);
    Line::from(spans)
}

/// Two blocks side by side: `left` at column `x`, `right` at column `rx`, top-aligned.
fn beside(x: usize, left: Vec<Line<'static>>, rx: usize, right: Vec<Line<'static>>) -> Vec<Line<'static>> {
    let n = left.len().max(right.len());
    (0..n)
        .map(|i| {
            let l = left.get(i).cloned().unwrap_or_default();
            let used = x + line_w(&l);
            let mut spans = vec![Span::raw(" ".repeat(x))];
            spans.extend(l.spans);
            if let Some(r) = right.get(i) {
                spans.push(Span::raw(" ".repeat(rx.saturating_sub(used))));
                spans.extend(r.spans.clone());
            }
            Line::from(spans)
        })
        .collect()
}

/// `left` at column `x` and `right` ending at column `end`.
fn spread(x: usize, left: Vec<Span<'static>>, end: usize, right: Vec<Span<'static>>) -> Line<'static> {
    let lw: usize = left.iter().map(|s| width(&s.content)).sum();
    let rw: usize = right.iter().map(|s| width(&s.content)).sum();
    let mut spans = vec![Span::raw(" ".repeat(x))];
    spans.extend(left);
    spans.push(Span::raw(" ".repeat(end.saturating_sub(x + lw + rw).max(1))));
    spans.extend(right);
    Line::from(spans)
}

/// `s` in at most `max` columns, its end kept: `โ€ฆ/acme/shop`.
fn keep_end(s: &str, max: usize) -> String {
    if width(s) <= max {
        return s.to_string();
    }
    let mut tail: Vec<char> = vec![];
    let mut used = 1;
    for c in s.chars().rev() {
        let cw = width(&c.to_string());
        if used + cw > max {
            break;
        }
        used += cw;
        tail.push(c);
    }
    tail.reverse();
    format!("โ€ฆ{}", tail.into_iter().collect::<String>())
}

fn mcp_text(info: &FrontInfo) -> Option<String> {
    let on: Vec<&str> = info.mcp.iter().filter(|(_, s)| s == "connected").map(|(n, _)| n.as_str()).collect();
    (!on.is_empty()).then(|| on.join(", "))
}

fn resume_spans(t: &Theme, what: &str, ago: &str, room: usize) -> Vec<Span<'static>> {
    vec![
        Span::styled("/resume", t.accent()),
        Span::raw("  "),
        Span::styled(fit(what, room.saturating_sub(width(ago) + 14).max(8)), t.dim()),
        Span::styled("  ยท  ", t.subtle()),
        Span::styled(ago.to_string(), t.subtle()),
    ]
}

/// `l` cut to `w` columns.
fn clip(l: Line<'static>, w: usize) -> Line<'static> {
    let mut used = 0;
    let mut spans = vec![];
    for s in l.spans {
        if used >= w {
            break;
        }
        let room = w - used;
        if width(&s.content) <= room {
            used += width(&s.content);
            spans.push(s);
        } else {
            let mut text = String::new();
            for c in s.content.chars() {
                let cw = width(&c.to_string());
                if used + cw > w {
                    break;
                }
                used += cw;
                text.push(c);
            }
            spans.push(Span::styled(text, s.style));
        }
    }
    Line::from(spans)
}

/// A rounded box `w` wide at column `x`, its rows padded inside.
fn boxed(x: usize, w: usize, rows: Vec<Line<'static>>, border: Style) -> Vec<Line<'static>> {
    let inner = w.saturating_sub(4);
    let mut out = vec![at(x, Line::from(Span::styled(format!("โ•ญ{}โ•ฎ", "โ”€".repeat(w.saturating_sub(2))), border)))];
    for r in rows {
        let r = clip(r, inner);
        let used = line_w(&r);
        let mut spans = vec![Span::raw(" ".repeat(x)), Span::styled("โ”‚ ", border)];
        spans.extend(r.spans);
        spans.push(Span::raw(" ".repeat(inner - used)));
        spans.push(Span::styled(" โ”‚", border));
        out.push(Line::from(spans));
    }
    out.push(at(x, Line::from(Span::styled(format!("โ•ฐ{}โ•ฏ", "โ”€".repeat(w.saturating_sub(2))), border))));
    out
}

// ---- the pages ----

/// The front page for `look`, `cols` wide. A terminal too narrow for a style's
/// layout gets the bare one.
pub fn front_page(t: &Theme, look: Look, info: &FrontInfo, cols: usize) -> FrontPage {
    let need = match look {
        Look::Card => 84,
        // Essentials draws the name in one row on a narrow terminal.
        Look::Frame => 0,
        Look::Ledger | Look::Status => 80,
        Look::Glow | Look::Bare => 0,
    };
    if cols < need {
        return bare(t, info, cols);
    }
    match look {
        Look::Frame => frame(t, info, cols),
        Look::Glow => glow(t, info, cols),
        Look::Card => mark_card(t, info, cols),
        Look::Ledger => ledger(t, info, cols),
        Look::Status => status_board(t, info, cols),
        Look::Bare => bare(t, info, cols),
    }
}

/// The name in one row, for a terminal too narrow for the wordmark.
fn small_name(t: &Theme) -> Vec<Span<'static>> {
    super::brand::wordmark_text(t).spans
}

/// 1 ยท Essentials: the mark and the wordmark centred together, twice the size
/// where the terminal is wide enough, "Code, then act." under them, then the
/// input (the frame style's panel, which names the mode, the model and where
/// it runs) and one line to resume the last conversation. A server that failed
/// to start says so; everything else (the folder, the branch, the version) is
/// on the status line.
fn frame(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    let one = wordmark_w(Letters::Shadow);
    // Twice the size only on a first run here (no earlier conversation): after
    // that the name steps back and the input leads.
    let first_run = info.recent.is_empty();
    let (mut above, lw) = if cols < one + 4 {
        // Too narrow for the letters: the name in one row.
        (vec![centred(Line::from(small_name(t)), cols)], 6)
    } else {
        let k = if first_run && cols >= 2 * one + 4 { 2 } else { 1 };
        let word = half_blocks(&scaled(&wordmark_grid(Letters::Shadow, t.light), k), t);
        let ww = k * one;
        // The mark as tall as the letters, beside them where there is room; without
        // colour its bar and triangle wouldn't show, so the letters go alone.
        let mark_n = 2 * word.len();
        let gap = 2 * k;
        let (logo, lw) = if t.color && cols >= ww + gap + mark_n + 4 {
            (beside(0, super::brand::mark(t, mark_n), mark_n + gap, word), mark_n + gap + ww)
        } else {
            (word, ww)
        };
        (centre_block(logo, lw, cols), lw)
    };
    // The panel at least as wide as the name above it, so the logo never overhangs the input.
    let fw = cols.saturating_sub(8).clamp(cols.min(20), 96.max(lw));
    let m = (cols - fw) / 2;
    // The text column inside the panel: the lines under it start there too.
    let col = m + 4;
    let room = fw.saturating_sub(4);
    above.push(Line::default());
    above.push(centred(Line::from(Span::styled(SHORT_TAGLINE, t.dim())), cols));
    above.push(Line::default());
    above.push(Line::default());
    let mut below = vec![];
    match info.recent.first() {
        Some((what, ago)) => {
            below.push(Line::default());
            below.push(at(col, Line::from(resume_spans(t, what, ago, room))));
        }
        // A first run: one thing to try, and where the project's instructions go.
        None => {
            below.push(Line::default());
            below.push(at(
                col,
                Line::from(vec![
                    Span::styled("Try ", t.subtle()),
                    Span::styled("\"explain this project\"", t.dim()),
                    Span::styled("  ยท  ", t.subtle()),
                    Span::styled("/init", t.accent()),
                    Span::styled(" writes KODACT.md for it", t.subtle()),
                ]),
            ));
        }
    }
    let failed: Vec<&str> = info.mcp.iter().filter(|(_, s)| s == "failed").map(|(n, _)| n.as_str()).collect();
    if !failed.is_empty() {
        below.push(Line::default());
        below.push(at(
            col,
            Line::from(vec![
                Span::styled("โ— ", t.warning()),
                Span::styled(fit(&failed.join(", "), room.saturating_sub(24)), t.dim()),
                Span::styled(" didn't start  ยท  ", t.subtle()),
                Span::styled("/mcp", t.accent()),
            ]),
        ));
    }
    // The panel names the model and where it runs.
    FrontPage { above, below, box_left: m, box_w: fw, under: true, ..Default::default() }
}

/// 2 ยท Centred, the name as hot metal with sparks over it and a glowing seam under it.
fn glow(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    let mut above = vec![];
    let ww = wordmark_w(Letters::Glow);
    if cols >= ww + 4 {
        // Sparks rising off the letters, at fixed places.
        let spark = |hex: u32| Style::default().fg(ink(hex, Color::Yellow, t));
        let rows: [&[(usize, &str, u32)]; 2] = [
            &[(9, "ยท", 0xdcff85), (21, "ห™", 0xf1ffc4), (33, "ยท", 0xc6ff3d)],
            &[(4, "ห™", 0xa8de2c), (15, "ยท", 0xdcff85), (27, "ยท", 0xb8f034), (40, "ห™", 0xc6ff3d)],
        ];
        for r in rows {
            let mut spans = vec![];
            let mut x = 0;
            for (at_x, ch, hex) in r {
                spans.push(Span::raw(" ".repeat(at_x - x)));
                spans.push(Span::styled(ch.to_string(), if t.color { spark(*hex) } else { t.subtle() }));
                x = at_x + 1;
            }
            above.push(at(cols.saturating_sub(ww) / 2, Line::from(spans)));
        }
        above.extend(centre_block(wordmark(t, Letters::Glow), ww, cols));
        // The seam: brightest in the middle.
        let n = 40usize;
        let seam: Vec<Span<'static>> = (0..n)
            .map(|i| {
                let d = (i as f32 - n as f32 / 2.0).abs() / (n as f32 / 2.0);
                let hex = if d < 0.2 {
                    0xf1ffc4
                } else if d < 0.5 {
                    0xc6ff3d
                } else if d < 0.8 {
                    0x749c18
                } else {
                    0x2a3a0c
                };
                Span::styled("โ”", if t.color { Style::default().fg(ink(hex, Color::Yellow, t)) } else { t.subtle() })
            })
            .collect();
        above.push(centred(Line::from(seam), cols));
    } else {
        above.push(centred(Line::from(small_name(t)), cols));
    }
    above.push(Line::default());
    let tagline = Line::from(Span::styled(TAGLINE, t.dim()));
    above.extend(
        super::text::wrap(vec![tagline], cols.saturating_sub(4).max(8) as u16).into_iter().map(|l| centred(l, cols)),
    );
    above.push(Line::default());
    above.push(Line::default());
    let bw = cols.saturating_sub(8).min(72);
    let mut below = vec![];
    if let Some((what, ago)) = info.recent.first() {
        below.push(Line::default());
        below.push(centred(Line::from(resume_spans(t, what, ago, cols)), cols));
    }
    FrontPage { above, below, box_left: (cols - bw) / 2, box_w: bw, under: true, ..Default::default() }
}

/// 3 ยท A card: the mark beside the name and the session's readouts; the input under it.
fn mark_card(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    let cw = cols.saturating_sub(8).min(86);
    let x = (cols - cw) / 2;
    let border = t.hairline();
    let inner = cw - 4;
    let mut rows = vec![];
    rows.push(spread(
        0,
        vec![Span::styled("kodact", t.accent()), Span::styled(format!("  v{}", info.version), t.subtle())],
        inner,
        vec![Span::styled("โ— ", t.success()), Span::styled("ready", t.dim())],
    ));
    rows.push(Line::from(Span::styled("โ”€".repeat(inner), border)));
    rows.push(Line::default());
    let mut right = wordmark(t, Letters::Flat);
    right.push(Line::default());
    let tag = if inner >= MARK_W + 5 + width(TAGLINE) { TAGLINE } else { "code, then act" };
    right.push(Line::from(Span::styled(tag, t.dim())));
    rows.extend(beside(1, mark_art(t), MARK_W + 5, right));
    rows.push(Line::default());
    let kv = |k: &str, v: String, d: String| {
        Line::from(vec![Span::styled(format!(" {k:<8}"), t.subtle()), Span::raw(v), Span::styled(d, t.subtle())])
    };
    rows.push(kv("model", info.model.clone(), format!("  on {}", info.endpoint)));
    let tools = match mcp_text(info) {
        Some(n) => format!("  ยท  {n}"),
        None => String::new(),
    };
    rows.push(kv("tools", format!("{} built in", info.tools), tools));
    let branch = info.branch.as_ref().map(|b| format!("  ยท  {b}")).unwrap_or_default();
    rows.push(kv("folder", keep_end(&info.folder, inner.saturating_sub(30)), branch));
    let mut below = vec![];
    if let Some((what, ago)) = info.recent.first() {
        below.push(Line::default());
        below.push(centred(Line::from(resume_spans(t, what, ago, cols)), cols));
    }
    FrontPage { above: boxed(x, cw, rows, border), below, box_left: x, box_w: cw, ..Default::default() }
}

/// 4 ยท Aligned left: the name in lime outline, past work numbered beside this folder,
/// the input docked at the bottom.
fn ledger(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    let m = 8;
    let end = cols - m;
    let mut header = vec![];
    let side = vec![
        Line::default(),
        Line::from(Span::styled(format!("v{}", info.version), t.subtle())),
        Line::from(Span::styled(info.model.clone(), t.dim())),
        Line::from(Span::styled(info.endpoint.clone(), t.subtle())),
    ];
    let sw = side.iter().map(line_w).max().unwrap_or(0);
    header.extend(beside(m, wordmark(t, Letters::Outline), end - sw, side));
    header.push(Line::default());
    header.push(at(m, Line::from(Span::styled("โ”€".repeat(end - m), t.hairline()))));
    header.push(Line::default());
    let half = m + (end - m) / 2 + 4;
    let mut left = vec![Line::from(Span::styled("Pick up where you left off", t.subtle())), Line::default()];
    if info.recent.is_empty() {
        left.push(Line::from(Span::styled("Nothing yet in this folder.", t.dim())));
    }
    for (i, (what, ago)) in info.recent.iter().take(3).enumerate() {
        left.push(Line::from(vec![
            Span::styled(format!("{:<4}", i + 1), t.accent()),
            Span::raw(fit(what, half.saturating_sub(m + 20))),
            Span::styled(format!("  {ago}"), t.subtle()),
        ]));
    }
    let mut right = vec![
        Line::from(Span::styled("This folder", t.subtle())),
        Line::default(),
        Line::from(keep_end(&info.folder, end.saturating_sub(half))),
    ];
    if let Some(b) = &info.branch {
        right.push(Line::from(Span::styled(format!("โއ {b}"), t.dim())));
    }
    if let Some(n) = mcp_text(info) {
        right.push(Line::from(vec![Span::styled("โ— ", t.success()), Span::styled(n, t.dim())]));
    }
    header.extend(beside(m, left, half, right));
    // The page sits centred down the screen, above the docked input.
    FrontPage { above: header, box_left: 2, box_w: cols - 4, dock: true, ..Default::default() }
}

/// 5 ยท The name over three boxed readouts of the session, the input as wide as them.
fn status_board(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    let total = cols.saturating_sub(8).min(96);
    let x = (cols - total) / 2;
    let mut above = centre_block(wordmark(t, Letters::Shadow), wordmark_w(Letters::Shadow), cols);
    above.push(Line::default());
    above.push(Line::default());
    let tw = (total - 4) / 3;
    let tile = |k: &str, v: String, d: String| -> Vec<Line<'static>> {
        boxed(
            0,
            tw,
            vec![
                Line::from(Span::styled(k.to_string(), t.accent())),
                Line::from(fit(&v, tw - 4)),
                Line::from(Span::styled(fit(&d, tw - 4), t.subtle())),
            ],
            t.hairline(),
        )
    };
    let tiles = [
        tile("Model", info.model.clone(), info.endpoint.clone()),
        tile(
            "Tools",
            format!("{} built in", info.tools),
            mcp_text(info).map(|n| format!("{n} over MCP")).unwrap_or_else(|| "no MCP servers".into()),
        ),
        tile("Folder", keep_end(&info.folder, tw - 4), info.branch.clone().unwrap_or_default()),
    ];
    for r in 0..tiles[0].len() {
        let mut spans = vec![Span::raw(" ".repeat(x))];
        for (i, tl) in tiles.iter().enumerate() {
            if i > 0 {
                spans.push(Span::raw("  "));
            }
            spans.extend(tl[r].spans.clone());
        }
        above.push(Line::from(spans));
    }
    above.push(Line::default());
    let mut below = vec![];
    if let Some((what, ago)) = info.recent.first() {
        below.push(Line::default());
        below.push(centred(Line::from(resume_spans(t, what, ago, cols)), cols));
    }
    FrontPage { above, below, box_left: x, box_w: total, ..Default::default() }
}

/// 6 ยท A small name in the corner, one question and the input in the middle, recent work under it.
fn bare(t: &Theme, info: &FrontInfo, cols: usize) -> FrontPage {
    // The model and endpoint on the right, when there is room for them beside the name.
    let where_ = format!("{} ยท {}", info.model, info.endpoint);
    let right = if cols >= 2 + 9 + 2 + width(&info.version) + 4 + width(&where_) + 2 {
        vec![Span::styled(where_, t.subtle())]
    } else {
        vec![]
    };
    // No room beside the name: the model goes under the input instead.
    let under = right.is_empty();
    let header = vec![
        Line::default(),
        spread(
            2,
            {
                let mut v = small_name(t);
                v.push(Span::styled(format!("  {}", info.version), t.subtle()));
                v
            },
            cols.saturating_sub(2),
            right,
        ),
    ];
    let bw = cols.saturating_sub(4).min(72);
    let x = (cols - bw) / 2;
    let above = vec![centred(Line::from(Span::styled("What shall we build today?", t.bold())), cols), Line::default()];
    let mut below = vec![];
    if !info.recent.is_empty() {
        below.push(Line::default());
        for (what, ago) in info.recent.iter().take(3) {
            below.push(spread(
                x + 2,
                vec![Span::styled(fit(what, bw.saturating_sub(20)), t.dim())],
                x + bw - 2,
                vec![Span::styled(ago.clone(), t.subtle())],
            ));
        }
    }
    FrontPage { header, above, below, box_left: x, box_w: bw, under, ..Default::default() }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::palette::Depth;

    fn plain(lines: &[Line]) -> Vec<String> {
        lines.iter().map(crate::tui::text::plain).collect()
    }

    const DARK: Theme = Theme { color: true, light: false, accent: None, depth: Depth::True };

    fn info() -> FrontInfo {
        FrontInfo {
            version: "0.1.0".into(),
            model: "stand-in".into(),
            endpoint: "127.0.0.1:11434".into(),
            folder: "~/acme/shop".into(),
            branch: Some("fix/cart-rounding".into()),
            mcp: vec![("jira".into(), "connected".into()), ("figma".into(), "failed".into())],
            tools: 18,
            recent: vec![("checkout rounding".into(), "2h ago".into())],
        }
    }

    #[test]
    fn the_wordmark_has_four_looks_and_the_mark_is_whole() {
        assert_eq!(wordmark(&DARK, Letters::Flat).len(), 5);
        assert_eq!(wordmark(&DARK, Letters::Shadow).len(), 6, "the shadow adds a pixel row");
        let heat = wordmark(&DARK, Letters::Glow);
        let fg = |l: &Line| {
            l.spans.iter().find(|s| s.content.contains('โ–ˆ') || s.content.contains('โ–€')).and_then(|s| s.style.fg)
        };
        assert_ne!(fg(&heat[0]), fg(&heat[4]), "hot at the top, cooler at the foot");
        let outline = plain(&wordmark(&DARK, Letters::Outline));
        assert!(outline.iter().any(|l| l.contains(' ')), "hollow letters");
        let a = mark_art(&DARK);
        assert_eq!(a.len(), 8);
        assert!(a.iter().all(|l| line_w(l) <= MARK_W));
    }

    #[test]
    fn every_style_fits_and_says_what_the_session_has() {
        for look in Look::ALL {
            for cols in [140, 100, 84, 60, 40] {
                let p = front_page(&DARK, look, &info(), cols);
                let rows: Vec<&Line> = p.header.iter().chain(&p.above).chain(&p.below).collect();
                for l in &rows {
                    assert!(line_w(l) <= cols, "{look:?} {cols}: {:?}", plain(std::slice::from_ref(*l)));
                }
                assert!(p.box_left + p.box_w <= cols, "{look:?} {cols}: the input fits");
                let text = rows.iter().map(|l| crate::tui::text::plain(l)).collect::<Vec<_>>().join("\n");
                assert!(text.contains("stand-in") || p.under, "{look:?} {cols}: the model shows\n{text}");
            }
        }
        let frame = plain(&front_page(&DARK, Look::Frame, &info(), 120).above).join("\n");
        assert!(!frame.contains("Welcome to"), "the name speaks for itself");
        // Twice the size on a first run only; after that the input leads.
        let first = FrontInfo { recent: vec![], ..info() };
        let big = front_page(&DARK, Look::Frame, &first, 140).above;
        assert!(big.iter().filter(|l| line_w(l) > 2 * WORDMARK_W).count() >= 10, "twice the size on a first run");
        assert!(big.iter().all(|l| line_w(l) <= 140));
        let again = front_page(&DARK, Look::Frame, &info(), 140).above;
        assert_eq!(big.len() - again.len(), 5, "normal size (6 rows, not 11) once there is history");
        let first_below = plain(&front_page(&DARK, Look::Frame, &first, 120).below).join("\n");
        assert!(first_below.contains("/init writes KODACT.md"), "a first run says what to try: {first_below}");
        // The panel is never narrower than the name over it.
        let p = front_page(&DARK, Look::Frame, &first, 140);
        assert!(p.box_w >= 2 * WORDMARK_W, "{}", p.box_w);
        assert!(frame.contains(SHORT_TAGLINE));
        // Essentials: what the panel or the status line already says is not repeated.
        for gone in ["Version", "tools ready", "KodAct reads", "~/acme/shop", "jira"] {
            assert!(!frame.contains(gone), "{gone}\n{frame}");
        }
        let below = plain(&front_page(&DARK, Look::Frame, &info(), 120).below).join("\n");
        assert!(below.contains("/resume  checkout rounding"), "{below}");
        assert!(below.contains("figma didn't start  ยท  /mcp"), "a server that failed says so: {below}");
        let ledger = front_page(&DARK, Look::Ledger, &info(), 120);
        assert!(ledger.dock && plain(&ledger.above).join("\n").contains("1   checkout rounding"));
        let board = plain(&front_page(&DARK, Look::Status, &info(), 120).above).join("\n");
        assert!(board.contains("Model") && board.contains("Tools") && board.contains("Folder"));
        // Too narrow for a style's layout: the bare page.
        let narrow = front_page(&DARK, Look::Card, &info(), 60);
        assert!(plain(&narrow.header).join("").contains("KodAct"));
        // Essentials keeps its layout on a narrow terminal: the name in one row, no second question.
        let narrow = plain(&front_page(&DARK, Look::Frame, &info(), 50).above).join("\n");
        assert!(narrow.contains("KodAct") && narrow.contains(SHORT_TAGLINE), "{narrow}");
        assert!(!narrow.contains("What shall we build"), "{narrow}");
        // Without colour the mark's bar and triangle wouldn't show: the letters go alone.
        let mono = Theme { color: false, ..DARK };
        let with = front_page(&DARK, Look::Frame, &info(), 140).above;
        let without = front_page(&mono, Look::Frame, &info(), 140).above;
        assert!(line_w(&without[3]) < line_w(&with[3]), "no mark without colour");
    }
}