Pipe long diff output through less pager when it exceeds terminal height

This commit is contained in:
Chris Wanstrath 2026-02-19 10:57:58 -08:00
parent 9fc126d58f
commit d4a3053bd7

View File

@ -333,21 +333,37 @@ program
process.exit(1) process.exit(1)
} }
let diff: string
if (status.text().trim().length > 0) { if (status.text().trim().length > 0) {
// Show uncommitted changes (both staged and unstaged) // Show uncommitted changes (both staged and unstaged)
const result = await $`git -C ${session.worktree} diff --color=always HEAD`.nothrow() const result = await $`git -C ${session.worktree} diff --color=always HEAD`.nothrow().quiet()
if (result.exitCode !== 0) { if (result.exitCode !== 0) {
// HEAD may not exist yet (no commits); fall back to showing all tracked + untracked // HEAD may not exist yet (no commits); fall back to showing all tracked + untracked
await $`git -C ${session.worktree} diff --color=always`.nothrow() const fallback = await $`git -C ${session.worktree} diff --color=always`.nothrow().quiet()
diff = fallback.text()
} else {
diff = result.text()
} }
} else { } else {
// No uncommitted changes — show full branch diff vs main // No uncommitted changes — show full branch diff vs main
const main = await git.mainBranch(root) const main = await git.mainBranch(root)
const result = await $`git -C ${session.worktree} diff --color=always ${main}...${branch}`.nothrow() const result = await $`git -C ${session.worktree} diff --color=always ${main}...${branch}`.nothrow().quiet()
if (result.exitCode !== 0) { if (result.exitCode !== 0) {
console.error("git diff failed") console.error("git diff failed")
process.exit(1) process.exit(1)
} }
diff = result.text()
}
const lines = diff.split("\n").length
const termHeight = process.stdout.rows || 24
if (lines > termHeight) {
const pager = Bun.spawn(["less", "-R"], { stdin: "pipe", stdout: "inherit", stderr: "inherit" })
pager.stdin.write(diff)
pager.stdin.end()
await pager.exited
} else {
process.stdout.write(diff)
} }
}) })