codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
//! Focus-context texture prototype (#4823).
//!
//! When a modal view is open, the area *outside* the focused modal can get a
//! subtle treatment so the focused region stands out:
//!
//! - `scrim` dims the already-rendered background toward the theme surface;
//! - `grain` sprinkles sparse deterministic dots over blank cells.
//!
//! Scope and guarantees, by construction:
//!
//! - **Prototype, bounded to modal contexts.** The only consumer is
//!   `ViewStack::render`, which passes the top view's `occupied_region` as the
//!   focus rect. Nothing else in the shell opts in.
//! - **Default off.** `FocusTextureMode::Off` (the default) returns zeroed
//!   stats and leaves the buffer untouched, so the render path stays
//!   byte-identical to the pre-prototype path.
//! - **Static, not animated.** The grain pattern is a pure function of cell
//!   coordinates with no time component, so it is motion-off-safe: the
//!   `low_motion` / `MotionPolicy::allows_decorative` path needs no special
//!   handling here, and two applications over the same buffer produce
//!   identical output.
//! - **Explicit fallbacks.** Off is the fallback for unknown setting values;
//!   cells whose background is `Color::Reset` (transparent terminals) are
//!   skipped under Scrim — the terminal owns that background; the grain dot
//!   falls back to `.` when ASCII-safe mode is on.
//! - **The focus rect is never painted** — the caller applies the texture
//!   before painting the backdrop and views, so the focused modal is drawn
//!   afterward at full strength and the texture can never overwrite it.
//! - **Text is never obscured.** Grain only writes blank/whitespace cells and
//!   never touches a cell that carries a symbol. Scrim preserves the
//!   WCAG AA body-text floor (4.5:1) whenever both colors are resolvable:
//!   after blending, the foreground is lifted with
//!   `palette::enforce_contrast` against the *new* background. Colors the
//!   terminal owns (`Reset`, named ANSI) are left alone rather than guessed.
//!
//! Near-fullscreen focus regions (covering at least
//! `FOCUS_COVERAGE_NOOP_PERCENT`% of the frame) and frames smaller than
//! [`FOCUS_TEXTURE_MIN_WIDTH`]x[`FOCUS_TEXTURE_MIN_HEIGHT`] refuse the
//! treatment entirely: there is no meaningful outside left to texture.

use ratatui::{buffer::Buffer, layout::Rect, style::Color};

use crate::palette::{self, AA_BODY_CONTRAST, UiTheme};

/// Minimum frame size that earns the texture. Below this, content and
/// controls own every cell. Mirrors the ambient-life floors.
pub const FOCUS_TEXTURE_MIN_WIDTH: u16 = crate::tui::ocean::AMBIENT_MIN_WIDTH;
pub const FOCUS_TEXTURE_MIN_HEIGHT: u16 = crate::tui::ocean::AMBIENT_MIN_HEIGHT;

/// Focus regions covering at least this share of the frame's cells leave no
/// meaningful outside to texture, so the pass is a no-op.
const FOCUS_COVERAGE_NOOP_PERCENT: u64 = 90;

/// Scrim background blend toward the theme surface.
const SCRIM_BG_BLEND: f32 = 0.5;
/// Scrim foreground blend toward the theme surface, before the contrast
/// floor lifts the result back to legibility.
const SCRIM_FG_BLEND: f32 = 0.25;

/// Grain dot glyph. Deterministic placement (see [`grain_dot_at`]) keeps the
/// texture static; `glyphs::ascii_fallback` maps this to `.` in ASCII-safe
/// mode.
const GRAIN_DOT: &str = "·";

/// Focus-context texture mode for modal views (#4823 prototype).
///
/// Parsed from the `focus_texture` setting at the consumption point; unknown
/// values fall back to `Off` and never panic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FocusTextureMode {
    /// No treatment. The render path is byte-identical to the pre-prototype
    /// path in this mode.
    #[default]
    Off,
    /// Dim cells outside the focused modal toward the theme surface.
    Scrim,
    /// Sparse deterministic dots on blank cells outside the focused modal.
    Grain,
}

impl FocusTextureMode {
    /// Parse a setting value; `None` for anything unknown (callers map that
    /// to `Off`). Case-insensitive, surrounding whitespace ignored.
    #[must_use]
    pub fn parse(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "off" => Some(Self::Off),
            "scrim" => Some(Self::Scrim),
            "grain" => Some(Self::Grain),
            _ => None,
        }
    }
}

/// Accounting for one [`apply_focus_texture`] pass.
///
/// Identity: `cells_examined == cells_scrimmed + cells_dotted
/// + cells_skipped_focus + cells_skipped_transparent + cells_skipped_text`.
///
/// Scrim examines every cell of the area. Grain examines focus cells, text
/// cells, and deterministic dot candidates; blank cells that earn no dot are
/// left untouched and unexamined, which keeps the identity exact without a
/// "blank but not dotted" bucket.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FocusTextureStats {
    pub cells_examined: u32,
    pub cells_scrimmed: u32,
    pub cells_dotted: u32,
    pub cells_skipped_focus: u32,
    pub cells_skipped_transparent: u32,
    pub cells_skipped_text: u32,
}

impl FocusTextureStats {
    /// The accounting identity asserted by the unit tests. This type's only
    /// consumer is the test gate below, hence the `dead_code` allowance.
    #[allow(dead_code)]
    #[must_use]
    pub fn accounted(&self) -> bool {
        self.cells_examined
            == self.cells_scrimmed
                + self.cells_dotted
                + self.cells_skipped_focus
                + self.cells_skipped_transparent
                + self.cells_skipped_text
    }
}

/// `true` when `(x, y)` lies inside `rect`.
fn rect_contains(rect: Rect, x: u16, y: u16) -> bool {
    x >= rect.left() && x < rect.right() && y >= rect.top() && y < rect.bottom()
}

/// Deterministic grain placement: a pure function of the cell coordinates,
/// so the texture is static (motion-off-safe) and reproducible.
fn grain_dot_at(x: u16, y: u16) -> bool {
    x.wrapping_mul(7)
        .wrapping_add(y.wrapping_mul(13))
        .is_multiple_of(11)
}

/// Apply the focus-context texture to `area`, treating `focus` as the
/// focused modal's occupied region. See the module docs for the guarantees.
///
/// Returns zeroed stats and leaves the buffer untouched when the mode is
/// `Off`, the frame is below the minimum size, or the focus rect (clamped to
/// the area) covers at least `FOCUS_COVERAGE_NOOP_PERCENT`% of the frame.
pub fn apply_focus_texture(
    area: Rect,
    buf: &mut Buffer,
    focus: Rect,
    theme: &UiTheme,
    mode: FocusTextureMode,
    ascii_safe: bool,
) -> FocusTextureStats {
    let mut stats = FocusTextureStats::default();
    if mode == FocusTextureMode::Off {
        return stats;
    }
    if area.width < FOCUS_TEXTURE_MIN_WIDTH || area.height < FOCUS_TEXTURE_MIN_HEIGHT {
        return stats;
    }
    let focus = focus.intersection(area);
    // u64: a full u16-square frame already fits u32, but the *100 coverage
    // compare would not.
    let area_cells = u64::from(area.width) * u64::from(area.height);
    let focus_cells = u64::from(focus.width) * u64::from(focus.height);
    if area_cells == 0 || focus_cells * 100 >= area_cells * FOCUS_COVERAGE_NOOP_PERCENT {
        return stats;
    }

    for y in area.top()..area.bottom() {
        for x in area.left()..area.right() {
            if rect_contains(focus, x, y) {
                stats.cells_examined += 1;
                stats.cells_skipped_focus += 1;
                continue;
            }
            match mode {
                FocusTextureMode::Off => unreachable!("off returns early"),
                FocusTextureMode::Scrim => {
                    stats.cells_examined += 1;
                    let cell = &buf[(x, y)];
                    // Transparent-terminal fallback: the terminal owns this
                    // background, so dimming it would be a guess. Skip.
                    if cell.bg == Color::Reset {
                        stats.cells_skipped_transparent += 1;
                        continue;
                    }
                    let new_bg =
                        crate::tui::ocean::mix_colors(cell.bg, theme.surface_bg, SCRIM_BG_BLEND);
                    let blended_fg =
                        crate::tui::ocean::mix_colors(cell.fg, theme.surface_bg, SCRIM_FG_BLEND);
                    // Text is never obscured by construction: when both
                    // colors are resolvable this lifts the foreground back to
                    // the AA body floor against the *new* background; when
                    // either side is terminal-owned the color is left alone.
                    let new_fg = palette::enforce_contrast(blended_fg, new_bg, AA_BODY_CONTRAST);
                    let cell = &mut buf[(x, y)];
                    cell.bg = new_bg;
                    cell.fg = new_fg;
                    stats.cells_scrimmed += 1;
                }
                FocusTextureMode::Grain => {
                    let cell = &buf[(x, y)];
                    // Never write over a cell that carries a symbol: grain is
                    // a background texture, not an ink.
                    if !cell.symbol().trim().is_empty() {
                        stats.cells_examined += 1;
                        stats.cells_skipped_text += 1;
                        continue;
                    }
                    // Blank cells that earn no dot are left untouched and
                    // unexamined (see the stats docs).
                    if !grain_dot_at(x, y) {
                        continue;
                    }
                    stats.cells_examined += 1;
                    let dot = if ascii_safe {
                        crate::tui::glyphs::ascii_fallback(GRAIN_DOT).unwrap_or(".")
                    } else {
                        GRAIN_DOT
                    };
                    let cell = &mut buf[(x, y)];
                    cell.set_symbol(dot);
                    // Set the dim ink only when it is resolvable; a
                    // terminal-owned `text_dim` (the Terminal theme) stays
                    // as-is rather than being guessed.
                    if palette::resolvable_rgb(theme.text_dim).is_some() {
                        cell.set_fg(theme.text_dim);
                    }
                    stats.cells_dotted += 1;
                }
            }
        }
    }
    stats
}

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

    /// The Blue Stage theme: fully resolvable (Rgb) surface and dim ink.
    fn theme() -> UiTheme {
        let theme = crate::palette::ThemeId::Whale.ui_theme();
        assert!(palette::resolvable_rgb(theme.surface_bg).is_some());
        assert!(palette::resolvable_rgb(theme.text_dim).is_some());
        theme
    }

    /// A 60x20 frame: large enough for the texture, small enough to eyeball.
    fn test_area() -> Rect {
        Rect::new(0, 0, 60, 20)
    }

    /// A focus rect well under the 90% coverage threshold (200 of 1200).
    fn test_focus() -> Rect {
        Rect::new(10, 5, 20, 10)
    }

    fn blank_buffer(area: Rect) -> Buffer {
        Buffer::empty(area)
    }

    fn assert_accounted(stats: FocusTextureStats) {
        assert!(stats.accounted(), "accounting identity broken: {stats:?}");
    }

    #[test]
    fn mode_parse_covers_every_setting_value() {
        for (value, mode) in [
            ("off", FocusTextureMode::Off),
            ("scrim", FocusTextureMode::Scrim),
            ("grain", FocusTextureMode::Grain),
        ] {
            assert_eq!(FocusTextureMode::parse(value), Some(mode));
        }
        assert_eq!(
            FocusTextureMode::parse(" SCRIM "),
            Some(FocusTextureMode::Scrim)
        );
        assert_eq!(
            FocusTextureMode::parse("Grain"),
            Some(FocusTextureMode::Grain)
        );
        assert_eq!(FocusTextureMode::parse("static"), None);
        assert_eq!(FocusTextureMode::parse(""), None);
        assert_eq!(FocusTextureMode::default(), FocusTextureMode::Off);
    }

    #[test]
    fn off_leaves_buffer_untouched() {
        let area = test_area();
        let mut buf = blank_buffer(area);
        buf[(0, 0)].set_symbol("a").set_fg(Color::White);
        buf[(1, 0)].set_bg(Color::Reset);
        let original = buf.clone();

        let stats = apply_focus_texture(
            area,
            &mut buf,
            test_focus(),
            &theme(),
            FocusTextureMode::Off,
            false,
        );

        assert_eq!(stats, FocusTextureStats::default());
        assert_eq!(buf, original);
    }

    #[test]
    fn near_fullscreen_focus_is_noop() {
        let area = test_area();
        // 59x20 = 1180 of 1200 cells (98%): over the 90% threshold.
        for focus in [area, Rect::new(0, 0, 59, 20)] {
            let mut buf = blank_buffer(area);
            let original = buf.clone();
            let stats = apply_focus_texture(
                area,
                &mut buf,
                focus,
                &theme(),
                FocusTextureMode::Scrim,
                false,
            );
            assert_eq!(stats, FocusTextureStats::default(), "focus {focus:?}");
            assert_eq!(buf, original, "focus {focus:?}");
        }
    }

    #[test]
    fn small_area_is_noop() {
        for area in [Rect::new(0, 0, 39, 20), Rect::new(0, 0, 60, 9)] {
            let mut buf = blank_buffer(area);
            let original = buf.clone();
            let stats = apply_focus_texture(
                area,
                &mut buf,
                Rect::new(0, 0, 4, 2),
                &theme(),
                FocusTextureMode::Grain,
                false,
            );
            assert_eq!(stats, FocusTextureStats::default(), "area {area:?}");
            assert_eq!(buf, original, "area {area:?}");
        }
    }

    #[test]
    fn scrim_preserves_focus_and_transparent_cells() {
        let area = test_area();
        let focus = test_focus();
        let mut buf = blank_buffer(area);
        let focus_style = Style::default().fg(Color::White).bg(Color::Blue);
        let mut reset_cells = 0_u32;
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                if rect_contains(focus, x, y) {
                    buf[(x, y)].set_symbol("F").set_style(focus_style);
                } else if (x + y) % 2 == 0 {
                    // Transparent-terminal cells outside the focus.
                    buf[(x, y)].set_bg(Color::Reset);
                    reset_cells += 1;
                } else {
                    buf[(x, y)]
                        .set_symbol("t")
                        .set_fg(Color::Rgb(200, 200, 200))
                        .set_bg(Color::Rgb(40, 40, 40));
                }
            }
        }
        let original = buf.clone();

        let stats = apply_focus_texture(
            area,
            &mut buf,
            focus,
            &theme(),
            FocusTextureMode::Scrim,
            false,
        );

        assert_accounted(stats);
        assert_eq!(stats.cells_examined, 60 * 20);
        assert_eq!(stats.cells_skipped_focus, 20 * 10);
        assert_eq!(stats.cells_skipped_transparent, reset_cells);
        assert_eq!(stats.cells_dotted, 0);
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                if rect_contains(focus, x, y) {
                    assert_eq!(
                        buf[(x, y)],
                        original[(x, y)],
                        "focus cell ({x},{y}) must stay byte-identical"
                    );
                } else if (x + y) % 2 == 0 {
                    assert_eq!(
                        buf[(x, y)],
                        original[(x, y)],
                        "Reset-bg cell ({x},{y}) must stay untouched"
                    );
                }
            }
        }
    }

    #[test]
    fn scrim_text_keeps_aa_contrast_when_resolvable() {
        let area = test_area();
        let focus = test_focus();
        let theme = theme();
        let combos = [
            (Color::Rgb(255, 255, 255), Color::Rgb(30, 30, 30)),
            (Color::Rgb(200, 200, 200), Color::Rgb(240, 240, 240)),
            (Color::Rgb(120, 120, 120), Color::Rgb(110, 110, 110)),
            (Color::Rgb(40, 80, 200), Color::Rgb(20, 20, 30)),
        ];
        for (row, (fg, bg)) in combos.iter().enumerate() {
            let mut buf = blank_buffer(area);
            let y = row as u16;
            let mut seeded = Vec::new();
            for x in area.left()..area.right() {
                if rect_contains(focus, x, y) {
                    continue;
                }
                buf[(x, y)].set_symbol("t").set_fg(*fg).set_bg(*bg);
                seeded.push(x);
            }

            let stats = apply_focus_texture(
                area,
                &mut buf,
                focus,
                &theme,
                FocusTextureMode::Scrim,
                false,
            );

            assert_accounted(stats);
            for x in seeded {
                let cell = &buf[(x, y)];
                assert_eq!(cell.symbol(), "t", "glyph must survive the scrim");
                let ratio = palette::contrast_ratio(cell.fg, cell.bg)
                    .expect("seeded colors are Rgb and stay resolvable");
                assert!(
                    ratio >= AA_BODY_CONTRAST,
                    "combo {fg:?}/{bg:?} ended at {ratio}:1, below the AA floor"
                );
            }
        }
    }

    #[test]
    fn grain_never_overwrites_text_and_dots_are_deterministic() {
        let area = test_area();
        let focus = test_focus();
        let theme = theme();
        let mut buf = blank_buffer(area);
        let mut text_cells = Vec::new();
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                if !rect_contains(focus, x, y) && (x + y) % 3 == 0 {
                    buf[(x, y)].set_symbol("a").set_fg(Color::White);
                    text_cells.push((x, y));
                }
            }
        }
        let original = buf.clone();

        let stats = apply_focus_texture(
            area,
            &mut buf,
            focus,
            &theme,
            FocusTextureMode::Grain,
            false,
        );

        assert_accounted(stats);
        assert_eq!(stats.cells_skipped_focus, 20 * 10);
        assert_eq!(stats.cells_skipped_text, text_cells.len() as u32);
        assert_eq!(stats.cells_scrimmed, 0);
        // Every seeded text cell is untouched, byte for byte.
        for (x, y) in &text_cells {
            assert_eq!(
                buf[(*x, *y)],
                original[(*x, *y)],
                "text cell ({x},{y}) must never be overwritten"
            );
        }
        // Dots land exactly at the deterministic positions on blank cells.
        let mut expected_dots = 0_u32;
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                if rect_contains(focus, x, y) || (x + y) % 3 == 0 {
                    continue;
                }
                if grain_dot_at(x, y) {
                    expected_dots += 1;
                    assert_eq!(buf[(x, y)].symbol(), GRAIN_DOT, "dot at ({x},{y})");
                    assert_eq!(buf[(x, y)].fg, theme.text_dim);
                } else {
                    assert_eq!(
                        buf[(x, y)],
                        original[(x, y)],
                        "non-dot blank cell ({x},{y}) must stay untouched"
                    );
                }
            }
        }
        assert_eq!(stats.cells_dotted, expected_dots);
    }

    #[test]
    fn grain_ascii_safe_uses_plain_dot() {
        let area = test_area();
        let focus = test_focus();
        let mut buf = blank_buffer(area);

        let stats = apply_focus_texture(
            area,
            &mut buf,
            focus,
            &theme(),
            FocusTextureMode::Grain,
            true,
        );

        assert_accounted(stats);
        assert!(stats.cells_dotted > 0);
        for y in area.top()..area.bottom() {
            for x in area.left()..area.right() {
                if !rect_contains(focus, x, y) && grain_dot_at(x, y) {
                    assert_eq!(buf[(x, y)].symbol(), ".", "ascii dot at ({x},{y})");
                }
            }
        }
    }

    #[test]
    fn grain_is_deterministic_across_applications() {
        let area = test_area();
        let focus = test_focus();
        let theme = theme();
        let mut first = blank_buffer(area);
        let mut second = blank_buffer(area);

        apply_focus_texture(
            area,
            &mut first,
            focus,
            &theme,
            FocusTextureMode::Grain,
            false,
        );
        apply_focus_texture(
            area,
            &mut second,
            focus,
            &theme,
            FocusTextureMode::Grain,
            false,
        );

        // Static texture: no time component, so motion-off needs no special
        // path and repeated passes over the same buffer agree exactly.
        assert_eq!(first, second);
    }
}