toes diff improvements

This commit is contained in:
Chris Wanstrath 2026-02-09 21:40:48 -08:00
parent 891b08ecd8
commit 115d3199e8

View File

@ -886,12 +886,28 @@ function showDiff(remote: string, local: string) {
let lineCount = 0 let lineCount = 0
const maxLines = 50 const maxLines = 50
const contextLines = 3 const contextLines = 3
let remoteLine = 1
let localLine = 1
let needsHeader = true
let hunkCount = 0
const printHeader = (_rStart: number, lStart: number) => {
if (hunkCount > 0) console.log()
if (lStart > 1) {
console.log(color.cyan(`Line ${lStart}:`))
lineCount++
}
needsHeader = false
hunkCount++
}
for (let i = 0; i < changes.length; i++) { for (let i = 0; i < changes.length; i++) {
const part = changes[i]! const part = changes[i]!
const lines = part.value.replace(/\n$/, '').split('\n') const lines = part.value.replace(/\n$/, '').split('\n')
if (part.added) { if (part.added) {
if (needsHeader) printHeader(remoteLine, localLine)
for (const line of lines) { for (const line of lines) {
if (lineCount >= maxLines) { if (lineCount >= maxLines) {
console.log(color.gray('... diff truncated')) console.log(color.gray('... diff truncated'))
@ -900,7 +916,9 @@ function showDiff(remote: string, local: string) {
console.log(color.green(`+ ${line}`)) console.log(color.green(`+ ${line}`))
lineCount++ lineCount++
} }
localLine += lines.length
} else if (part.removed) { } else if (part.removed) {
if (needsHeader) printHeader(remoteLine, localLine)
for (const line of lines) { for (const line of lines) {
if (lineCount >= maxLines) { if (lineCount >= maxLines) {
console.log(color.gray('... diff truncated')) console.log(color.gray('... diff truncated'))
@ -909,6 +927,7 @@ function showDiff(remote: string, local: string) {
console.log(color.red(`- ${line}`)) console.log(color.red(`- ${line}`))
lineCount++ lineCount++
} }
remoteLine += lines.length
} else { } else {
// Context: show lines near changes // Context: show lines near changes
const prevHasChange = i > 0 && (changes[i - 1]!.added || changes[i - 1]!.removed) const prevHasChange = i > 0 && (changes[i - 1]!.added || changes[i - 1]!.removed)
@ -929,9 +948,11 @@ function showDiff(remote: string, local: string) {
if (nextHasChange) { if (nextHasChange) {
const start = Math.max(0, lines.length - contextLines) const start = Math.max(0, lines.length - contextLines)
if (start > 0) { if (start > 0) {
console.log(color.gray(' ...')) needsHeader = true
lineCount++
} }
const headerLine = remoteLine + start
const headerLocalLine = localLine + start
if (needsHeader) printHeader(headerLine, headerLocalLine)
for (let j = start; j < lines.length; j++) { for (let j = start; j < lines.length; j++) {
if (lineCount >= maxLines) { if (lineCount >= maxLines) {
console.log(color.gray('... diff truncated')) console.log(color.gray('... diff truncated'))
@ -942,7 +963,7 @@ function showDiff(remote: string, local: string) {
} }
} }
// Show context after previous change // Show context after previous change
if (prevHasChange) { if (prevHasChange && !nextHasChange) {
const end = Math.min(lines.length, contextLines) const end = Math.min(lines.length, contextLines)
for (let j = 0; j < end; j++) { for (let j = 0; j < end; j++) {
if (lineCount >= maxLines) { if (lineCount >= maxLines) {
@ -952,11 +973,13 @@ function showDiff(remote: string, local: string) {
console.log(color.gray(` ${lines[j]}`)) console.log(color.gray(` ${lines[j]}`))
lineCount++ lineCount++
} }
if (end < lines.length && !nextHasChange) { if (end < lines.length) {
console.log(color.gray(' ...')) needsHeader = true
} }
} }
} }
remoteLine += lines.length
localLine += lines.length
} }
} }
} }