mnml-rs 0.2.13

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! 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));
    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 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 line = Line::from(vec![
        Span::raw(" "),
        Span::styled(
            text,
            Style::default()
                .fg(t.fg)
                .bg(t.bg_darker)
                .add_modifier(if fading {
                    Modifier::DIM
                } else {
                    Modifier::empty()
                }),
        ),
    ]);
    frame.render_widget(
        Paragraph::new(line).style(Style::default().bg(t.bg_darker)),
        inner,
    );
    *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
}