Pipe long diff output through less pager when it exceeds terminal height
This commit is contained in:
parent
9fc126d58f
commit
d4a3053bd7
22
src/cli.ts
22
src/cli.ts
|
|
@ -333,21 +333,37 @@ program
|
|||
process.exit(1)
|
||||
}
|
||||
|
||||
let diff: string
|
||||
if (status.text().trim().length > 0) {
|
||||
// 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) {
|
||||
// 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 {
|
||||
// No uncommitted changes — show full branch diff vs main
|
||||
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) {
|
||||
console.error("git diff failed")
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user