Skip to content

Commit dbf5884

Browse files
committed
fix(tui): don't count hunk headers as patch rows
a file with exactly 10 changed lines counted as 11 and got a hint that did nothing
1 parent bd168eb commit dbf5884

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎packages/tui/src/util/collapse-tool-output.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ export function collapseToolOutput(output: string, maxLines: number, maxChars: n
2323
return { output: preview, overflow: true }
2424
}
2525

26-
/** Lines of a patch without its file headers, which is what a unified diff view renders. */
26+
/** Lines of a patch without its file and hunk headers, which is what a unified diff view renders. */
2727
export function patchBodyLines(patch: string) {
2828
const start = patch.search(/^@@ /m)
29-
return start === -1 ? 0 : patch.slice(start).trimEnd().split("\n").length
29+
if (start === -1) return 0
30+
return patch
31+
.slice(start)
32+
.trimEnd()
33+
.split("\n")
34+
.filter((line) => !line.startsWith("@@") && !line.startsWith("\\")).length
3035
}
3136

3237
export function collapseShellOutput(input: string, output: string, maxLines: number, maxChars: number) {

‎packages/tui/test/cli/tui/collapse-tool-output.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test("limits command input and output to the same line budget", () => {
1616
test("counts patch lines without the file headers", () => {
1717
const patch = ["Index: a.ts", "===", "--- a.ts", "+++ a.ts", "@@ -1,2 +1,2 @@", "-one", "+uno", " two", ""].join("\n")
1818

19-
expect(patchBodyLines(patch)).toBe(4)
19+
expect(patchBodyLines(patch)).toBe(3)
20+
expect(patchBodyLines(`${patch.trimEnd()}\n@@ -9 +9 @@\n-x\n+y\n\\ No newline at end of file`)).toBe(5)
2021
expect(patchBodyLines("Index: a.ts\n")).toBe(0)
2122
})

0 commit comments

Comments
 (0)