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
//! Stacked toasts + progress overlay — paints, bottom-up from
//! the statusline:
//!
//! 1. `App.toast_stack` (ephemeral, TTL-expiring) — closest to
//! statusline, newest first.
//! 2. `App.persistent_toasts` (pinned until dismiss) — above.
//! 3. `App.progress_items` (active work, animated spinner) —
//! topmost. These represent the most demanding attention:
//! something is actively happening.
//!
//! Level-driven border color per `ToastLevel`: info + warn use the
//! standard comment color (calm); error uses red so failures stand
//! out. Progress items use a cyan border (distinct from toasts) so
//! the eye can separate "something's happening" from "here's a
//! notification."
use std::time::Duration;
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use crate::app::{App, PendingUndo, ProgressItem, ProgressStatus, ToastEntry, ToastLevel};
use crate::ui::theme;
// R10 nvchad SEV-3 2026-08-14 — bumped 50 → 64 so the `:q` dirty-
// buffer toast (`unsaved changes in <name> — use :q! to discard`)
// fits without silent truncation. Still capped by `area.width` so
// narrow terminals aren't blown out.
const MAX_WIDTH: u16 = 64;
const RIGHT_MARGIN: u16 = 1;
const BOTTOM_MARGIN: u16 = 2; // 1 statusline + 1 spacer
const FADE_TAIL: Duration = Duration::from_millis(800);
/// Max visible toasts. Beyond this we render a `+K more…` collapse
/// chip in place of the oldest visible slot. Bounded so a burst of
/// activity doesn't paint the whole pane column with toasts (issue #13).
const MAX_VISIBLE_TOASTS: usize = 5;
/// Braille-cycle spinner frames. Standard 8-phase pattern; each
/// frame is one Nerd-Font-safe grapheme.
const SPINNER_FRAMES: [&str; 8] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧"];
/// How long one spinner frame stays on screen before advancing.
const SPINNER_FRAME_MS: u128 = 100;
pub fn draw(frame: &mut Frame, app: &mut App) {
// mouse-round-11 SEV-3 2026-07-12 — clear the click-target
// rects BEFORE any early return so a stale rect from a
// previous frame can't outlive the toast box that painted it.
// Otherwise a right-click on the box's ghost coord opens a
// "Toast: (gone)" menu.
app.rects.toast_stack_rects.clear();
let has_persistent = !app.persistent_toasts.is_empty();
// mouse-round-10 SEV-3 2026-07-12 — was `> 1` so a solo toast
// only echoed on the cmdline bar (single-line, dim). That made
// single important notifications LESS prominent than a burst of
// trivial ones. Threshold is now `>= 1` — every toast paints as
// a top-right box; the cmdline echo persists below as the "log
// tail" for context.
let has_stack = !app.toast_stack.is_empty();
let has_progress = !app.progress_items.is_empty();
let has_undo = app.pending_undo.is_some();
if !has_persistent && !has_stack && !has_progress && !has_undo {
return;
}
let area = frame.area();
if area.width < 20 || area.height < 6 {
return;
}
let t = theme::cur();
let max_x_right = area.x + area.width.saturating_sub(RIGHT_MARGIN);
let mut y_bottom = area.y + area.height.saturating_sub(BOTTOM_MARGIN);
// #20 — pending undo chip sits closest to the statusline
// (right below the toast stack). Painted first so it's the
// most visible affordance right after the destructive action.
app.rects.pending_undo_chip = None;
if let Some(u) = app.pending_undo.clone()
&& let Some(chip_rect) = draw_undo_chip(frame, &u, &mut y_bottom, max_x_right, area, &t)
{
app.rects.pending_undo_chip = Some(chip_rect);
}
// (Rects cleared at the top of `draw` — see mouse-round-11
// SEV-3.) Renderer re-populates below. mouse-round-10 SEV-2
// 2026-07-12 — click-on-toast dismisses.
// Ephemeral toasts (newest first — closest to statusline).
// Cap the visible count; if there are more than MAX_VISIBLE_TOASTS,
// reserve the last visible slot for a "+K more…" chip so we never
// fully hide the older ones from the user's awareness.
let total = app.toast_stack.len();
let show_more_chip = total > MAX_VISIBLE_TOASTS;
let visible_take = if show_more_chip {
MAX_VISIBLE_TOASTS.saturating_sub(1)
} else {
MAX_VISIBLE_TOASTS
};
for entry in app.toast_stack.iter().take(visible_take) {
let before = y_bottom;
if !draw_toast_box(frame, entry, &mut y_bottom, max_x_right, area, &t) {
break;
}
// draw_toast_box moves y_bottom to the new top edge; the
// rendered box occupies rows [y_bottom..before). Register
// for click-to-dismiss.
let box_h = before.saturating_sub(y_bottom);
if box_h > 0 {
let box_w = 40u16.min(area.width);
let rect = ratatui::layout::Rect {
x: max_x_right.saturating_sub(box_w),
y: y_bottom,
width: box_w,
height: box_h,
};
app.rects.toast_stack_rects.push(rect);
}
}
if show_more_chip {
let hidden = total.saturating_sub(visible_take);
draw_more_chip(frame, hidden, &mut y_bottom, max_x_right, area, &t);
}
// Persistent toasts (above the ephemeral stack).
for entry in app.persistent_toasts.iter().rev() {
if !draw_toast_box(frame, entry, &mut y_bottom, max_x_right, area, &t) {
break;
}
}
// Progress items (topmost — active work).
for item in app.progress_items.iter().rev() {
if !draw_progress_box(frame, item, &mut y_bottom, max_x_right, area, &t) {
break;
}
}
}
/// Draw one toast box just above `y_bottom`; updates `y_bottom`
/// to the new top edge. Returns false when out of vertical space.
fn draw_toast_box(
frame: &mut Frame,
entry: &ToastEntry,
y_bottom: &mut u16,
max_x_right: u16,
area: Rect,
t: &crate::ui::theme::Theme,
) -> bool {
// R10 nvchad SEV-3 2026-08-14 — mark truncation with `…` so the
// reader sees the cut instead of assuming the toast just ends
// mid-word (previously the last char vanished silently — e.g.
// `use :q! to discar` for the dirty-buffer hint).
let cap = (MAX_WIDTH as usize).saturating_sub(4);
let full_len = entry.text.chars().count();
let text: String = if full_len > cap {
let mut s: String = entry.text.chars().take(cap.saturating_sub(1)).collect();
s.push('…');
s
} else {
entry.text.clone()
};
let inner_w = text.chars().count() as u16 + 2;
let box_w = (inner_w + 2)
.min(MAX_WIDTH)
.min(area.width.saturating_sub(2));
// A toast with an offer is one row taller so the chip sits INSIDE
// the box on its own line. It used to be painted on the bottom
// BORDER, which is geometrically inside but does not read that way:
// a filled chip straddling a 1-cell rule looks detached, and the
// user asked whether it was "outside the border".
let box_h: u16 = if entry.action.is_some() { 4 } else { 3 };
if *y_bottom < area.y + box_h {
return false;
}
let y = *y_bottom - box_h;
let x = max_x_right.saturating_sub(box_w);
let rect = Rect {
x,
y,
width: box_w,
height: box_h,
};
let is_persistent = entry.persistent_id.is_some();
let age = entry.created_at.elapsed();
let fading = !is_persistent && age + FADE_TAIL >= Duration::from_secs(4);
let border_fg = match entry.level {
ToastLevel::Error => t.red,
ToastLevel::Warn | ToastLevel::Info if fading => t.bg3,
_ => t.comment,
};
frame.render_widget(Clear, rect);
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(border_fg).bg(t.bg_darker))
.style(Style::default().bg(t.bg_darker));
let inner = block.inner(rect);
frame.render_widget(block, rect);
let mut spans = vec![
Span::raw(" "),
Span::styled(
text,
Style::default()
.fg(t.fg)
.bg(t.bg_darker)
.add_modifier(if fading {
Modifier::DIM
} else {
Modifier::empty()
}),
),
];
// `xN` for a message that arrived again while still on screen —
// one box saying "three times", not three boxes saying the same
// thing.
if entry.repeats > 1 {
spans.push(Span::styled(
format!(" x{}", entry.repeats),
Style::default().fg(t.comment).bg(t.bg_darker),
));
}
let line = Line::from(spans);
frame.render_widget(
Paragraph::new(line).style(Style::default().bg(t.bg_darker)),
inner,
);
// Close affordance in the top-right of the border.
//
// Clicking a toast ALREADY dismissed it — this adds no capability,
// it says the capability is there. The user asked for "a red X to
// close quickly so user not have to wait", having assumed there was
// no way to dismiss one early; that is a discoverability failure,
// not a missing feature.
//
// Painted on the border row rather than inside, so it costs the
// message no width, and dimmed while the toast is fading out so it
// does not brighten as everything else recedes.
// Action chip on the BOTTOM border, mirroring the close `x` on the
// top one — it costs the message no width, and a toast is small
// enough that an extra body line would crowd it.
if let Some(act) = &entry.action
&& rect.width > 6
{
let label = format!(" {} ", act.label());
let w = (label.chars().count() as u16).min(rect.width.saturating_sub(4));
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
label,
Style::default()
.fg(crate::ui::action_button::CHIP_LABEL_FG)
.bg(t.green)
.add_modifier(Modifier::BOLD),
))),
ratatui::layout::Rect {
x: inner.x,
y: inner.y + 1,
width: w,
height: 1,
},
);
}
// Close affordance, three cells wide and BOLD.
//
// It was a single dim `x` sitting flush against the corner glyphs,
// and the user did not know it existed until it was mentioned. Set
// off by spaces it reads as a control rather than as part of the
// rule, and the padding makes it a bigger mouse target — a 1-cell
// click target on a transient widget is a poor bet.
if rect.width > 6 {
let mut style = Style::default()
.fg(t.red)
.bg(t.bg_darker)
.add_modifier(Modifier::BOLD);
if fading {
style = style.add_modifier(Modifier::DIM);
}
frame.render_widget(
Paragraph::new(Line::from(Span::styled(" \u{00d7} ", style))),
ratatui::layout::Rect {
x: rect.x + rect.width.saturating_sub(4),
y: rect.y,
width: 3,
height: 1,
},
);
}
*y_bottom = y;
true
}
/// "+K more toasts…" collapse chip drawn above the visible stack
/// when there are more toasts than [`MAX_VISIBLE_TOASTS`]. Same
/// dimensions as a toast so the visual pattern is consistent.
fn draw_more_chip(
frame: &mut Frame,
hidden: usize,
y_bottom: &mut u16,
max_x_right: u16,
area: Rect,
t: &crate::ui::theme::Theme,
) {
let text = format!("+{hidden} more…");
let inner_w = text.chars().count() as u16 + 2;
let box_w = (inner_w + 2)
.min(MAX_WIDTH)
.min(area.width.saturating_sub(2));
let box_h: u16 = 3;
if *y_bottom < area.y + box_h {
return;
}
let y = *y_bottom - box_h;
let x = max_x_right.saturating_sub(box_w);
let rect = Rect {
x,
y,
width: box_w,
height: box_h,
};
frame.render_widget(Clear, rect);
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(t.comment).bg(t.bg_darker))
.style(Style::default().bg(t.bg_darker));
let inner = block.inner(rect);
frame.render_widget(block, rect);
let line = Line::from(vec![
Span::raw(" "),
Span::styled(
text,
Style::default()
.fg(t.comment)
.bg(t.bg_darker)
.add_modifier(Modifier::DIM),
),
]);
frame.render_widget(
Paragraph::new(line).style(Style::default().bg(t.bg_darker)),
inner,
);
*y_bottom = y;
}
/// #20 — the undo chip. Anchored just above the statusline,
/// paints a compact box: `<label> · ↶ Undo`. Returns the click
/// rect so mouse routing can dispatch to `commit_pending_undo`.
fn draw_undo_chip(
frame: &mut Frame,
u: &PendingUndo,
y_bottom: &mut u16,
max_x_right: u16,
area: Rect,
t: &crate::ui::theme::Theme,
) -> Option<Rect> {
let label: String = u.label.chars().take(MAX_WIDTH as usize - 20).collect();
// keyboard-round-8 SEV-3 2026-07-11 — was "(⇧⌃Z)" which
// implied a chord that doesn't fire the undo action. `u` is
// the actual key that fires it (vim-mode); mouse users click
// the chip. Neutral hint.
let suffix = " \u{21B6} Undo (click) ";
let inner_text = format!(" {label}{suffix}");
let inner_w = inner_text.chars().count() as u16;
let box_w = (inner_w + 2)
.min(MAX_WIDTH)
.min(area.width.saturating_sub(2));
let box_h: u16 = 3;
if *y_bottom < area.y + box_h {
return None;
}
let y = *y_bottom - box_h;
let x = max_x_right.saturating_sub(box_w);
let rect = Rect {
x,
y,
width: box_w,
height: box_h,
};
frame.render_widget(Clear, rect);
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(t.cyan).bg(t.bg_darker))
.style(Style::default().bg(t.bg_darker));
let inner = block.inner(rect);
frame.render_widget(block, rect);
let line = Line::from(vec![
Span::styled(
format!(" {label} "),
Style::default().fg(t.fg).bg(t.bg_darker),
),
Span::styled(
"· \u{21B6} Undo ",
Style::default()
.fg(t.cyan)
.bg(t.bg_darker)
.add_modifier(Modifier::BOLD),
),
Span::styled(
"(click) ",
Style::default()
.fg(t.comment)
.bg(t.bg_darker)
.add_modifier(Modifier::DIM),
),
]);
frame.render_widget(
Paragraph::new(line).style(Style::default().bg(t.bg_darker)),
inner,
);
*y_bottom = y;
Some(rect)
}
/// Draw one progress item box just above `y_bottom`; updates
/// `y_bottom` to the new top edge. Returns false when out of
/// space. Spinner phase derives from wall-clock time via
/// `started_at.elapsed()`.
fn draw_progress_box(
frame: &mut Frame,
item: &ProgressItem,
y_bottom: &mut u16,
max_x_right: u16,
area: Rect,
t: &crate::ui::theme::Theme,
) -> bool {
// Body: <glyph> <label> [<percent>%].
let glyph: String = match item.finished {
None => {
let ms = item.started_at.elapsed().as_millis();
let phase = (ms / SPINNER_FRAME_MS) as usize % SPINNER_FRAMES.len();
SPINNER_FRAMES[phase].to_string()
}
Some((ProgressStatus::Success, _)) => "✓".to_string(),
Some((ProgressStatus::Failed, _)) => "✗".to_string(),
Some((ProgressStatus::Cancelled, _)) => "⊘".to_string(),
};
let percent_suffix = item.percent.map(|p| format!(" {p}%")).unwrap_or_default();
let label: String = item
.label
.chars()
.take(MAX_WIDTH as usize - percent_suffix.chars().count() - 6)
.collect();
let body_text = format!(" {glyph} {label}{percent_suffix}");
let inner_w = body_text.chars().count() as u16;
let box_w = (inner_w + 2)
.min(MAX_WIDTH)
.min(area.width.saturating_sub(2));
let box_h: u16 = 3;
if *y_bottom < area.y + box_h {
return false;
}
let y = *y_bottom - box_h;
let x = max_x_right.saturating_sub(box_w);
let rect = Rect {
x,
y,
width: box_w,
height: box_h,
};
let border_fg = match item.finished {
None => t.cyan,
Some((ProgressStatus::Success, _)) => t.green,
Some((ProgressStatus::Failed, _)) => t.red,
Some((ProgressStatus::Cancelled, _)) => t.comment,
};
frame.render_widget(Clear, rect);
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(border_fg).bg(t.bg_darker))
.style(Style::default().bg(t.bg_darker));
let inner = block.inner(rect);
frame.render_widget(block, rect);
let line = Line::from(Span::styled(
body_text,
Style::default().fg(t.fg).bg(t.bg_darker),
));
frame.render_widget(
Paragraph::new(line).style(Style::default().bg(t.bg_darker)),
inner,
);
*y_bottom = y;
true
}
#[cfg(test)]
mod action_chip_tests {
use crate::app::{App, ToastAction, ToastLevel};
use crate::config::Config;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
fn render(app: &mut App, w: u16, h: u16) -> Vec<String> {
let mut term = Terminal::new(TestBackend::new(w, h)).unwrap();
term.draw(|f| super::draw(f, app)).unwrap();
let buf = term.backend().buffer();
(0..h)
.map(|y| (0..w).map(|x| buf[(x, y)].symbol()).collect::<String>())
.collect()
}
fn app_with_offer() -> (tempfile::TempDir, App) {
let d = tempfile::tempdir().unwrap();
let mut app = App::new(d.path().to_path_buf(), Config::default()).unwrap();
app.toast_with_action(
"needs the loopback driver",
ToastLevel::Error,
ToastAction::RunInTerminal {
label: "Install".into(),
cmd: "brew install --cask blackhole-2ch".into(),
},
);
(d, app)
}
/// USER REPORT — "the install is outside the border, is that
/// expected". It was painted on the bottom BORDER: geometrically
/// inside, but a filled chip straddling a 1-cell rule does not read
/// that way. It now has its own row within the box.
#[test]
fn the_action_chip_sits_inside_the_box_not_on_its_border() {
let (_d, mut app) = app_with_offer();
let rows = render(&mut app, 70, 12);
let i = rows
.iter()
.position(|r| r.contains("Install"))
.expect("no action chip drawn");
// The row carrying the chip must be a CONTENT row — bounded by
// the box's vertical rules, not made of horizontal rule glyphs.
assert!(
rows[i].contains('\u{2502}'),
"the chip row has no side walls — it is on a border:\n{}",
rows[i]
);
assert!(
!rows[i].contains('\u{2500}'),
"the chip is painted on a horizontal rule:\n{}",
rows[i]
);
// And a border still closes the box beneath it.
assert!(
rows[i + 1].contains('\u{2518}'),
"no bottom border under the chip:\n{}",
rows[i + 1]
);
}
/// USER REPORT — "that x is too hard to see, i didnt know it was
/// there". A single dim glyph flush against the corner. It is bold
/// and padded now, which also makes it a 3-cell mouse target rather
/// than a 1-cell one.
#[test]
fn the_close_affordance_is_padded_and_bold() {
let (_d, mut app) = app_with_offer();
let rows = render(&mut app, 70, 12);
let top = rows
.iter()
.find(|r| r.contains('\u{00d7}'))
.expect("no close affordance drawn");
assert!(
top.contains(" \u{00d7} "),
"the x is flush against the border glyphs:\n{top}"
);
}
/// A toast with NO offer keeps its original three-row shape.
#[test]
fn a_toast_without_an_offer_is_unchanged() {
let d = tempfile::tempdir().unwrap();
let mut app = App::new(d.path().to_path_buf(), Config::default()).unwrap();
app.toast_leveled("just a message", ToastLevel::Info);
let rows = render(&mut app, 70, 12);
let drawn: Vec<&String> = rows.iter().filter(|r| !r.trim().is_empty()).collect();
assert_eq!(
drawn.len(),
3,
"a toast with no offer grew a row:\n{}",
drawn
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join("\n")
);
}
}