dirge-agent 0.18.3

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! Bottom strip widget — avatar box, input box (or overlay), and
//! the status row.
//!
//! The strip occupies three rects from `Layout`: `avatar_box` (the
//! face on the bottom-left), `input_box` (between the chat │
//! columns), and `right_margin` (mirror gutter, always blank). The
//! `status` rect at the very bottom is a single row holding the
//! BBS-style status chips.
//!
//! When an overlay is active (permission alert / questionnaire) the
//! input editor is REPLACED inside the same frame — the alert's
//! title goes in the top border and the body lines fill the inner
//! band. No second box appears anywhere.

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color as RColor, Style};
use ratatui::widgets::Widget;

use super::layout::Layout;

/// What the input frame currently shows. `Copy` so it can be
/// captured in a `Scene` without explicit cloning.
#[derive(Clone, Copy)]
pub enum BottomBody<'a> {
    /// User input editor. Pre-wrapped into one string per visual
    /// row — caller is responsible for splitting on newlines AND
    /// soft-wrapping long logical lines to the input box's inner
    /// width. `cursor_row` and `cursor_col` locate the cursor
    /// inside `rows` (rows[cursor_row], display col 0-based).
    Editor {
        rows: &'a [String],
        cursor_row: u16,
        cursor_col: u16,
        is_running: bool,
        /// Dim preview row shown below the input when slash-command
        /// tab completion is active (e.g. "/mode  /panel  /quit").
        /// Empty string = no preview.
        completion_preview: &'a str,
        /// Inline dark-gray ghost completion painted right after the
        /// typed text (e.g. typing "/dis" shows "play"). Empty = none.
        ghost: &'a str,
    },
    /// Modal overlay replacing the editor. `title` shows in the
    /// frame's top border (e.g. `[ALERT]`); `lines` paint inside
    /// the inner band, one per row. Colors are crossterm-flavored
    /// (matches the rest of the renderer state) — converted to
    /// ratatui at paint time. `scroll` is the wrapped-row offset into
    /// the body when it overflows the box (the last line — action keys
    /// — stays pinned to the bottom); 0 shows the top.
    Overlay {
        title: &'a str,
        lines: &'a [(String, crossterm::style::Color)],
        scroll: usize,
    },
}

/// Avatar face spec — a single char and its color. The legacy
/// `( -_- )` art is multi-char; for ratatui we accept any short
/// string and clip to fit the avatar box's inner width.
pub struct AvatarSpec<'a> {
    pub face: &'a str,
    pub color: RColor,
}

/// Top-level bottom strip composer. Use the builder methods to
/// supply the avatar, body, and status text, then render against
/// the full terminal area (the widget reads rects from `layout`).
pub struct BottomStrip<'a> {
    layout: &'a Layout,
    avatar: Option<AvatarSpec<'a>>,
    body: Option<BottomBody<'a>>,
    status: &'a str,
    border_style: Style,
}

impl<'a> BottomStrip<'a> {
    pub fn new(layout: &'a Layout) -> Self {
        Self {
            layout,
            avatar: None,
            body: None,
            status: "",
            border_style: Style::default().fg(RColor::Green),
        }
    }

    pub fn avatar(mut self, spec: AvatarSpec<'a>) -> Self {
        self.avatar = Some(spec);
        self
    }
    pub fn body(mut self, body: BottomBody<'a>) -> Self {
        self.body = Some(body);
        self
    }
    pub fn status(mut self, status: &'a str) -> Self {
        self.status = status;
        self
    }
    pub fn border_style(mut self, style: Style) -> Self {
        self.border_style = style;
        self
    }
}

impl<'a> Widget for BottomStrip<'a> {
    fn render(self, _area: Rect, buf: &mut Buffer) {
        let l = self.layout;
        paint_avatar_box(buf, l.avatar_box, self.avatar.as_ref(), self.border_style);
        match self.body.as_ref() {
            Some(BottomBody::Editor {
                rows,
                is_running,
                completion_preview,
                ghost,
                ..
            }) => paint_editor_box(
                buf,
                l.input_box,
                rows,
                *is_running,
                completion_preview,
                ghost,
                self.border_style,
            ),
            Some(BottomBody::Overlay {
                title,
                lines,
                scroll,
            }) => paint_overlay_box(buf, l.input_box, title, lines, *scroll, self.border_style),
            None => paint_empty_box(buf, l.input_box, self.border_style),
        }
        // right_margin: blank — explicitly clear so stale right
        // panel pixels can't ghost in when the strip grows.
        for dy in 0..l.right_margin.height {
            for dx in 0..l.right_margin.width {
                let x = l.right_margin.x + dx;
                let y = l.right_margin.y + dy;
                buf[(x, y)].set_char(' ');
            }
        }
        paint_status(buf, l.status, self.status);
    }
}

fn paint_avatar_box(buf: &mut Buffer, area: Rect, spec: Option<&AvatarSpec>, style: Style) {
    if area.width < 4 || area.height < 2 {
        return;
    }
    let inner_w = area.width as usize - 2;

    // Top border.
    buf[(area.x, area.y)].set_char('').set_style(style);
    for i in 0..inner_w {
        buf[(area.x + 1 + i as u16, area.y)]
            .set_char('')
            .set_style(style);
    }
    buf[(area.x + area.width - 1, area.y)]
        .set_char('')
        .set_style(style);

    // Sides + blank interior.
    for dy in 1..(area.height - 1) {
        let y = area.y + dy;
        buf[(area.x, y)].set_char('').set_style(style);
        for dx in 1..(area.width - 1) {
            buf[(area.x + dx, y)].set_char(' ');
        }
        buf[(area.x + area.width - 1, y)]
            .set_char('')
            .set_style(style);
    }

    // Bottom border.
    let by = area.y + area.height - 1;
    buf[(area.x, by)].set_char('').set_style(style);
    for i in 0..inner_w {
        buf[(area.x + 1 + i as u16, by)]
            .set_char('')
            .set_style(style);
    }
    buf[(area.x + area.width - 1, by)]
        .set_char('')
        .set_style(style);

    // Centered face.
    if let Some(spec) = spec {
        let face_w = spec.face.chars().count();
        if face_w <= inner_w {
            let content_h = area.height as usize - 2;
            if content_h == 0 {
                return;
            }
            let mid_dy = (content_h / 2) as u16;
            let y = area.y + 1 + mid_dy;
            let pad = (inner_w - face_w) / 2;
            let face_style = Style::default().fg(spec.color);
            buf.set_stringn(
                area.x + 1 + pad as u16,
                y,
                spec.face,
                inner_w - pad,
                face_style,
            );
        }
    }
}

fn paint_frame(buf: &mut Buffer, area: Rect, title: Option<&str>, style: Style) {
    if area.width < 2 || area.height < 2 {
        return;
    }
    let inner_w = area.width as usize - 2;
    let title_chars: Vec<char> = title.map(|t| t.chars().collect()).unwrap_or_default();
    let tw = title_chars.len();

    // Top border.
    buf[(area.x, area.y)].set_char('').set_style(style);
    buf[(area.x + area.width - 1, area.y)]
        .set_char('')
        .set_style(style);
    if tw > 0 && tw <= inner_w {
        let pad = inner_w - tw;
        let lpad = pad / 2;
        for i in 0..lpad as u16 {
            buf[(area.x + 1 + i, area.y)].set_char('').set_style(style);
        }
        for (i, ch) in title_chars.iter().enumerate() {
            buf[(area.x + 1 + lpad as u16 + i as u16, area.y)]
                .set_char(*ch)
                .set_style(style);
        }
        for i in (1 + lpad + tw)..(1 + inner_w) {
            buf[(area.x + i as u16, area.y)]
                .set_char('')
                .set_style(style);
        }
    } else {
        for i in 0..inner_w {
            buf[(area.x + 1 + i as u16, area.y)]
                .set_char('')
                .set_style(style);
        }
    }

    // Sides + blank interior.
    for dy in 1..(area.height - 1) {
        let y = area.y + dy;
        buf[(area.x, y)].set_char('').set_style(style);
        for dx in 1..(area.width - 1) {
            buf[(area.x + dx, y)].set_char(' ');
        }
        buf[(area.x + area.width - 1, y)]
            .set_char('')
            .set_style(style);
    }

    // Bottom border.
    let by = area.y + area.height - 1;
    buf[(area.x, by)].set_char('').set_style(style);
    for i in 0..inner_w {
        buf[(area.x + 1 + i as u16, by)]
            .set_char('')
            .set_style(style);
    }
    buf[(area.x + area.width - 1, by)]
        .set_char('')
        .set_style(style);
}

fn paint_empty_box(buf: &mut Buffer, area: Rect, style: Style) {
    paint_frame(buf, area, None, style);
}

/// Width (in cells) of the input prompt zone — the prompt glyph plus
/// one trailing space. The running spinner `░▌` is 2 cells (zone 3);
/// the idle `>` is 1 cell (zone 2). Shared by `paint_editor_box` (text
/// column) and `scene.rs` (hardware cursor) so they never drift.
pub fn input_prompt_width(is_running: bool) -> u16 {
    if is_running { 3 } else { 2 }
}

#[allow(clippy::too_many_arguments)]
fn paint_editor_box(
    buf: &mut Buffer,
    area: Rect,
    rows: &[String],
    is_running: bool,
    completion_preview: &str,
    ghost: &str,
    style: Style,
) {
    paint_frame(buf, area, None, style);
    if area.width < 6 || area.height < 3 {
        return;
    }
    let inner_w = area.width as usize - 2;
    // Prompt glyph + exactly one trailing space, so the text column sits
    // one cell past the glyph. The running spinner `░▌` is 2 cells wide
    // (zone 3); the idle `>` is 1 cell (zone 2). `prompt_w` MUST equal
    // `input_prompt_width(is_running)` so `scene.rs`'s cursor lands on
    // the same column as the painted text.
    let prompt_main = if is_running { "░▌ " } else { "> " };
    // Continuation prompt — a thin glyph padded to the same zone width —
    // so wrapped lines visually attach to the prompt above.
    let prompt_cont = if is_running { "" } else { "" };
    let prompt_w = input_prompt_width(is_running) as usize;
    let accent = Style::default().fg(RColor::Yellow);
    let user = Style::default().fg(super::chat::crossterm_to_ratatui(
        crate::ui::theme::user_input(),
    ));
    let dim = Style::default().fg(RColor::DarkGray);
    let text_avail = inner_w.saturating_sub(prompt_w);
    let visible_rows = (area.height as usize).saturating_sub(2);
    let has_preview = !completion_preview.is_empty();
    let editor_rows = visible_rows.saturating_sub(if has_preview { 1 } else { 0 });
    // Track the last painted row so the inline ghost completion can be
    // drawn right after its text.
    let mut last_row: Option<(u16, usize)> = None;
    for (i, row_text) in rows.iter().take(editor_rows).enumerate() {
        let y = area.y + 1 + i as u16;
        let prompt = if i == 0 { prompt_main } else { prompt_cont };
        buf.set_stringn(area.x + 1, y, prompt, inner_w, accent);
        let text_x = area.x + 1 + prompt_w as u16;
        buf.set_stringn(text_x, y, row_text, text_avail, user);
        last_row = Some((y, row_text.chars().count()));
    }
    // Inline dark-gray ghost completion, painted right after the typed
    // text on the last row (only set when the cursor is at end-of-input).
    if !ghost.is_empty()
        && let Some((y, used)) = last_row
    {
        let remaining = text_avail.saturating_sub(used);
        if remaining > 0 {
            let ghost_x = area.x + 1 + prompt_w as u16 + used as u16;
            buf.set_stringn(ghost_x, y, ghost, remaining, dim);
        }
    }
    if has_preview {
        let preview_y = area.y + 1 + editor_rows as u16;
        buf.set_stringn(area.x + 1, preview_y, prompt_cont, inner_w, accent);
        let text_x = area.x + 1 + prompt_w as u16;
        buf.set_stringn(text_x, preview_y, completion_preview, text_avail, dim);
    }
    // Cursor positioning is owned by render_frame via
    // Frame::set_cursor_position.
}

/// Return the display width of a `label:` prefix at the start of
/// `text`, or 0 if there is no such prefix. The label must be all
/// ASCII alpha (`[A-Za-z]+`) followed by a literal `: ` — narrow
/// enough that arbitrary user content like "1:30 ratio" won't be
/// treated as a label. Used to compute hanging continuation
/// indents for wrapped overlay lines.
fn label_prefix_width(text: &str) -> usize {
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() && bytes[i].is_ascii_alphabetic() {
        i += 1;
    }
    if i > 0 && bytes.get(i) == Some(&b':') && bytes.get(i + 1) == Some(&b' ') {
        i + 2
    } else {
        0
    }
}

fn paint_overlay_box(
    buf: &mut Buffer,
    area: Rect,
    title: &str,
    lines: &[(String, crossterm::style::Color)],
    scroll: usize,
    _style: Style,
) {
    // Alert box border is yellow regardless of the supplied frame
    // style — the user must see at a glance that this is a
    // cautionary modal, not a casual chrome border.
    let yellow = Style::default().fg(RColor::Yellow);
    paint_frame(buf, area, Some(title), yellow);
    let inner_w = area.width as usize - 2;
    let inner_h = area.height as usize - 2;

    // Soft-wrap each input line, but use a single SENTINEL value for
    // the action-keys row (passed as the LAST line in `lines`) so
    // we can guarantee it stays visible even if the body overflows.
    // Lines shaped like `label: value` get a hanging continuation
    // indent equal to the label width — wrapped rows align under
    // the value column instead of bleeding back to col 0.
    let wrap_w = inner_w.saturating_sub(2).max(1);
    use crate::ui::wrap::soft_wrap;
    let last_idx = lines.len().saturating_sub(1);
    let mut head_visual: Vec<(String, crossterm::style::Color)> = Vec::new();
    let mut sticky_last: Vec<(String, crossterm::style::Color)> = Vec::new();
    for (i, (text, color)) in lines.iter().enumerate() {
        // Detect `label: ` prefix to derive the hanging indent for
        // continuation rows. e.g. `args: very long content` wraps to:
        //   args: very long
        //         content
        // rather than:
        //   args: very long
        //   content
        // We restrict this to ASCII alpha labels followed by ": " so
        // arbitrary user content like "1:30 ratio" doesn't trigger.
        let hang = label_prefix_width(text);
        let cont_indent: String = " ".repeat(hang);
        let chunks = soft_wrap(text, wrap_w, &cont_indent);
        for chunk in chunks {
            if i == last_idx {
                sticky_last.push((chunk, *color));
            } else {
                head_visual.push((chunk, *color));
            }
        }
    }

    // Layout: head visual rows fill from the top; the sticky tail
    // (action keys) always anchors to the bottom. When the head
    // overflows the available rows we DON'T silently drop the tail of
    // the command — the user must be able to read the whole thing
    // before approving it (dirge-coy3 / #587). Instead we reserve one
    // row for a scroll-status line and window the head by `scroll`, so
    // the full command is reachable with the keyboard.
    let sticky_len = sticky_last.len();
    let head_budget = inner_h.saturating_sub(sticky_len);

    let overflow = head_visual.len() > head_budget;
    // With overflow, one row goes to the scroll-status line.
    let content_rows = if overflow {
        head_budget.saturating_sub(1)
    } else {
        head_budget
    };
    let max_scroll = head_visual.len().saturating_sub(content_rows);
    let scroll = scroll.min(max_scroll);
    let end = (scroll + content_rows).min(head_visual.len());
    let head_to_show: &[(String, crossterm::style::Color)] = if head_visual.is_empty() {
        &[]
    } else {
        &head_visual[scroll..end]
    };

    // Paint head rows LEFT-aligned with 1 leading space of padding.
    let mut row_idx = 0usize;
    for (text, color) in head_to_show {
        let y = area.y + 1 + row_idx as u16;
        let st = Style::default().fg(super::chat::crossterm_to_ratatui(*color));
        buf.set_stringn(area.x + 2, y, text, inner_w.saturating_sub(2), st);
        row_idx += 1;
    }
    // Scroll-status line: tells the user there's more and how to reach
    // it, so a long command is never presented as if it were complete.
    if overflow && row_idx < inner_h {
        let above = scroll;
        let below = max_scroll.saturating_sub(scroll);
        let hint = if below > 0 {
            format!("{below} more line(s) — ↑/↓ PgUp/PgDn · Ctrl+O to scroll")
        } else {
            format!("{above} line(s) above — ↑/↓ PgUp/PgDn to scroll")
        };
        let y = area.y + 1 + row_idx as u16;
        let dim = Style::default().fg(RColor::DarkGray);
        buf.set_stringn(area.x + 2, y, &hint, inner_w.saturating_sub(2), dim);
        row_idx += 1;
    }
    // Paint the sticky tail rows (action keys) at the BOTTOM of the
    // inner band so they're always visible.
    let tail_start = inner_h.saturating_sub(sticky_len);
    for (i, (text, color)) in sticky_last.iter().enumerate() {
        let slot = tail_start + i;
        if slot >= inner_h {
            break;
        }
        let y = area.y + 1 + slot as u16;
        let st = Style::default().fg(super::chat::crossterm_to_ratatui(*color));
        buf.set_stringn(area.x + 2, y, text, inner_w.saturating_sub(2), st);
    }
    let _ = row_idx; // silence linter; head rows fill from top, tail anchors to bottom
}

/// Soft-wrap the overlay `lines` against `outer_width` (the input
/// box's outer width — inner cells = outer - 2) and return the
/// total number of visual rows the body needs. Called by the
/// renderer to size `input_box.height` so wrapped content fits
/// instead of getting clipped at the box edge.
pub fn overlay_wrapped_row_count(
    lines: &[(String, crossterm::style::Color)],
    outer_width: u16,
) -> usize {
    let inner_w = (outer_width as usize).saturating_sub(2);
    let wrap_w = inner_w.saturating_sub(4).max(1);
    use crate::ui::wrap::soft_wrap;
    let mut total = 0;
    for (text, _) in lines {
        total += soft_wrap(text, wrap_w, "  ").len();
    }
    total
}

/// Wrapped-row counts for the overlay body, split as `(head, tail)`:
/// every line except the last (the head — e.g. the command detail) and
/// the last line alone (the sticky action-keys row). Mirrors the wrap
/// width + per-line hanging indent used by [`paint_overlay_box`], so the
/// renderer can compute an accurate maximum scroll offset for a body that
/// overflows the box. Used by [`crate::ui::renderer`] to clamp the alert
/// scroll (dirge-coy3).
pub fn overlay_head_tail_wrapped(
    lines: &[(String, crossterm::style::Color)],
    outer_width: u16,
) -> (usize, usize) {
    let inner_w = (outer_width as usize).saturating_sub(2);
    let wrap_w = inner_w.saturating_sub(2).max(1);
    use crate::ui::wrap::soft_wrap;
    let last_idx = lines.len().saturating_sub(1);
    let (mut head, mut tail) = (0usize, 0usize);
    for (i, (text, _)) in lines.iter().enumerate() {
        let cont_indent = " ".repeat(label_prefix_width(text));
        let n = soft_wrap(text, wrap_w, &cont_indent).len();
        if i == last_idx {
            tail += n;
        } else {
            head += n;
        }
    }
    (head, tail)
}

fn paint_status(buf: &mut Buffer, area: Rect, status: &str) {
    if area.width == 0 || area.height == 0 {
        return;
    }
    let style = Style::default().fg(RColor::DarkGray);
    // Wipe + paint left-aligned (the legacy renderer centered it
    // under the chat band, but with the centered layout the status
    // bar reads cleaner left-flush).
    for dx in 0..area.width {
        buf[(area.x + dx, area.y)].set_char(' ');
    }
    buf.set_stringn(area.x, area.y, status, area.width as usize, style);
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    fn render<F: FnOnce(&Layout) -> BottomStrip<'_>>(
        cols: u16,
        rows: u16,
        input_rows: u16,
        build: F,
    ) -> TestBackend {
        let layout = Layout::new(cols, rows, input_rows);
        let mut backend = TestBackend::new(cols, rows);
        let mut terminal = Terminal::new(backend.clone()).unwrap();
        terminal
            .draw(|f| {
                let area = f.area();
                let widget = build(&layout);
                f.render_widget(widget, area);
            })
            .unwrap();
        backend = terminal.backend().clone();
        backend
    }

    fn row_chars(backend: &TestBackend, y: u16, x0: u16, w: u16) -> Vec<char> {
        (0..w)
            .map(|i| {
                backend
                    .buffer()
                    .cell((x0 + i, y))
                    .unwrap()
                    .symbol()
                    .chars()
                    .next()
                    .unwrap_or(' ')
            })
            .collect()
    }

    /// Avatar box: rounded frame around the avatar rect, face
    /// centered vertically + horizontally.
    #[test]
    fn avatar_box_centers_face() {
        let layout = Layout::new(160, 30, 1);
        let backend = render(160, 30, 1, |l| {
            BottomStrip::new(l).avatar(AvatarSpec {
                face: "(-_-)",
                color: RColor::Green,
            })
        });
        let area = layout.avatar_box;
        // Top + bottom borders.
        let top = row_chars(&backend, area.y, area.x, area.width);
        assert_eq!(top[0], '');
        assert_eq!(top[area.width as usize - 1], '');
        let bot = row_chars(&backend, area.y + area.height - 1, area.x, area.width);
        assert_eq!(bot[0], '');
        assert_eq!(bot[area.width as usize - 1], '');
        // Face on the middle row.
        let mid_y = area.y + 1 + (area.height - 2) / 2;
        let mid = row_chars(&backend, mid_y, area.x, area.width);
        let face_str: String = mid.iter().collect();
        assert!(face_str.contains("(-_-)"), "got mid row {:?}", face_str);
        // Edges of mid row should still be │.
        assert_eq!(mid[0], '');
        assert_eq!(mid[area.width as usize - 1], '');
    }

    /// Input box (editor mode): rounded frame + prompt + text.
    #[test]
    fn input_box_paints_editor() {
        let layout = Layout::new(160, 30, 1);
        let rows: Vec<String> = vec!["hi".to_string()];
        let mut backend = TestBackend::new(160, 30);
        let mut terminal = Terminal::new(backend.clone()).unwrap();
        terminal
            .draw(|f| {
                let area = f.area();
                let widget = BottomStrip::new(&layout).body(BottomBody::Editor {
                    rows: &rows,
                    cursor_row: 0,
                    cursor_col: 2,
                    is_running: false,
                    completion_preview: "",
                    ghost: "",
                });
                f.render_widget(widget, area);
            })
            .unwrap();
        backend = terminal.backend().clone();
        let area = layout.input_box;
        // Top row: ╭───────╮ (no title).
        let top = row_chars(&backend, area.y, area.x, area.width);
        assert_eq!(top[0], '');
        assert_eq!(top[area.width as usize - 1], '');
        // First inner row contains the prompt + "hi". The idle prompt
        // zone is `> ` (glyph + one space), so the text follows with a
        // single space.
        let body: String = row_chars(&backend, area.y + 1, area.x, area.width)
            .into_iter()
            .collect();
        assert!(body.contains("> hi"), "got body {:?}", body);
        // ...and NOT a double space (regression guard for the extra gap).
        assert!(
            !body.contains(">  hi"),
            "extra space before input: {body:?}"
        );
        // First and last cells are the side borders.
        let chars: Vec<char> = body.chars().collect();
        assert_eq!(chars[0], '');
        assert_eq!(chars[area.width as usize - 1], '');
    }

    /// Overlay mode: rounded frame with title in top border, lines
    /// painted inside. Replaces the editor — no second box.
    #[test]
    fn input_box_overlay_replaces_editor() {
        use crossterm::style::Color as CC;
        // input_rows=4 so the overlay has 4 inner rows. With our
        // sticky-tail logic, last line (action keys) anchors at the
        // bottom; head lines fill from the top. With 2 lines and
        // last-is-sticky, "⚠ PERMISSION REQUIRED" is at row 1 (top
        // inner), action-keys-equivalent "tool: read_file" at row 4
        // (bottom inner).
        let layout = Layout::new(160, 30, 4);
        let lines = vec![
            ("⚠ PERMISSION REQUIRED".to_string(), CC::Yellow),
            ("tool: read_file".to_string(), CC::Yellow),
        ];
        let mut backend = TestBackend::new(160, 30);
        let mut terminal = Terminal::new(backend.clone()).unwrap();
        terminal
            .draw(|f| {
                let area = f.area();
                let widget = BottomStrip::new(&layout).body(BottomBody::Overlay {
                    title: "[ALERT]",
                    lines: &lines,
                    scroll: 0,
                });
                f.render_widget(widget, area);
            })
            .unwrap();
        backend = terminal.backend().clone();
        let area = layout.input_box;
        let top: String = row_chars(&backend, area.y, area.x, area.width)
            .into_iter()
            .collect();
        // Top should contain [ALERT] centered.
        assert!(top.contains("[ALERT]"), "got top {:?}", top);
        assert!(top.starts_with(''));
        // Body row 0 contains the warning text.
        let body0: String = row_chars(&backend, area.y + 1, area.x, area.width)
            .into_iter()
            .collect();
        assert!(
            body0.contains("PERMISSION REQUIRED"),
            "got body0 {:?}",
            body0
        );
    }

    /// dirge-coy3 (#587): a command taller than the box must stay fully
    /// readable — the action-keys row is pinned to the bottom, a scroll
    /// hint shows there's more, and scrolling reveals the hidden tail
    /// (rather than silently dropping it under a bare "…").
    #[test]
    fn overlay_scrolls_long_command_and_pins_action_keys() {
        use crossterm::style::Color as CC;
        // 6 head lines + the action-keys row into a 4-row inner box:
        // it overflows, so the body must scroll.
        let mut lines: Vec<(String, CC)> = vec![("command: cd /a/b/c".to_string(), CC::Yellow)];
        for i in 0..5 {
            lines.push((format!("cmd-line-{i}"), CC::Yellow));
        }
        lines.push((
            "[y] allow once  [a] allow always  [n] deny  [ESC] abort".to_string(),
            CC::Yellow,
        ));

        let render_at = |scroll: usize| -> Vec<String> {
            let layout = Layout::new(160, 30, 4);
            let mut backend = TestBackend::new(160, 30);
            let mut terminal = Terminal::new(backend.clone()).unwrap();
            terminal
                .draw(|f| {
                    let area = f.area();
                    let widget = BottomStrip::new(&layout).body(BottomBody::Overlay {
                        title: "[ALERT]",
                        lines: &lines,
                        scroll,
                    });
                    f.render_widget(widget, area);
                })
                .unwrap();
            backend = terminal.backend().clone();
            let area = layout.input_box;
            (0..area.height)
                .map(|dy| {
                    row_chars(&backend, area.y + dy, area.x, area.width)
                        .into_iter()
                        .collect::<String>()
                })
                .collect()
        };

        // At the top: the command's first line shows, the LAST head line
        // (cmd-line-4) is hidden, the action keys are pinned at the bottom,
        // and a scroll hint tells the user there's more.
        let top = render_at(0);
        let joined_top = top.join("\n");
        assert!(
            joined_top.contains("command: cd /a/b/c"),
            "top view must show the command start: {joined_top:?}"
        );
        assert!(
            !joined_top.contains("cmd-line-4"),
            "the tail must be hidden at scroll=0, not silently dropped: {joined_top:?}"
        );
        assert!(
            joined_top.contains("more line(s)") || joined_top.contains("scroll"),
            "a scroll hint must indicate hidden content: {joined_top:?}"
        );
        // Action keys pinned to the bottom inner row regardless of scroll.
        let bottom_inner = &top[top.len() - 2];
        assert!(
            bottom_inner.contains("[y] allow once"),
            "action keys must be pinned at the bottom: {bottom_inner:?}"
        );

        // Scrolled down: the previously-hidden tail line becomes visible,
        // and the action keys are STILL pinned.
        let scrolled = render_at(999); // clamps to max internally
        let joined_scrolled = scrolled.join("\n");
        assert!(
            joined_scrolled.contains("cmd-line-4"),
            "scrolling must reveal the command tail: {joined_scrolled:?}"
        );
        let bottom_inner_s = &scrolled[scrolled.len() - 2];
        assert!(
            bottom_inner_s.contains("[y] allow once"),
            "action keys stay pinned after scrolling: {bottom_inner_s:?}"
        );
    }

    /// right_margin gets blanked even when the strip has body
    /// content — so right-panel pixels from earlier renders can't
    /// ghost into the bottom strip.
    #[test]
    fn right_margin_is_blanked() {
        let layout = Layout::new(160, 30, 1);
        // Pre-fill the buffer's right margin with garbage by
        // rendering once, then overwriting cells manually, then
        // re-rendering BottomStrip and asserting the cells are
        // back to ' '.
        let mut backend = TestBackend::new(160, 30);
        let mut terminal = Terminal::new(backend.clone()).unwrap();
        // First draw: write 'X' into right_margin cols.
        terminal
            .draw(|f| {
                let buf = f.buffer_mut();
                for dy in 0..layout.right_margin.height {
                    for dx in 0..layout.right_margin.width {
                        let x = layout.right_margin.x + dx;
                        let y = layout.right_margin.y + dy;
                        buf[(x, y)].set_char('X');
                    }
                }
            })
            .unwrap();
        // Second draw: BottomStrip should blank right_margin.
        terminal
            .draw(|f| {
                let area = f.area();
                f.render_widget(BottomStrip::new(&layout), area);
            })
            .unwrap();
        backend = terminal.backend().clone();
        for dy in 0..layout.right_margin.height {
            for dx in 0..layout.right_margin.width {
                let x = layout.right_margin.x + dx;
                let y = layout.right_margin.y + dy;
                let c = backend.buffer().cell((x, y)).unwrap().symbol();
                assert_eq!(c, " ", "right_margin not blanked at ({x},{y}) = {c:?}");
            }
        }
    }

    /// Status row paints the supplied status text on the bottom row.
    #[test]
    fn status_row_paints_text() {
        let layout = Layout::new(160, 30, 1);
        let backend = render(160, 30, 1, |l| BottomStrip::new(l).status("ready [code]"));
        let row: String = row_chars(&backend, layout.status.y, 0, 160)
            .into_iter()
            .collect();
        assert!(row.starts_with("ready [code]"), "got status {:?}", row);
    }
}