kono-wt 1.1.0

A single-binary CLI + TUI for managing Git worktrees and their GitHub pull requests.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
//! Modal overlays for the TUI (spec §10): the help screen, the create and
//! checkout pickers, the PR compose form and picker, and the confirm dialogs.
//! Each is a pure render of [`App`] state plus its mode-specific state; the
//! parent [`render`](super::render) dispatches to these by [`Mode`](super::Mode).

use super::*;

/// Renders the help overlay: the full key-binding reference, generated from the
/// live [`Keymap`](crate::keys::Keymap) so every action is documented and the
/// list can never drift from the actual bindings (issue #39). One row per
/// [`KeyAction`], skipping any action the user has unbound.
pub(super) fn render_help(app: &App, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let lines: Vec<Line> = KeyAction::ALL
        .iter()
        .filter_map(|&action| {
            app.keymap
                .display_for(action)
                .map(|keys| (keys, action.label()))
        })
        .map(|(keys, desc)| {
            Line::from(vec![
                Span::styled(format!("{keys:<14}"), theme.hint_key()),
                Span::styled(desc.to_string(), theme.hint_label()),
            ])
        })
        .collect();
    let rect = centered(area, 56, lines.len() as u16 + 2);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("help", theme.title(true)))),
        rect,
    );
}

/// The most option rows shown at once in an inline dropdown before it scrolls.
const OPTION_ROWS: usize = 6;

/// Builds the inline option-dropdown lines for an open [`OptionList`] (issue
/// #25): each match on its own line with the cursor row highlighted, windowed to
/// [`OPTION_ROWS`] and capped with a "N more" hint. Shared by the create and
/// compose modals so every pop-up field selects options the same way.
fn option_lines(theme: &Theme, options: &OptionList) -> Vec<Line<'static>> {
    let total = options.match_count();
    let cursor = options.cursor();
    // Window of up to OPTION_ROWS rows that keeps the cursor visible.
    let start = if cursor >= OPTION_ROWS {
        cursor - (OPTION_ROWS - 1)
    } else {
        0
    };
    let end = (start + OPTION_ROWS).min(total);
    let labels: Vec<&str> = options.match_labels().collect();
    let mut lines: Vec<Line<'static>> = labels[start..end]
        .iter()
        .enumerate()
        .map(|(i, label)| {
            let highlighted = start + i == cursor;
            let (symbol, style) = if highlighted {
                (theme.selection_symbol(), theme.selection())
            } else {
                ("  ", theme.label())
            };
            Line::from(Span::styled(format!("{symbol}{label}"), style))
        })
        .collect();
    if total > end {
        lines.push(Line::from(Span::styled(
            format!("{} more", total - end),
            theme.hint_label(),
        )));
    }
    lines
}

/// The model/effort options dropdown for the active PR-compose field, seeded to
/// the current selection; `None` for the free-text title/body fields.
fn compose_dropdown(state: &PrComposeState) -> Option<OptionList> {
    let (labels, index) = match state.field {
        ComposeField::Model => (
            AgentModel::all()
                .iter()
                .map(|m| m.label().to_string())
                .collect(),
            AgentModel::all().iter().position(|m| *m == state.model)?,
        ),
        ComposeField::Effort => (
            Effort::all()
                .iter()
                .map(|e| e.label().to_string())
                .collect(),
            Effort::all().iter().position(|e| *e == state.effort)?,
        ),
        ComposeField::Title | ComposeField::Body => return None,
    };
    let mut options = OptionList::new(labels);
    options.open();
    options.set_cursor(index);
    Some(options)
}

/// Renders the create-worktree prompt.
pub(super) fn render_create(app: &App, state: &CreateState, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let field = |active: bool, label: &'static str, value: &str| {
        let (marker, style) = if active {
            (label, theme.accent())
        } else {
            (label, theme.label())
        };
        Line::from(vec![
            Span::styled(marker.to_string(), style),
            Span::raw(format!(" {value}")),
        ])
    };
    let mut lines = vec![
        field(
            state.step == CreateStep::Branch,
            // Leading marker shows which field is active.
            if state.step == CreateStep::Branch {
                "> branch:"
            } else {
                "  branch:"
            },
            &state.branch,
        ),
        field(
            state.step == CreateStep::Base,
            if state.step == CreateStep::Base {
                "> base:  "
            } else {
                "  base:  "
            },
            &state.base,
        ),
    ];
    if let Some(err) = &state.error {
        lines.push(Line::from(Span::styled(format!("! {err}"), theme.error())));
    }
    // Inline options dropdown for the active field (existing branches).
    if state.options.is_open() {
        lines.extend(option_lines(&theme, &state.options));
    }
    lines.push(Line::from(Span::styled(
        hints::format_hint_row(hints::create_hints()),
        theme.hint_label(),
    )));
    // Grow the modal to fit the fields, optional error, dropdown, and hint.
    let rect = centered(area, 60, lines.len() as u16 + 2);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("new worktree", theme.title(true)))),
        rect,
    );
}

/// Renders the checkout branch-picker: a single query line over the known
/// branches with the type-ahead dropdown, the target worktree, and any error.
pub(super) fn render_checkout(app: &App, state: &CheckoutState, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let target = app
        .worktrees
        .get(state.worktree_index)
        .and_then(|w| w.branch.clone())
        .unwrap_or_else(|| "worktree".to_string());
    let mut lines = vec![
        Line::from(vec![
            Span::styled("worktree: ", theme.label()),
            Span::styled(target, theme.accent()),
        ]),
        Line::from(vec![
            Span::styled("> branch: ", theme.accent()),
            Span::raw(state.query.clone()),
        ]),
    ];
    if let Some(err) = &state.error {
        lines.push(Line::from(Span::styled(format!("! {err}"), theme.error())));
    }
    if state.options.is_open() {
        lines.extend(option_lines(&theme, &state.options));
    }
    lines.push(Line::from(Span::styled(
        hints::format_hint_row(hints::checkout_hints()),
        theme.hint_label(),
    )));
    let rect = centered(area, 60, lines.len() as u16 + 2);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("checkout branch", theme.title(true)))),
        rect,
    );
}

/// Renders the PR compose form (`wt pr open`): a title + multi-line body with a
/// header showing branch → trunk, the create/update action, and the draft state.
pub(super) fn render_pr_compose(app: &App, state: &PrComposeState, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);

    let title_active = state.field == ComposeField::Title;
    let body_active = state.field == ComposeField::Body;
    let model_active = state.field == ComposeField::Model;
    let effort_active = state.field == ComposeField::Effort;
    let draft_mark = if state.draft { "[x]" } else { "[ ]" };
    // The active option field gets the `>` marker, mirroring the text fields.
    let opt_label = |active: bool, label: &'static str| {
        Span::styled(
            label,
            if active {
                theme.accent()
            } else {
                theme.label()
            },
        )
    };

    let mut lines = vec![
        // Header: branch → trunk   [action]   draft [x]/[ ]
        Line::from(vec![
            Span::styled(format!("{} ", state.branch), theme.accent()),
            Span::raw(""),
            Span::styled(state.trunk.clone(), theme.label()),
            Span::raw("   "),
            Span::styled(format!("[{}]", state.action_label), theme.label()),
            Span::raw("   "),
            Span::styled(format!("draft {draft_mark}"), theme.label()),
        ]),
        // Agent settings used for `Ctrl-A` auto-fill (model + effort).
        Line::from(vec![
            opt_label(
                model_active,
                if model_active {
                    "> model: "
                } else {
                    "  model: "
                },
            ),
            Span::styled(state.model.label().to_string(), theme.accent()),
            Span::raw("   "),
            opt_label(effort_active, "effort: "),
            Span::styled(state.effort.label().to_string(), theme.accent()),
        ]),
        Line::raw(""),
        // Title field.
        Line::from(vec![
            Span::styled(
                if title_active {
                    "> title: "
                } else {
                    "  title: "
                },
                if title_active {
                    theme.accent()
                } else {
                    theme.label()
                },
            ),
            Span::raw(state.title.clone()),
        ]),
        Line::raw(""),
        // Body field label.
        Line::from(Span::styled(
            if body_active { "> body:" } else { "  body:" },
            if body_active {
                theme.accent()
            } else {
                theme.label()
            },
        )),
    ];
    // When a model/effort field is active, show its options dropdown right under
    // the agent-settings line; the modal grows to keep the rest from clipping.
    let mut extra_rows = 0u16;
    if let Some(options) = compose_dropdown(state) {
        let dropdown = option_lines(&theme, &options);
        extra_rows = dropdown.len() as u16;
        lines.splice(2..2, dropdown);
    }
    // Body content (multi-line); show at least one (blank) line.
    if state.body.is_empty() {
        lines.push(Line::raw(""));
    } else {
        for line in state.body.split('\n') {
            lines.push(Line::raw(format!("  {line}")));
        }
    }
    if let Some(err) = &state.error {
        lines.push(Line::from(Span::styled(format!("! {err}"), theme.error())));
    }
    lines.push(Line::raw(""));
    if state.submitting {
        lines.push(Line::from(Span::styled("working…", theme.hint_label())));
    } else {
        // Two hint rows: AI auto-fill controls, then the edit/submit controls.
        lines.push(Line::from(Span::styled(
            hints::format_hint_row(hints::compose_ai_hints()),
            theme.hint_label(),
        )));
        lines.push(Line::from(Span::styled(
            hints::format_hint_row(hints::compose_edit_hints()),
            theme.hint_label(),
        )));
    }

    // Keep the established size, but grow for an open options dropdown.
    let rect = centered(area, 76, 20 + extra_rows);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("open pull request", theme.title(true))))
            .wrap(Wrap { trim: false }),
        rect,
    );
}

/// Renders the PR picker overlay.
pub(super) fn render_pr_picker(app: &App, state: &PrPickerState, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let rect = centered(area, 70, 20);
    frame.render_widget(Clear, rect);
    let block = Block::bordered().title(Span::styled("open pull requests", theme.title(true)));
    if let Some(err) = &state.error {
        frame.render_widget(
            Paragraph::new(vec![
                Line::from(Span::styled(err.clone(), theme.error())),
                Line::from(Span::styled(
                    "(run `gh auth login`)   Esc: close",
                    theme.hint_label(),
                )),
            ])
            .block(block),
            rect,
        );
        return;
    }
    if state.loading {
        frame.render_widget(
            Paragraph::new(Span::styled("loading…", theme.spinner())).block(block),
            rect,
        );
        return;
    }
    let now = now_unix();
    let items: Vec<ListItem> = state
        .prs
        .iter()
        .map(|pr| {
            let state_style = PrState::parse(&pr.state)
                .map(|s| theme.pr_state(s))
                .unwrap_or_default();
            let age = parse_iso8601(&pr.created_at)
                .map(|u| relative(now, u))
                .unwrap_or_default();
            ListItem::new(Line::from(vec![
                Span::styled(format!("#{}", pr.number), theme.commit_hash()),
                Span::raw("  "),
                Span::raw(pr.title.clone()),
                Span::raw("  "),
                Span::styled(format!("({})", pr.author), theme.hint_label()),
                Span::raw("  "),
                Span::styled(pr.state.clone(), state_style),
                Span::raw("  "),
                Span::styled(age, theme.time()),
            ]))
        })
        .collect();
    let list = List::new(items)
        .block(block)
        .highlight_style(theme.selection())
        .highlight_symbol(theme.selection_symbol())
        .highlight_spacing(HighlightSpacing::Always);
    let mut list_state = ListState::default().with_selected(Some(state.selected));
    frame.render_stateful_widget(list, rect, &mut list_state);
}

/// Renders the confirm-remove dialog.
///
/// Beyond the branch, path, and safety warnings, this surfaces the same
/// glanceable context as the detail pane — upstream/base, ahead/behind and
/// working-tree state, the tip commit with its age, and any recorded PR — so a
/// deletion decision can be made without leaving the dialog (issue #7). All of
/// this is already on the [`crate::model::Worktree`] row; nothing new is read
/// from git. The dialog grows to fit its content.
pub(super) fn render_confirm(app: &App, index: usize, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let Some(worktree) = app.worktrees.get(index) else {
        return;
    };
    let now = now_unix();
    let glyphs = Glyphs::new(app.nerd_fonts);
    let loaded = app.is_loaded(worktree);

    // Branch + upstream (or "(no upstream)"), mirroring the detail pane.
    let branch_span = Span::styled(
        branch_display(worktree),
        theme.branch(worktree.is_current, worktree.is_detached),
    );
    let mut lines = match &worktree.upstream {
        Some(up) => vec![Line::from(vec![
            Span::styled("branch: ", theme.label()),
            branch_span,
            Span::raw(""),
            Span::styled(up.clone(), theme.accent()),
        ])],
        None => vec![Line::from(vec![
            Span::styled("branch: ", theme.label()),
            branch_span,
            Span::styled(" (no upstream)", theme.label()),
        ])],
    };
    lines.push(Line::from(vec![
        Span::styled("path:   ", theme.label()),
        Span::raw(worktree.path.display().to_string()),
    ]));
    if let Some(base) = &worktree.base_ref {
        lines.push(Line::from(vec![
            Span::styled("base:   ", theme.label()),
            Span::raw(base.clone()),
        ]));
    }

    if worktree.is_missing {
        // No working tree to read — the safety marker is all the context there is.
        lines.push(Line::from(Span::styled(
            "(directory already deleted)",
            theme.hint_label(),
        )));
    } else {
        // Glanceable status / commit / PR, gated on the async load like the
        // detail pane: a spinner until the row's fields are available.
        if loaded {
            let mut status_spans = vec![Span::styled("status: ", theme.label())];
            status_spans.extend(ahead_behind_spans(worktree, &theme, true, &glyphs));
            status_spans.push(Span::raw("  "));
            status_spans.push(dirty_label_span(worktree, &theme));
            lines.push(Line::from(status_spans));
            if let Some(c) = &worktree.commit {
                let rel = parse_iso8601(&c.timestamp)
                    .map(|u| relative(now, u))
                    .unwrap_or_default();
                lines.push(Line::from(vec![
                    Span::styled("commit: ", theme.label()),
                    Span::styled(c.hash.clone(), theme.commit_hash()),
                    Span::raw(" "),
                    Span::raw(c.subject.clone()),
                    Span::raw(" "),
                    Span::styled(format!("({rel})"), theme.time()),
                ]));
            }
            if let Some(pr) = &worktree.pr {
                lines.push(Line::from(vec![
                    Span::styled("pr:     ", theme.label()),
                    Span::styled(
                        format!("#{} ({}) ", pr.number, pr.state.as_str()),
                        theme.pr_state(pr.state),
                    ),
                    Span::raw(pr.title.clone()),
                ]));
                if let Some(url) = worktree.pr_url.as_deref().filter(|u| !u.is_empty()) {
                    lines.push(Line::from(vec![
                        Span::raw("        "),
                        Span::styled(url.to_string(), theme.url()),
                    ]));
                }
            }
        } else {
            lines.push(Line::from(vec![
                Span::styled("status: ", theme.label()),
                Span::styled("", theme.spinner()),
            ]));
        }

        // Safety warnings, layered on top of the neutral context above for
        // emphasis. The dirty warning mirrors the remove guard; dirtiness is
        // orthogonal to mergedness, so a merged-but-dirty tree still warns.
        let guard = crate::worktree_service::guard_status(worktree, app.remove_untracked_blocks);
        if guard.dirty {
            lines.push(Line::from(Span::styled(
                "(has uncommitted changes — data may be lost)",
                theme.error(),
            )));
        }
        // The unpushed message is driven by the offline merge state so a branch
        // that was simply merged (into its base/default, or via a PR) is not
        // flagged as alarming "unpushed work" (spec §10). A confirmed merge
        // suppresses the unpushed warning entirely.
        if let Some(line) = merge_state_note(worktree, &theme, true) {
            lines.push(line);
        }
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("Remove this worktree? ["),
        Span::styled("y", theme.warning()),
        Span::raw("/N]"),
    ]));

    // Size to the content (plus the top/bottom border); `centered` clamps to the
    // available area, and `Wrap` keeps long paths/subjects/titles from overflowing.
    let height = lines.len() as u16 + 2;
    let rect = centered(area, 72, height);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("confirm remove", theme.error())))
            .wrap(Wrap { trim: false }),
        rect,
    );
}

/// Renders the confirm-create dialog for a worktree-less branch row (issue #47):
/// the branch, its base and ahead/behind, and the tip commit — enough to decide —
/// then a prompt to create a worktree and switch into it.
pub(super) fn render_confirm_create(app: &App, index: usize, frame: &mut Frame, area: Rect) {
    let theme = Theme::with_palette(app.color, app.palette);
    let Some(worktree) = app.worktrees.get(index) else {
        return;
    };
    let now = now_unix();
    let glyphs = Glyphs::new(app.nerd_fonts);
    let loaded = app.is_loaded(worktree);

    let branch_span = Span::styled(branch_display(worktree), theme.branch(false, false));
    let mut lines = vec![Line::from(vec![
        Span::styled("branch: ", theme.label()),
        branch_span,
    ])];
    if let Some(base) = &worktree.base_ref {
        lines.push(Line::from(vec![
            Span::styled("base:   ", theme.label()),
            Span::raw(base.clone()),
        ]));
    }
    if loaded {
        let mut spans = vec![Span::styled("vs base: ", theme.label())];
        spans.extend(ahead_behind_spans(worktree, &theme, true, &glyphs));
        lines.push(Line::from(spans));
        if let Some(c) = &worktree.commit {
            let rel = parse_iso8601(&c.timestamp)
                .map(|u| relative(now, u))
                .unwrap_or_default();
            lines.push(Line::from(vec![
                Span::styled("commit: ", theme.label()),
                Span::styled(c.hash.clone(), theme.commit_hash()),
                Span::raw(" "),
                Span::raw(c.subject.clone()),
                Span::raw(" "),
                Span::styled(format!("({rel})"), theme.time()),
            ]));
        }
    } else {
        lines.push(Line::from(vec![
            Span::styled("vs base: ", theme.label()),
            Span::styled("", theme.spinner()),
        ]));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("Create a worktree and switch into it? ["),
        Span::styled("y", theme.success()),
        Span::raw("/N]"),
    ]));

    let height = lines.len() as u16 + 2;
    let rect = centered(area, 72, height);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("create worktree", theme.title(true))))
            .wrap(Wrap { trim: false }),
        rect,
    );
}

/// Renders the confirm-delete dialog for a worktree-less branch row (issue #53):
/// the branch and its tip, then a prompt to delete the local branch. On the
/// `force` re-prompt — after a safe `git branch -d` refused an unmerged branch —
/// the prompt warns that deleting it may discard commits.
pub(super) fn render_confirm_delete_branch(
    app: &App,
    index: usize,
    force: bool,
    frame: &mut Frame,
    area: Rect,
) {
    let theme = Theme::with_palette(app.color, app.palette);
    let Some(worktree) = app.worktrees.get(index) else {
        return;
    };
    let now = now_unix();
    let glyphs = Glyphs::new(app.nerd_fonts);
    let loaded = app.is_loaded(worktree);

    let branch_span = Span::styled(branch_display(worktree), theme.branch(false, false));
    let mut lines = vec![Line::from(vec![
        Span::styled("branch: ", theme.label()),
        branch_span,
    ])];
    if let Some(base) = &worktree.base_ref {
        lines.push(Line::from(vec![
            Span::styled("base:   ", theme.label()),
            Span::raw(base.clone()),
        ]));
    }
    if loaded {
        let mut spans = vec![Span::styled("vs base: ", theme.label())];
        spans.extend(ahead_behind_spans(worktree, &theme, true, &glyphs));
        lines.push(Line::from(spans));
        if let Some(c) = &worktree.commit {
            let rel = parse_iso8601(&c.timestamp)
                .map(|u| relative(now, u))
                .unwrap_or_default();
            lines.push(Line::from(vec![
                Span::styled("commit: ", theme.label()),
                Span::styled(c.hash.clone(), theme.commit_hash()),
                Span::raw(" "),
                Span::raw(c.subject.clone()),
                Span::raw(" "),
                Span::styled(format!("({rel})"), theme.time()),
            ]));
        }
    } else {
        lines.push(Line::from(vec![
            Span::styled("vs base: ", theme.label()),
            Span::styled("", theme.spinner()),
        ]));
    }

    lines.push(Line::from(""));
    if force {
        lines.push(Line::from(Span::styled(
            "(branch is not fully merged — deleting may discard commits)",
            theme.error(),
        )));
        lines.push(Line::from(vec![
            Span::raw("Force-delete this branch? ["),
            Span::styled("y", theme.warning()),
            Span::raw("/N]"),
        ]));
    } else {
        lines.push(Line::from(vec![
            Span::raw("Delete this branch? ["),
            Span::styled("y", theme.warning()),
            Span::raw("/N]"),
        ]));
    }

    let height = lines.len() as u16 + 2;
    let rect = centered(area, 72, height);
    frame.render_widget(Clear, rect);
    let title = if force {
        "force-delete branch"
    } else {
        "delete branch"
    };
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled(title, theme.error())))
            .wrap(Wrap { trim: false }),
        rect,
    );
}

/// Renders the stale-base confirm dialog (issue #56): the base a new worktree
/// would fork from is behind its upstream. Offers update / proceed / cancel; when
/// the base has diverged it notes that updating would fail.
pub(super) fn render_confirm_stale_base(
    app: &App,
    state: &StaleBaseState,
    frame: &mut Frame,
    area: Rect,
) {
    let theme = Theme::with_palette(app.color, app.palette);
    let mut lines = vec![
        Line::from(vec![
            Span::styled("new branch: ", theme.label()),
            Span::styled(state.branch.clone(), theme.branch(false, false)),
        ]),
        Line::from(vec![
            Span::styled("base:       ", theme.label()),
            Span::raw(state.base.clone().unwrap_or_else(|| "(default)".into())),
        ]),
        Line::from(vec![
            Span::styled("status:     ", theme.label()),
            Span::styled(
                format!(
                    "{} commit(s) behind {}",
                    state.behind, state.upstream_display
                ),
                theme.warning(),
            ),
        ]),
    ];
    if !state.can_fast_forward {
        lines.push(Line::from(Span::styled(
            "(base has diverged — update will fail; proceed or cancel)",
            theme.error(),
        )));
    }
    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::styled("u", theme.success()),
        Span::raw("pdate the base, "),
        Span::styled("p", theme.warning()),
        Span::raw("roceed off it, or cancel?"),
    ]));

    let height = lines.len() as u16 + 2;
    let rect = centered(area, 72, height);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(Block::bordered().title(Span::styled("base behind origin", theme.warning())))
            .wrap(Wrap { trim: false }),
        rect,
    );
}

/// Renders the submodule-init confirm dialog (issue #50): a freshly created
/// worktree has uninitialized submodules and the policy is left at its `prompt`
/// default. Offers to initialize them recursively, defaulting to yes.
pub(super) fn render_confirm_init_submodules(
    app: &App,
    state: &InitSubmodulesState,
    frame: &mut Frame,
    area: Rect,
) {
    let theme = Theme::with_palette(app.color, app.palette);
    let lines = vec![
        Line::from(vec![
            Span::styled("branch:     ", theme.label()),
            Span::styled(state.branch.clone(), theme.branch(false, false)),
        ]),
        Line::from(vec![
            Span::styled("submodules: ", theme.label()),
            Span::styled(format!("{} uninitialized", state.count), theme.warning()),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::raw("Initialize submodules recursively? ["),
            Span::styled("Y", theme.success()),
            Span::raw("/n]"),
        ]),
    ];

    let height = lines.len() as u16 + 2;
    let rect = centered(area, 72, height);
    frame.render_widget(Clear, rect);
    frame.render_widget(
        Paragraph::new(lines)
            .block(
                Block::bordered().title(Span::styled("initialize submodules", theme.title(true))),
            )
            .wrap(Wrap { trim: false }),
        rect,
    );
}