path-cli 0.13.0

CLI for deriving, querying, and visualizing Toolpath provenance (binary: path)
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
//! Interactive session selection backed by a fuzzy picker.
//!
//! The protocol:
//! - `path p list <provider> --format tsv` emits one session per line,
//!   tab-delimited. For single-keyed providers (codex, opencode) col 1
//!   is the session id. For project-keyed providers (claude, gemini,
//!   pi) col 1 is the project path and col 2 is the session id.
//!   Subsequent columns are display-only.
//! - `path show <provider>` emits a markdown summary for one session,
//!   suitable as the picker's `--preview` command.
//! - `path p import <provider>` (and `path share` / `path resume`) with
//!   no `--session` auto-launches the picker when stdin and stderr are
//!   TTYs. Multi-select produces a `Graph`.
//!
//! Two backends, selected at runtime:
//!
//! - **External `fzf`** (preferred when on `PATH`). Users keep their
//!   own fzf config and keybindings.
//! - **Embedded skim** (Rust fzf-clone, compiled in unless the
//!   `embedded-picker` feature is disabled). Same `{1}`/`{2}` preview
//!   placeholder grammar and the same `--with-nth`/`--tiebreak`
//!   notation, so existing call sites work unchanged.
//!
//! When neither backend can run (no TTY, or no external fzf with the
//! embedded picker disabled at build time), callers fall through to the
//! documented manual recipe printed by [`print_recipe`].
#![cfg(not(target_os = "emscripten"))]

use anyhow::{Context, Result};
use clap::ValueEnum;
use std::io::{IsTerminal, Write};
use std::process::{Command, Stdio};
use std::sync::OnceLock;

/// Backend override for the interactive fuzzy picker, set once at CLI
/// startup via the global `--picker` flag and read by [`pick`].
///
/// `Auto` (the default) preserves the historical behavior: external
/// `fzf` if on `PATH`, embedded skim otherwise. `Fzf` and `Skim` are
/// strict — they error out rather than silently using the other backend.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "lower")]
pub enum Picker {
    /// Pick whichever backend is available; prefer external `fzf`.
    #[default]
    Auto,
    /// Force the external `fzf` binary. Errors if it isn't on `PATH`.
    Fzf,
    /// Force the embedded skim picker. Errors if path-cli was built
    /// with `--no-default-features`.
    Skim,
}

static PICKER_OVERRIDE: OnceLock<Picker> = OnceLock::new();

/// Set the global picker preference. Called once at CLI startup from
/// the `--picker` flag handler. Subsequent calls are no-ops — the first
/// value wins, so library callers can't accidentally override the
/// user's explicit choice mid-run.
pub fn set_picker_override(picker: Picker) {
    let _ = PICKER_OVERRIDE.set(picker);
}

fn current_picker() -> Picker {
    PICKER_OVERRIDE.get().copied().unwrap_or_default()
}

/// Returns true when an interactive picker can actually run: stdin and
/// stderr are TTYs *and* at least one backend is available (external
/// `fzf` on `PATH`, or the embedded skim picker compiled in). Callers
/// use this as a guard before invoking [`pick`]; on `false` they print
/// the manual recipe via [`print_recipe`].
pub fn available() -> bool {
    if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
        return false;
    }
    embedded_picker_available() || external_fzf_available()
}

/// True when the external `fzf` binary is on `PATH`. Used by [`pick`]
/// to decide between the external picker and the embedded skim
/// fallback, and to tailor the manual-recipe message.
pub fn external_fzf_available() -> bool {
    which("fzf").is_some()
}

/// True when the embedded skim picker was compiled in. Always `true`
/// in the default build; `false` under `--no-default-features`.
#[cfg(feature = "embedded-picker")]
pub const fn embedded_picker_available() -> bool {
    true
}

#[cfg(not(feature = "embedded-picker"))]
pub const fn embedded_picker_available() -> bool {
    false
}

fn which(cmd: &str) -> Option<std::path::PathBuf> {
    let path = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path) {
        let candidate = dir.join(cmd);
        if candidate.is_file() {
            return Some(candidate);
        }
    }
    None
}

/// Replace tabs / newlines / carriage returns with a single space so a
/// value safely fits inside one TSV cell without breaking the row
/// structure or pushing content onto a new line.
pub(crate) fn tab_safe(s: &str) -> String {
    s.replace(['\t', '\n', '\r'], " ")
}

/// Pad `s` with trailing spaces to `width` *characters* (not bytes) so
/// the next column lines up predictably even with multibyte titles.
/// If `s` is longer than `width`, truncate and replace the last visible
/// character with `…` so the boundary is obvious.
pub(crate) fn pad_or_truncate(s: &str, width: usize) -> String {
    let count = s.chars().count();
    if count == width {
        s.to_string()
    } else if count < width {
        format!("{s}{}", " ".repeat(width - count))
    } else {
        let head: String = s.chars().take(width.saturating_sub(1)).collect();
        format!("{head}")
    }
}

/// Truncate `s` to `width` characters with a trailing `…` if needed.
/// No padding — used for the trailing column of a picker row where
/// the picker is free to horizontally scroll if the line overflows.
pub(crate) fn clip_chars(s: &str, width: usize) -> String {
    if s.chars().count() <= width {
        return s.to_string();
    }
    let head: String = s.chars().take(width.saturating_sub(1)).collect();
    format!("{head}")
}

/// Last two path segments of `p`, slash-joined. Just enough to
/// disambiguate `…/repo/.claude/worktrees/foo` from
/// `…/other-repo/main` in a picker without spending screen real
/// estate on the full absolute path.
pub(crate) fn project_short(p: &str) -> String {
    let trimmed = p.trim_end_matches('/');
    let parts: Vec<&str> = trimmed.rsplit('/').take(2).collect();
    if parts.is_empty() {
        return p.to_string();
    }
    let mut out: Vec<&str> = parts.into_iter().collect();
    out.reverse();
    out.join("/")
}

/// Right-align an item count with its unit (`" 875 msgs"`, `"  12 lines"`,
/// `"   4 entries"`). Providers count different things, so the unit is
/// caller-supplied — kept as a single helper rather than one per unit
/// to keep the per-provider call sites grep-able.
pub(crate) fn count(n: usize, unit: &str) -> String {
    format!("{n:>4} {unit}")
}

/// One visible picker column packed into a single fixed-width string,
/// so columns line up consistently across pickers. Terminal tab stops
/// produce ugly variable gaps; explicit space padding here avoids that.
///
/// Layout: `[{leading} ]{when:16}  {count}  [{project:28}]  {title}`.
///
/// - `leading` is an optional prefix (used by `path share` to inject a
///   cwd-match marker and a harness symbol; `None` for single-provider
///   `path p import <provider>` pickers where neither applies).
/// - `when` renders as `YYYY-MM-DD HH:MM` or a 16-char placeholder when
///   the provider doesn't have a timestamp on hand.
/// - `count` is supplied pre-formatted via [`count`] so the picker
///   doesn't need to know that codex counts lines and pi counts entries.
/// - `project` is optional — omitted in per-project views where the
///   project label would be redundant on every row.
/// - `title` is run through [`clean_for_picker_display`] to strip
///   Claude's slash-command and local-command-caveat envelopes.
pub(crate) fn render_row(
    leading: Option<&str>,
    when: Option<chrono::DateTime<chrono::Utc>>,
    count: &str,
    project: Option<&str>,
    title: &str,
) -> String {
    const PROJECT_WIDTH: usize = 28;
    const TITLE_MAX: usize = 96;
    let leading_segment = match leading {
        Some(s) => format!("{s} "),
        None => String::new(),
    };
    let when_str = when
        .map(|t| t.format("%Y-%m-%d %H:%M").to_string())
        .unwrap_or_else(|| "".to_string());
    let project_segment = match project {
        Some(p) => format!("{}  ", pad_or_truncate(&tab_safe(p), PROJECT_WIDTH)),
        None => String::new(),
    };
    let title_str = clip_chars(&clean_for_picker_display(title), TITLE_MAX);
    format!("{leading_segment}{when_str}  {count}  {project_segment}{title_str}")
}

/// Clean up a user prompt for display in a picker row.
///
/// Claude wraps the first user message of a session with XML envelopes
/// when the user invoked a slash command or a local `!cmd`. In their
/// raw form they're unreadable noise — a row like
/// `<command-message>biko</command-message> <command-name>/biko</command-name> <command-args>From origin/main…</command-args>`
/// pushes the actual intent off the end of the picker line. This helper
/// strips the envelope down to its human-readable kernel:
///
/// - Slash command envelopes render as `/<name> <args>`.
/// - `<local-command-caveat>…</local-command-caveat>` is dropped; if a
///   `<bash-input>…</bash-input>` follows, it renders as `! <cmd>`.
/// - Any other XML-like wrappers are stripped, keeping inner content.
///
/// The original prompt is preserved in the session file and shown in
/// full via the `path show` preview pane — this only affects the
/// truncated row text used for picker scanning.
pub(crate) fn clean_for_picker_display(s: &str) -> String {
    if let Some(out) = strip_slash_command_envelope(s) {
        return out;
    }
    if let Some(out) = strip_local_command_caveat(s) {
        return out;
    }
    strip_xml_tags(s)
}

/// Recognize `<command-message>X</command-message> <command-name>/X</command-name> <command-args>Y</command-args>`
/// and re-emit it as `/X Y`. Returns None if the envelope isn't present
/// so the caller can fall through to the next strategy.
fn strip_slash_command_envelope(s: &str) -> Option<String> {
    let name = extract_tag_content(s, "command-name")?;
    let args = extract_tag_content(s, "command-args").unwrap_or_default();
    // `name` already includes the leading `/` per Claude's format.
    let name = name.trim();
    let args = args.trim();
    if args.is_empty() {
        Some(name.to_string())
    } else {
        Some(format!("{name} {args}"))
    }
}

/// Recognize `<local-command-caveat>…</local-command-caveat>` blocks.
/// If a `<bash-input>cmd</bash-input>` follows, re-emit as `! cmd`; if
/// not, fall back to stripping the caveat block and returning what's
/// left. Returns None when no caveat is present.
fn strip_local_command_caveat(s: &str) -> Option<String> {
    if !s.contains("<local-command-caveat>") {
        return None;
    }
    if let Some(cmd) = extract_tag_content(s, "bash-input") {
        return Some(format!("! {}", cmd.trim()));
    }
    // No bash-input — strip the caveat block and return whatever's left.
    let stripped = remove_block(s, "<local-command-caveat>", "</local-command-caveat>");
    Some(strip_xml_tags(&stripped))
}

/// Extract the inner text of the first `<tag>…</tag>` in `s`.
fn extract_tag_content(s: &str, tag: &str) -> Option<String> {
    let open = format!("<{tag}>");
    let close = format!("</{tag}>");
    let start = s.find(&open)? + open.len();
    let end = s[start..].find(&close)?;
    Some(s[start..start + end].to_string())
}

/// Remove the substring from the first `start` marker through the next
/// `end` marker, inclusive. No-op when either marker isn't found.
fn remove_block(s: &str, start: &str, end: &str) -> String {
    let Some(open_idx) = s.find(start) else {
        return s.to_string();
    };
    let after_open = open_idx + start.len();
    let Some(close_off) = s[after_open..].find(end) else {
        return s.to_string();
    };
    let close_idx = after_open + close_off + end.len();
    format!("{}{}", &s[..open_idx], &s[close_idx..])
}

/// Strip all `<tag>` and `</tag>` markers, keeping the inner text.
/// Best-effort fallback for envelopes we don't have a structured
/// renderer for. Collapses consecutive whitespace afterward so the
/// resulting row doesn't have weird gaps.
fn strip_xml_tags(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut in_tag = false;
    for ch in s.chars() {
        match ch {
            '<' => in_tag = true,
            '>' if in_tag => in_tag = false,
            _ if !in_tag => out.push(ch),
            _ => {}
        }
    }
    // Collapse runs of whitespace.
    let mut collapsed = String::with_capacity(out.len());
    let mut prev_space = false;
    for ch in out.chars() {
        if ch.is_whitespace() {
            if !prev_space {
                collapsed.push(' ');
            }
            prev_space = true;
        } else {
            collapsed.push(ch);
            prev_space = false;
        }
    }
    collapsed.trim().to_string()
}

/// Substitute the `{exe}` placeholder in a preview command with the
/// shell-quoted absolute path of the currently-running binary.
///
/// Why this exists: previews used to hard-code `path show …`, which
/// resolves whatever `path` is first on `$PATH`. Users with an older
/// `path` from `cargo install path-cli` would see preview errors
/// (mismatched flags) instead of the rendered session. Substituting
/// the running binary makes the preview self-consistent — same code
/// runs in the picker, the preview, and the eventual import.
pub(crate) fn substitute_exe_placeholder(preview: &str) -> String {
    let exe = match std::env::current_exe() {
        Ok(p) => shell_quote(&p.to_string_lossy()),
        // Fallback to bare `path` if we can't locate ourselves — preserves
        // the previous behavior rather than blowing up the preview.
        Err(_) => "path".to_string(),
    };
    preview.replace("{exe}", &exe)
}

/// Single-quote a path for embedding in a `/bin/sh -c` command line.
/// Any embedded single-quote becomes `'\''` so the path survives intact.
fn shell_quote(s: &str) -> String {
    let escaped = s.replace('\'', "'\\''");
    format!("'{escaped}'")
}

/// Outcome of a picker invocation.
///
/// Distinguishes a deliberate user cancel (Esc / Ctrl-C, fzf exit 130)
/// from the no-match case (fzf exit 1). Callers that want to surface a
/// non-zero exit on cancel can match on `Cancelled`; callers that just
/// want the picked lines treat both `NoMatch` and `Cancelled` as "empty
/// selection".
pub enum PickResult {
    /// Picker exited cleanly with at least one selected line.
    Selected(Vec<String>),
    /// Picker exited cleanly with nothing matched / nothing selected.
    NoMatch,
    /// User pressed Esc / Ctrl-C / Ctrl-D.
    Cancelled,
}

/// Run the fuzzy picker with the supplied lines and options. Honors
/// the global `--picker` override; defaults to `Auto`, which prefers
/// the external `fzf` binary when it's on `PATH` (so users keep their
/// own fzf config / keybindings) and falls back to the embedded skim
/// picker otherwise. Errors out only when the requested backend isn't
/// available — callers should check [`available`] first.
pub fn pick(lines: &[String], opts: &PickOptions<'_>) -> Result<PickResult> {
    match current_picker() {
        Picker::Fzf => {
            if !external_fzf_available() {
                anyhow::bail!(
                    "`--picker fzf` requested but `fzf` isn't on PATH; install it or pass `--picker auto`/`skim`"
                );
            }
            pick_external(lines, opts)
        }
        Picker::Skim => pick_embedded(lines, opts),
        Picker::Auto => {
            if external_fzf_available() {
                pick_external(lines, opts)
            } else {
                pick_embedded(lines, opts)
            }
        }
    }
}

/// Invoke the embedded skim picker. Compiled-out under
/// `--no-default-features`, in which case calling this returns a clear
/// error rather than the cryptic "no backend" surface.
#[cfg(feature = "embedded-picker")]
fn pick_embedded(lines: &[String], opts: &PickOptions<'_>) -> Result<PickResult> {
    crate::skim_picker::pick(lines, opts)
}

#[cfg(not(feature = "embedded-picker"))]
fn pick_embedded(_lines: &[String], _opts: &PickOptions<'_>) -> Result<PickResult> {
    anyhow::bail!(
        "embedded skim picker isn't compiled in (built with `--no-default-features`); install `fzf` and pass `--picker fzf` or rebuild with the default `embedded-picker` feature"
    )
}

/// Run the external `fzf` binary directly. Same surface as [`pick`] —
/// kept as a dedicated path so call sites that *must* hit external fzf
/// (e.g. integration tests pinning behavior) can opt in.
pub fn pick_external(lines: &[String], opts: &PickOptions<'_>) -> Result<PickResult> {
    let mut args: Vec<String> = vec![
        "--delimiter=\t".into(),
        format!("--with-nth={}", opts.with_nth),
        format!("--prompt={}", opts.prompt),
        format!("--tiebreak={}", opts.tiebreak),
    ];

    if opts.multi {
        args.push("--multi".into());
    }

    if let Some(preview) = opts.preview {
        args.push(format!("--preview={}", substitute_exe_placeholder(preview)));
        args.push(format!("--preview-window={}", opts.preview_window));
        args.push("--preview-wrap-sign=".into());
    }

    if let Some(header) = opts.header {
        args.push(format!("--header={}", header));
    }

    let mut child = Command::new("fzf")
        .args(&args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()
        .context("Failed to spawn fzf")?;

    {
        let stdin = child
            .stdin
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("Failed to open fzf stdin"))?;
        for line in lines {
            stdin.write_all(line.as_bytes())?;
            stdin.write_all(b"\n")?;
        }
    }

    let output = child.wait_with_output().context("Failed to wait on fzf")?;

    // fzf exit codes: 0 = match, 1 = no match, 2 = error, 130 = cancelled.
    match output.status.code() {
        Some(0) => {
            let text = String::from_utf8_lossy(&output.stdout);
            Ok(PickResult::Selected(
                text.lines().map(|s| s.to_string()).collect(),
            ))
        }
        Some(1) => Ok(PickResult::NoMatch),
        Some(130) => Ok(PickResult::Cancelled),
        _ => anyhow::bail!("fzf exited with status {:?}", output.status),
    }
}

/// Options for a picker invocation. Field names mirror fzf's flags;
/// the embedded skim backend maps them onto its equivalents.
pub struct PickOptions<'a> {
    /// Visible columns in fzf's notation, e.g. `2..` to hide col 1.
    pub with_nth: &'a str,
    /// Prompt string shown to the left of the input.
    pub prompt: &'a str,
    /// Optional `--preview` command. Use `{1}`, `{2}` ... to substitute fields.
    pub preview: Option<&'a str>,
    /// `--preview-window` placement. Defaults to `right:60%:wrap` (side-by-side);
    /// pass `up:60%:wrap` for a stacked layout that fits narrow terminals.
    pub preview_window: &'a str,
    /// Optional header line shown above the list.
    pub header: Option<&'a str>,
    /// Tiebreak ordering — `index` preserves input order.
    pub tiebreak: &'a str,
    /// Allow selecting multiple rows.
    pub multi: bool,
}

impl Default for PickOptions<'_> {
    fn default() -> Self {
        Self {
            with_nth: "2..",
            prompt: "> ",
            preview: None,
            preview_window: "right:60%:wrap-word",
            header: None,
            tiebreak: "index",
            multi: false,
        }
    }
}

/// Print a manual recipe for users who can't get an interactive picker.
/// Reached when stdin/stderr aren't TTYs, or — under
/// `--no-default-features` — when external `fzf` is also missing.
/// `provider` is the subcommand name; `project_keyed` toggles the
/// column layout.
pub fn print_recipe(provider: &str, project_keyed: bool) {
    let leader = if !external_fzf_available() && !embedded_picker_available() {
        "Interactive selection needs `fzf` on PATH (or a build with the \
         `embedded-picker` feature) and a TTY."
    } else {
        "Interactive selection needs a TTY."
    };

    if project_keyed {
        eprintln!(
            "{leader}\n\
             \n\
             Manual recipe:\n  \
             path p list {provider} --format tsv \\\n    \
             | fzf --delimiter=$'\\t' --with-nth=3.. \\\n          \
             --preview 'path show {provider} --project {{1}} --session {{2}}' \\\n    \
             | awk -F'\\t' '{{print $1 \"\\n\" $2}}' \\\n    \
             | xargs -L2 sh -c 'path p import {provider} --project \"$1\" --session \"$2\"' --\n"
        );
    } else {
        eprintln!(
            "{leader}\n\
             \n\
             Manual recipe:\n  \
             path p list {provider} --format tsv \\\n    \
             | fzf --delimiter=$'\\t' --with-nth=2.. \\\n          \
             --preview 'path show {provider} --session {{1}}' \\\n    \
             | cut -f1 \\\n    \
             | xargs -I{{}} path p import {provider} --session {{}}\n"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shell_quote_wraps_in_single_quotes() {
        assert_eq!(shell_quote("/usr/local/bin/path"), "'/usr/local/bin/path'");
    }

    #[test]
    fn shell_quote_escapes_embedded_single_quote() {
        // A path containing a single quote becomes `'…'\''…'` so the
        // shell reassembles the literal correctly.
        assert_eq!(shell_quote("/o'reilly/bin"), "'/o'\\''reilly/bin'");
    }

    #[test]
    fn substitute_exe_replaces_placeholder() {
        // We don't pin the substituted value (it's `current_exe()` at
        // runtime), but we can confirm the placeholder is consumed and
        // the surrounding template survives.
        let out = substitute_exe_placeholder("{exe} show --ansi claude --session {1}");
        assert!(!out.contains("{exe}"), "placeholder not substituted: {out}");
        assert!(out.contains(" show --ansi claude --session {1}"));
    }

    #[test]
    fn substitute_exe_leaves_other_placeholders_alone() {
        // `{1}`, `{2}` etc. are the picker's column placeholders —
        // they must survive the substitution untouched.
        let out = substitute_exe_placeholder("{exe} show --session {1} --project {2}");
        assert!(out.contains("{1}"));
        assert!(out.contains("{2}"));
    }

    use chrono::TimeZone;

    fn jan_first() -> chrono::DateTime<chrono::Utc> {
        chrono::Utc.with_ymd_and_hms(2026, 1, 29, 10, 5, 0).unwrap()
    }

    /// Bare row — no leading prefix, no project, real timestamp.
    /// Pin the exact output so a regression in spacing or column order
    /// shows up as a diff on this test.
    #[test]
    fn render_row_bare_matches_expected_format() {
        let out = render_row(None, Some(jan_first()), "  17 msgs", None, "hello world");
        // {when:16}␣␣{count}␣␣{title}
        assert_eq!(out, "2026-01-29 10:05    17 msgs  hello world");
    }

    /// `path share` injects a scope marker + harness symbol as the
    /// `leading` prefix; assert it's separated from the timestamp by
    /// exactly one space so the layout is grep-able.
    #[test]
    fn render_row_with_leading_prefix_separates_with_one_space() {
        let out = render_row(
            Some("· claude  "),
            Some(jan_first()),
            "  17 msgs",
            None,
            "hi",
        );
        assert!(out.starts_with("· claude   2026-01-29 10:05"), "{out}");
    }

    /// Optional project segment pads to PROJECT_WIDTH (28 chars) so
    /// the title column starts at the same offset on every row.
    #[test]
    fn render_row_pads_project_to_fixed_width() {
        let short = render_row(None, Some(jan_first()), "  17 msgs", Some("a/b"), "t");
        let long = render_row(
            None,
            Some(jan_first()),
            "  17 msgs",
            Some("a-much-longer-name"),
            "t",
        );
        let title_offset = |s: &str| s.find("t").and_then(|_| s.rfind("  t")).unwrap();
        assert_eq!(
            title_offset(&short),
            title_offset(&long),
            "title misaligned across rows:\n short={short:?}\n long={long:?}"
        );
    }

    /// Project longer than PROJECT_WIDTH is truncated with a `…`
    /// sentinel; padding never makes a row wider than the budget.
    #[test]
    fn render_row_truncates_oversized_project_with_ellipsis() {
        let long = "x".repeat(40);
        let out = render_row(None, Some(jan_first()), "  17 msgs", Some(&long), "t");
        // The project segment renders 28 chars wide then `  ` separator.
        // Find the literal "  t" that follows it.
        let project_segment = out
            .strip_prefix("2026-01-29 10:05    17 msgs  ")
            .unwrap()
            .strip_suffix("  t")
            .unwrap();
        assert_eq!(project_segment.chars().count(), 28);
        assert!(project_segment.ends_with(''), "got: {project_segment:?}");
    }

    /// Missing timestamp renders a 16-char placeholder so subsequent
    /// columns still align with timestamped rows. Compare via *char*
    /// position — the placeholder uses an em dash which is 3 bytes but
    /// 1 char, so byte offsets would diverge despite visual alignment.
    #[test]
    fn render_row_missing_when_renders_placeholder_at_fixed_width() {
        let with = render_row(None, Some(jan_first()), "  17 msgs", None, "t");
        let without = render_row(None, None, "  17 msgs", None, "t");
        let char_offset = |s: &str| {
            s.chars()
                .collect::<Vec<_>>()
                .windows(7)
                .position(|w| w.iter().collect::<String>() == "17 msgs")
        };
        assert_eq!(char_offset(&with), char_offset(&without));
    }

    /// Long titles get clipped with a `…` sentinel; the surrounding
    /// columns are untouched.
    #[test]
    fn render_row_clips_long_title_with_ellipsis() {
        let title = "x".repeat(200);
        let out = render_row(None, Some(jan_first()), "  17 msgs", None, &title);
        let title_part = out.strip_prefix("2026-01-29 10:05    17 msgs  ").unwrap();
        // TITLE_MAX = 96; ellipsis takes one slot.
        assert_eq!(title_part.chars().count(), 96);
        assert!(title_part.ends_with(''), "got: {title_part:?}");
    }

    /// Slash-command envelopes are stripped from the title via
    /// `clean_for_picker_display` before truncation. Belt-and-suspenders
    /// regression guard for the row noise the picker rows used to show.
    #[test]
    fn render_row_strips_slash_command_envelope_from_title() {
        let raw = "<command-message>biko</command-message> <command-name>/biko</command-name> <command-args>do the thing</command-args>";
        let out = render_row(None, Some(jan_first()), "  17 msgs", None, raw);
        assert!(out.ends_with("/biko do the thing"), "got: {out}");
        assert!(!out.contains("<command-"));
    }

    /// `count` right-aligns the number to 4 chars so the unit column
    /// stays in the same place across rows with different magnitudes.
    #[test]
    fn count_right_aligns_number_to_four_chars() {
        assert_eq!(count(7, "msgs"), "   7 msgs");
        assert_eq!(count(1572, "msgs"), "1572 msgs");
        assert_eq!(count(12, "entries"), "  12 entries");
    }
}