parecode 0.1.1

A terminal coding agent built for token efficiency and local model reliability
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
/// Chat history pane rendering — build_items, draw_history, draw_chips, spinner, utilities.
use ratatui::{
    Frame,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, List, ListItem, Paragraph},
};

use super::{AppState, ConversationEntry, Mode};
use crate::plan::{PlanStatus, StepStatus};
use crate::ui::tool_glyph;

// ── Spinner ────────────────────────────────────────────────────────────────────

pub const SPINNER_GLYPHS: &[&str] = &["", "", "", "", "", "", "", "", "", ""];
const SPINNER_MSGS: &[(&str, Color)] = &[
    ("thinking…",         Color::Cyan),
    ("reasoning…",        Color::Cyan),
    ("reading context…",  Color::Cyan),
    ("crafting response…",Color::Rgb(0, 200, 255)),
    ("working on it…",    Color::Rgb(0, 220, 180)),
    ("almost there…",     Color::Rgb(100, 200, 255)),
    ("processing…",       Color::Cyan),
    ("analysing…",        Color::Cyan),
    ("on it…",            Color::Rgb(0, 220, 180)),
    ("running tools…",    Color::Yellow),
];

pub fn spinner_frame(tick: u32) -> (&'static str, &'static str, Color) {
    let glyph = SPINNER_GLYPHS[(tick as usize) % SPINNER_GLYPHS.len()];
    // Message cycles more slowly — changes every ~2 seconds (120ms × 16 ticks)
    let msg_idx = (tick as usize / 16) % SPINNER_MSGS.len();
    let (msg, color) = SPINNER_MSGS[msg_idx];
    (glyph, msg, color)
}

// ── Tool colour ────────────────────────────────────────────────────────────────

fn tool_color(tool_name: &str) -> Color {
    match tool_name {
        "read_file"               => Color::Cyan,
        "write_file" | "edit_file" => Color::Green,
        "bash"                    => Color::Yellow,
        "search"                  => Color::Magenta,
        "list_files"              => Color::Blue,
        _                         => Color::White,
    }
}

// ── History items builder ──────────────────────────────────────────────────────

pub fn build_items(state: &AppState, term_width: u16) -> Vec<ListItem<'static>> {
    let mut items: Vec<ListItem<'static>> = Vec::new();

    for entry in &state.entries {
        match entry {
            ConversationEntry::UserMessage(msg) => {
                let msg = msg.clone();
                // Bubble colours
                let bg       = Color::Rgb(28, 26, 52);
                let border   = Color::Rgb(110, 90, 200);
                let label_fg = Color::Rgb(160, 140, 255);
                let text_fg  = Color::Rgb(235, 232, 255);
                let body_style = Style::default().fg(text_fg).bg(bg);
                let edge_style = Style::default().fg(border).bg(bg);

                // Dynamic widths — 2 chars left margin, 1 right margin
                let inner_w = (term_width as usize).saturating_sub(3).max(10);
                // Top: "╭─ you ──...──╮"  — label is " you " (5 chars), corners+space = 4
                let dash_total = inner_w.saturating_sub(4 + 5); // "╭─ " + "you" + " " + "╮"
                let top_dashes = "".repeat(dash_total);
                items.push(ListItem::new(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(format!("╭─ "), edge_style),
                    Span::styled("you", Style::default()
                        .fg(label_fg).bg(bg).add_modifier(Modifier::BOLD)),
                    Span::styled(format!(" {top_dashes}"), edge_style),
                ])));

                // Body — word-wrap inside the box (inner_w minus "│ " = 2)
                let wrap_width = inner_w.saturating_sub(2).max(10);
                let raw_lines: Vec<&str> = if msg.is_empty() { vec![""] } else { msg.lines().collect() };
                let wrapped: Vec<String> = raw_lines
                    .iter()
                    .flat_map(|line| wrap_text(line, wrap_width))
                    .collect();
                for line in &wrapped {
                    items.push(ListItem::new(Line::from(vec![
                        Span::raw("  "),
                        Span::styled("", edge_style),
                        Span::styled(line.clone(), body_style),
                    ])));
                }

                // Bottom: "╰──...──╯"
                let bot_dashes = "".repeat(inner_w.saturating_sub(2)); // "╰" + dashes + "╯"
                items.push(ListItem::new(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(format!("{bot_dashes}"), edge_style),
                ])));
                items.push(ListItem::new(Line::raw("")));
            }

            ConversationEntry::ThinkingChunk(text) => {
                // Render model reasoning dimmed and indented — visually distinct from response
                let wrap_width = (term_width as usize).saturating_sub(10).max(20);
                let think_fg = Color::Rgb(100, 100, 130); // muted purple-grey
                let mut first = true;
                for src_line in text.lines() {
                    for w in wrap_text(src_line, wrap_width) {
                        if first {
                            first = false;
                            items.push(ListItem::new(Line::from(vec![
                                Span::raw("  "),
                                Span::styled("think ", Style::default().fg(think_fg).add_modifier(Modifier::ITALIC)),
                                Span::styled(w, Style::default().fg(think_fg).add_modifier(Modifier::ITALIC)),
                            ])));
                        } else {
                            items.push(ListItem::new(Line::from(vec![
                                Span::raw("        "),
                                Span::styled(w, Style::default().fg(think_fg).add_modifier(Modifier::ITALIC)),
                            ])));
                        }
                    }
                }
                items.push(ListItem::new(Line::raw("")));
            }

            ConversationEntry::AssistantChunk(text) => {
                // Word-wrap each source line to terminal width
                // "        " indent = 8 cols
                let wrap_width = (term_width as usize).saturating_sub(8).max(20);
                let label_fg = Color::Rgb(0, 210, 210);
                let text_fg  = Color::Rgb(210, 230, 255);

                let mut first = true;
                for src_line in text.lines() {
                    let wrapped = wrap_text(src_line, wrap_width);
                    for w in wrapped {
                        if first {
                            first = false;
                            items.push(ListItem::new(Line::from(vec![
                                Span::raw("  "),
                                Span::styled("parecode", Style::default()
                                    .fg(label_fg)
                                    .add_modifier(Modifier::BOLD)),
                                Span::styled("  ", Style::default()),
                                Span::styled(w, Style::default().fg(text_fg)),
                            ])));
                        } else {
                            items.push(ListItem::new(Line::from(vec![
                                Span::raw("        "),
                                Span::styled(w, Style::default().fg(text_fg)),
                            ])));
                        }
                    }
                }
                // Blank source lines (empty lines between paragraphs)
                // are preserved as blank items via wrap_text returning [""]
                items.push(ListItem::new(Line::raw("")));
            }

            ConversationEntry::ToolCall { name, args_summary } => {
                let glyph = tool_glyph(name).to_string();
                let color = tool_color(name);
                let name = name.clone();
                let args_summary = args_summary.clone();
                items.push(ListItem::new(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(format!("{glyph} {name} "), Style::default().fg(color)),
                    Span::styled(args_summary, Style::default().fg(Color::DarkGray)),
                ])));
            }

            ConversationEntry::ToolResult(summary) => {
                let mut line_iter = summary.lines();
                // First line gets the "→ " prefix
                if let Some(first) = line_iter.next() {
                    let color = if first.starts_with('') || first.contains("failed") || first.contains("error") {
                        Color::Red
                    } else {
                        Color::DarkGray
                    };
                    items.push(ListItem::new(Line::from(vec![
                        Span::raw("    "),
                        Span::styled("", Style::default().fg(Color::DarkGray)),
                        Span::styled(first.to_string(), Style::default().fg(color)),
                    ])));
                    // Subsequent lines indented to align with first, up to 20 lines
                    for line in line_iter.take(20) {
                        let color = if line.contains("error[") || line.starts_with("error") {
                            Color::Red
                        } else if line.contains("warning") {
                            Color::Yellow
                        } else {
                            Color::DarkGray
                        };
                        items.push(ListItem::new(Line::from(vec![
                            Span::raw("      "),
                            Span::styled(line.to_string(), Style::default().fg(color)),
                        ])));
                    }
                }
            }

            ConversationEntry::CacheHit(path) => {
                let path = path.clone();
                items.push(ListItem::new(Line::from(vec![
                    Span::raw("    "),
                    Span::styled("↩ cache  ", Style::default().fg(Color::DarkGray)),
                    Span::styled(path, Style::default().fg(Color::DarkGray)),
                ])));
            }

            ConversationEntry::SystemMsg(msg) => {
                for line in msg.lines() {
                    let line = line.to_string();
                    items.push(ListItem::new(Line::from(vec![
                        Span::raw("  "),
                        Span::styled(line, Style::default().fg(Color::DarkGray)),
                    ])));
                }
            }

            ConversationEntry::PlanCard => {
                items.extend(build_plan_card_items(state, state.cost_per_mtok_input));
            }

            ConversationEntry::GitNotification { files_changed, .. } => {
                // Compact single-line nudge — directs user to Git tab
                let s = if *files_changed == 1 { "" } else { "s" };
                items.push(ListItem::new(Line::from(vec![
                    Span::raw("  "),
                    Span::styled("", Style::default().fg(Color::Rgb(100, 180, 255))),
                    Span::styled(
                        format!("{files_changed} file{s} changed"),
                        Style::default().fg(Color::Rgb(140, 140, 180)),
                    ),
                    Span::styled(
                        "  — 5 to review · d to diff · /undo to revert",
                        Style::default().fg(Color::Rgb(60, 60, 90)),
                    ),
                ])));
                items.push(ListItem::new(Line::raw("")));
            }

            ConversationEntry::TaskComplete {
                input_tokens,
                output_tokens,
                tool_calls,
                compressed_count,
            } => {
                let mut spans = vec![
                    Span::raw("  "),
                    Span::styled("✓ done", Style::default()
                        .fg(Color::Rgb(0, 240, 120))
                        .add_modifier(Modifier::BOLD)),
                    Span::styled("  ·  ", Style::default().fg(Color::Rgb(50, 50, 70))),
                    Span::styled("in ", Style::default().fg(Color::DarkGray)),
                    Span::styled(input_tokens.to_string(), Style::default().fg(Color::Rgb(100, 180, 255))),
                    Span::styled("  out ", Style::default().fg(Color::DarkGray)),
                    Span::styled(output_tokens.to_string(), Style::default().fg(Color::Rgb(100, 220, 180))),
                    Span::styled("  tools ", Style::default().fg(Color::DarkGray)),
                    Span::styled(tool_calls.to_string(), Style::default().fg(Color::Rgb(200, 160, 255))),
                ];
                if *compressed_count > 0 {
                    spans.push(Span::styled(
                        format!("  · {compressed_count} compressed"),
                        Style::default().fg(Color::Rgb(80, 80, 100)),
                    ));
                }
                items.push(ListItem::new(Line::from(spans)));
                items.push(ListItem::new(Line::raw("")));
            }

            ConversationEntry::HookOutput { event, output, success } => {
                let (mark, color) = if *success {
                    ("", Color::Rgb(60, 60, 80))
                } else {
                    ("", Color::Rgb(200, 140, 60))
                };
                let label = format!("{event} {mark}");
                items.push(ListItem::new(Line::from(vec![
                    Span::styled(label, Style::default().fg(color)),
                ])));
                for line in output.lines().take(10) {
                    items.push(ListItem::new(Line::from(vec![
                        Span::styled(
                            format!("    {line}"),
                            Style::default().fg(color),
                        ),
                    ])));
                }
            }
        }
    }

    if matches!(state.mode, Mode::AgentRunning | Mode::PlanRunning) {
        let glyph = SPINNER_GLYPHS[(state.spinner_tick as usize) % SPINNER_GLYPHS.len()];
        let live = state.last_stream_text.trim();
        // Show last line of streamed text (strip newlines, truncate to fit)
        let display_text: String = live
            .lines()
            .last()
            .unwrap_or("")
            .chars()
            .take(60)
            .collect();
        if display_text.is_empty() {
            // Nothing streamed yet — fall back to rotating status message
            let (_, msg, color) = spinner_frame(state.spinner_tick);
            items.push(ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled(format!("{glyph} "), Style::default().fg(color).add_modifier(Modifier::BOLD)),
                Span::styled(msg.to_string(), Style::default().fg(color)),
            ])));
        } else if state.stream_in_think {
            items.push(ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled(format!("{glyph} "), Style::default().fg(Color::Rgb(120, 100, 180)).add_modifier(Modifier::BOLD)),
                Span::styled("think  ", Style::default().fg(Color::Rgb(80, 70, 130))),
                Span::styled(display_text, Style::default().fg(Color::Rgb(130, 115, 170)).add_modifier(Modifier::ITALIC | Modifier::DIM)),
            ])));
        } else {
            items.push(ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled(format!("{glyph} "), Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
                Span::styled(display_text, Style::default().fg(Color::Rgb(180, 220, 255))),
            ])));
        }
    }

    items
}

// ── Plan card ─────────────────────────────────────────────────────────────────

pub fn build_plan_card_items(state: &AppState, cost_per_mtok: Option<f64>) -> Vec<ListItem<'static>> {
    let Some(pr) = &state.plan_review else { return vec![] };
    let plan = &pr.plan;
    let running = matches!(state.mode, Mode::PlanRunning);
    let complete = plan.status == PlanStatus::Complete;

    let (header_fg, header_label) = if complete {
        (Color::Rgb(0, 200, 100), "✓ plan complete")
    } else if running {
        (Color::Cyan, "▶ running plan")
    } else {
        (Color::Rgb(220, 160, 30), "◇ plan ready")
    };

    let mut out: Vec<ListItem<'static>> = Vec::new();

    // Header line
    let task_fg = Color::Rgb(230, 220, 255);
    out.push(ListItem::new(Line::from(vec![
        Span::raw("  "),
        Span::styled(
            format!("{header_label}  "),
            Style::default().fg(header_fg).add_modifier(Modifier::BOLD),
        ),
        Span::styled(plan.task.clone(), Style::default().fg(task_fg)),
    ])));

    // Cost estimate line (only in review mode before execution)
    if !running && !complete {
        let estimate = plan.estimate_display(cost_per_mtok);
        let step_count = plan.steps.len();
        out.push(ListItem::new(Line::from(vec![
            Span::raw("  "),
            Span::styled(
                format!("{step_count} step{}  ·  {estimate}", if step_count == 1 { "" } else { "s" }),
                Style::default().fg(Color::Rgb(80, 75, 100)),
            ),
        ])));
    }

    // Divider
    out.push(ListItem::new(Line::from(vec![
        Span::styled(
            "  ─────────────────────────────────────",
            Style::default().fg(Color::Rgb(50, 50, 70)),
        ),
    ])));

    // Steps
    for (i, step) in plan.steps.iter().enumerate() {
        let selected = !running && i == pr.selected;
        let is_running_step = running && i == state.plan_running_step;

        let (status_glyph, status_color) = match step.status {
            StepStatus::Pass     => ("", Color::Rgb(0, 200, 100)),
            StepStatus::Approved => ("", Color::Rgb(200, 160, 30)),  // amber — reviewed, not yet run
            StepStatus::Fail     => ("", Color::Rgb(220, 60, 60)),
            StepStatus::Running  => {
                let g = SPINNER_GLYPHS[(state.spinner_tick as usize) % SPINNER_GLYPHS.len()];
                (g, Color::Cyan)
            }
            StepStatus::Skipped  => ("", Color::DarkGray),
            StepStatus::Pending  => ("", Color::DarkGray),
        };

        let ann_mark = if step.user_annotation.is_some() { "" } else { "" };

        let (num_fg, desc_fg, bg) = if selected {
            (Color::White, Color::White, Color::Rgb(35, 30, 18))
        } else if is_running_step {
            (Color::White, Color::White, Color::Rgb(15, 30, 45))
        } else {
            (Color::DarkGray, Color::Rgb(180, 180, 200), Color::Reset)
        };

        out.push(ListItem::new(Line::from(vec![
            Span::styled(
                format!("  {status_glyph} "),
                Style::default().fg(status_color).bg(bg),
            ),
            Span::styled(
                format!("{:>2}  ", i + 1),
                Style::default().fg(num_fg).bg(bg),
            ),
            Span::styled(
                format!("{}{}", step.description, ann_mark),
                Style::default().fg(desc_fg).bg(bg),
            ),
        ])));

        // Show annotation inline if selected and annotating
        if selected && pr.annotating {
            out.push(ListItem::new(Line::from(vec![
                Span::raw("       "),
                Span::styled("note: ", Style::default().fg(Color::Rgb(200, 160, 0))),
                Span::styled(pr.annotation_input.clone(), Style::default().fg(Color::White)),
                Span::styled("", Style::default().fg(Color::Rgb(200, 160, 0))),
            ])));
        } else if selected {
            if let Some(note) = &step.user_annotation {
                out.push(ListItem::new(Line::from(vec![
                    Span::raw("       "),
                    Span::styled("note: ", Style::default().fg(Color::Rgb(160, 120, 0))),
                    Span::styled(note.clone(), Style::default().fg(Color::Rgb(200, 170, 100))),
                ])));
            }
        }
    }

    // Footer hint (only in review mode, not running, not complete)
    if !running && !complete {
        // Check if all steps approved so we can show the "ready to run" prompt
        let all_approved = plan.steps.iter().all(|s| {
            matches!(s.status, StepStatus::Approved | StepStatus::Pass)
        });
        out.push(ListItem::new(Line::from(vec![
            Span::styled(
                "  ─────────────────────────────────────",
                Style::default().fg(Color::Rgb(50, 50, 70)),
            ),
        ])));
        let hint_fg = Color::Rgb(80, 75, 50);
        let key_fg = Color::Rgb(200, 160, 30);
        if all_approved {
            // All steps reviewed — show the run prompt prominently
            out.push(ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled("all steps approved  ", Style::default().fg(Color::Rgb(200, 160, 30))),
                Span::styled("Enter", Style::default().fg(Color::Rgb(0, 220, 120)).add_modifier(Modifier::BOLD)),
                Span::styled(" to run  ", Style::default().fg(hint_fg)),
                Span::styled("Esc", Style::default().fg(key_fg)),
                Span::styled(" cancel", Style::default().fg(hint_fg)),
            ])));
        } else {
            out.push(ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled("↑↓", Style::default().fg(key_fg)),
                Span::styled(" navigate  ", Style::default().fg(hint_fg)),
                Span::styled("a", Style::default().fg(key_fg)),
                Span::styled(" approve step  ", Style::default().fg(hint_fg)),
                Span::styled("e", Style::default().fg(key_fg)),
                Span::styled(" annotate  ", Style::default().fg(hint_fg)),
                Span::styled("Esc", Style::default().fg(key_fg)),
                Span::styled(" cancel", Style::default().fg(hint_fg)),
            ])));
        }
    }

    out.push(ListItem::new(Line::raw("")));
    out
}

// ── Draw functions ─────────────────────────────────────────────────────────────

pub fn draw_history(f: &mut Frame, state: &AppState, area: Rect) {
    let all_items = build_items(state, area.width);
    let total = all_items.len();
    let visible = area.height as usize;

    let skip = if total > visible {
        (total - visible).saturating_sub(state.scroll)
    } else {
        0
    };

    let sliced: Vec<ListItem<'static>> = all_items.into_iter().skip(skip).collect();
    let list = List::new(sliced)
        .block(Block::default().style(Style::default().bg(Color::Rgb(8, 8, 14))));
    f.render_widget(list, area);
}

pub fn draw_chips(f: &mut Frame, state: &AppState, area: Rect) {
    let mut spans = vec![Span::styled(" 📎 ", Style::default().fg(Color::DarkGray))];
    for (i, file) in state.attached_files.iter().enumerate() {
        let focused = state.focused_chip == Some(i);
        let name = short_filename(&file.path);
        let (bg, fg) = if focused {
            (Color::Cyan, Color::Black)
        } else {
            (Color::DarkGray, Color::White)
        };
        spans.push(Span::styled(
            format!(" {name}"),
            Style::default().fg(fg).bg(bg),
        ));
        spans.push(Span::raw(" "));
    }
    if !state.attached_files.is_empty() {
        spans.push(Span::styled(
            " Tab to focus · Del to remove ",
            Style::default().fg(Color::DarkGray),
        ));
    }
    f.render_widget(Paragraph::new(Line::from(spans)), area);
}

fn short_filename(path: &str) -> &str {
    std::path::Path::new(path)
        .file_name()
        .and_then(|f| f.to_str())
        .unwrap_or(path)
}

// ── Utilities ──────────────────────────────────────────────────────────────────

/// Format "used/total" — shows raw tokens below 1k, switches to `k` above.
/// e.g. "340/32k", "1.2k/32k", "12k/32k"
pub fn fmt_tokens(used: usize, total: u32) -> String {
    let total_k = total / 1000;
    let used_fmt = if used < 1000 {
        format!("{used}")
    } else if used < 10_000 {
        format!("{:.1}k", used as f32 / 1000.0)
    } else {
        format!("{}k", used / 1000)
    };
    format!("{used_fmt}/{total_k}k")
}

/// Word-wrap a single line of text to `max_width` columns.
/// Splits on whitespace; never truncates mid-word unless the word alone exceeds max_width.
pub fn wrap_text(text: &str, max_width: usize) -> Vec<String> {
    if text.is_empty() {
        return vec![String::new()];
    }
    let mut lines = Vec::new();
    let mut current = String::new();
    let mut current_width = 0usize;

    for word in text.split_whitespace() {
        let word_width = word.len(); // close enough for ASCII; unicode_width would be better
        if current_width == 0 {
            // First word on line
            current.push_str(word);
            current_width = word_width;
        } else if current_width + 1 + word_width <= max_width {
            current.push(' ');
            current.push_str(word);
            current_width += 1 + word_width;
        } else {
            lines.push(current.clone());
            current = word.to_string();
            current_width = word_width;
        }
    }
    if !current.is_empty() || lines.is_empty() {
        lines.push(current);
    }
    lines
}

pub fn truncate_path(path: &str, max: usize) -> String {
    if path.len() <= max {
        path.to_string()
    } else {
        format!("{}", &path[path.len() - max + 1..])
    }
}