Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(picker): cap the preview pane at a line count skim can represent (#3959)

Worktrunk Botbe7839f289c4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f71e7324d..9543d0815 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,8 @@
### Fixed
+- **The `wt switch` picker survives a preview longer than 65,535 lines**: skim keeps the preview pane's line count in a `u16`, so a branch whose diff against the default branch ran past that aborted `wt` with exit 101 a few seconds after the picker painted — on screen, the picker appeared to close by itself. Preview panes now cap at 60,000 lines and say so above the pane. ([#3959](https://github.com/max-sixty/worktrunk/pull/3959), fixes [#3958](https://github.com/max-sixty/worktrunk/issues/3958), thanks @sandertammesoo for reporting and diagnosing)
+
- **`wt list` no longer grows `.git/objects` during advisory conflict checks**: untracked content stays visible but is excluded from synthetic trees, and all probe-only objects use temporary storage. ([#3906](https://github.com/max-sixty/worktrunk/pull/3906), fixes [#3883](https://github.com/max-sixty/worktrunk/issues/3883), thanks @srobroek for reporting and the original fix)
- **`HEAD±` counts untracked files without inflating moves**: `wt list`, the picker, and statusline include untracked lines. Tracked deletions paired with untracked destinations count as renames, so pure moves are line-neutral and edited moves show only their edits. `HEAD±` now always detects renames, regardless of `diff.renames`. ([#3925](https://github.com/max-sixty/worktrunk/pull/3925))
diff --git a/src/commands/picker/items.rs b/src/commands/picker/items.rs
index 210d38db7..6b5e3871f 100644
--- a/src/commands/picker/items.rs
+++ b/src/commands/picker/items.rs
@@ -908,6 +908,41 @@ struct Tab {
has_content: bool,
}
+/// Longest preview body handed to skim, in lines.
+///
+/// skim keeps the pane's line count in a `u16` and `unwrap()`s the conversion
+/// (`total_lines` in skim 5.6.5's `src/tui/preview.rs`), so a body over 65,535
+/// lines aborts the whole process instead of rendering — the picker paints,
+/// then dies once the preview lands (#3958). A `git diff <default>...<branch>`
+/// on a long-lived branch clears that on its own. The cap sits under the
+/// ceiling rather than at it because the body isn't the whole pane: the tab bar
+/// and this notice ride above it and count toward the same total.
+const MAX_PREVIEW_LINES: usize = 60_000;
+
+/// Cap a preview body at [`MAX_PREVIEW_LINES`], returning the body to render
+/// and, when it was cut, the notice that says so.
+///
+/// The notice goes above the body rather than at the cut, where 60,000 lines of
+/// scrolling separate it from the reader.
+fn cap_preview_lines(mut body: String) -> (String, Option<String>) {
+ // The line that ends the budget: everything through its newline is kept.
+ let Some((last_newline, _)) = body.match_indices('\n').nth(MAX_PREVIEW_LINES - 1) else {
+ return (body, None);
+ };
+ let keep = last_newline + 1;
+ if keep == body.len() {
+ // Exactly the budget, newline-terminated — nothing to cut, nothing to say.
+ return (body, None);
+ }
+ body.truncate(keep);
+ (
+ body,
+ Some(cformat!(
+ "{INFO_SYMBOL} <dim>Preview truncated to {MAX_PREVIEW_LINES} lines</>\n"
+ )),
+ )
+}
+
/// Render the preview tab bar, shared by worktree rows and `--prs` rows.
///
/// Every full-form tab keeps its `N: label` text — only the formatting varies,
@@ -1085,8 +1120,7 @@ impl PickerRow {
/// rather than the process-wide picker mode.
fn render_preview(&self, mode: PreviewMode, width: usize, height: usize) -> String {
let avail = self.tab_availability();
- let mut result = render_preview_tabs(mode, avail, width);
- result.push_str(&match mode {
+ let body = match mode {
// The PR-backed tabs read the same `pr_status` slot for both row
// kinds: `pr` renders from it, `comments` reads the background-fetched
// thread when the row has a PR (always, for a `--prs` row).
@@ -1100,7 +1134,15 @@ impl PickerRow {
Some(_) => self.preview_for_mode(mode, width, height),
None => self.render_listed_pr_mode(mode),
},
- });
+ };
+ // Every pane converges here, so the cap sits here too — a `comments`
+ // thread or a forge `log` reaches skim by the same route a diff does.
+ let (body, notice) = cap_preview_lines(body);
+ let mut result = render_preview_tabs(mode, avail, width);
+ if let Some(notice) = notice {
+ result.push_str(&notice);
+ }
+ result.push_str(&body);
result
}
@@ -2842,6 +2884,59 @@ mod tests {
);
}
+ #[test]
+ fn preview_caps_a_pane_longer_than_skim_can_count() {
+ // skim keeps the pane's line count in a `u16` and unwraps the
+ // conversion, so handing it a longer pane aborts the picker rather than
+ // rendering (#3958). A `git diff <default>...<branch>` on a long-lived
+ // branch clears 65,535 lines on its own.
+ let cache: PreviewCache = Arc::new(DashMap::new());
+ let row = worktree_test_row("feature", Arc::clone(&cache), None);
+ cache.insert(
+ (row.preview_key(), PreviewMode::BranchDiff),
+ "+ a line of diff\n".repeat(70_000),
+ );
+
+ let pane = row.render_preview(PreviewMode::BranchDiff, WIDE, 24);
+ let lines = pane.lines().count();
+ // The pane opens with the tab bar, so the notice is a few lines in; the
+ // head is what an assertion failure needs to show (the body is a
+ // megabyte of diff).
+ let head = pane.lines().take(6).collect::<Vec<_>>().join("\n");
+ assert!(
+ lines < u16::MAX as usize,
+ "pane must stay countable by skim's u16: {lines} lines"
+ );
+ assert!(
+ head.contains("Preview truncated"),
+ "truncation is stated above the pane: {head:?}"
+ );
+
+ // A pane under the cap is handed through whole, notice-free.
+ cache.insert(
+ (row.preview_key(), PreviewMode::BranchDiff),
+ "+ a line of diff\n".repeat(10),
+ );
+ let short = row.render_preview(PreviewMode::BranchDiff, WIDE, 24);
+ assert!(
+ !short.contains("Preview truncated"),
+ "no notice under the cap: {short:?}"
+ );
+
+ // Exactly the budget, newline-terminated: the cut point is the end of
+ // the body, so nothing is dropped and nothing is claimed.
+ cache.insert(
+ (row.preview_key(), PreviewMode::BranchDiff),
+ "+ a line of diff\n".repeat(MAX_PREVIEW_LINES),
+ );
+ let exact = row.render_preview(PreviewMode::BranchDiff, WIDE, 24);
+ let exact_head = exact.lines().take(6).collect::<Vec<_>>().join("\n");
+ assert!(
+ !exact_head.contains("Preview truncated"),
+ "no notice at the cap: {exact_head:?}"
+ );
+ }
+
#[test]
fn pr_pane_is_memoized_until_the_slot_is_invalidated() {
use crate::commands::list::ci_status::{CiSource, CiStatus, PrRef, ReviewState};