fix: bg color for diff

This commit is contained in:
Nate 2025-07-14 00:54:49 -07:00
parent 12eb0d3d30
commit 63c9ecf24f
2 changed files with 15 additions and 54 deletions

View file

@ -1,57 +1,18 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { createTwoFilesPatch } from "diff";
import { Tool } from "./types.js"; import { Tool } from "./types.js";
function generateDiff(oldContent: string, newContent: string, filePath: string): string { function generateDiff(oldContent: string, newContent: string, filePath: string): string {
const oldLines = oldContent.split('\n'); return createTwoFilesPatch(
const newLines = newContent.split('\n'); filePath,
filePath,
let diff = `--- ${filePath}\n+++ ${filePath}\n`; oldContent,
newContent,
// Simple line-by-line diff undefined,
const maxLines = Math.max(oldLines.length, newLines.length); undefined,
let hunkStart = -1; { context: 3 }
let hunkOldCount = 0; );
let hunkNewCount = 0;
let hunkLines: string[] = [];
for (let i = 0; i < maxLines; i++) {
const oldLine = oldLines[i] || '';
const newLine = newLines[i] || '';
if (oldLine !== newLine) {
if (hunkStart === -1) {
hunkStart = i;
}
if (i < oldLines.length) {
hunkLines.push(`-${oldLine}`);
hunkOldCount++;
}
if (i < newLines.length) {
hunkLines.push(`+${newLine}`);
hunkNewCount++;
}
} else {
if (hunkStart !== -1) {
// End of hunk
diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`;
diff += hunkLines.join('\n') + '\n';
hunkStart = -1;
hunkOldCount = 0;
hunkNewCount = 0;
hunkLines = [];
}
}
}
// Handle final hunk
if (hunkStart !== -1) {
diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`;
diff += hunkLines.join('\n') + '\n';
}
return diff;
} }
export const writeFileTool: Tool = { export const writeFileTool: Tool = {

View file

@ -83,19 +83,19 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent })
{displayableLines.map((line, index) => { {displayableLines.map((line, index) => {
const lineKey = `diff-line-${index}`; const lineKey = `diff-line-${index}`;
let gutterNumStr = ''; let gutterNumStr = '';
let color: string | undefined = undefined;
let prefixSymbol = ' '; let prefixSymbol = ' ';
let dim = false; let dim = false;
let backgroundColor: string | undefined = undefined;
switch (line.type) { switch (line.type) {
case 'add': case 'add':
gutterNumStr = (line.newLine ?? '').toString(); gutterNumStr = (line.newLine ?? '').toString();
color = 'green'; backgroundColor = '#1a4a1a';
prefixSymbol = '+'; prefixSymbol = '+';
break; break;
case 'del': case 'del':
gutterNumStr = (line.oldLine ?? '').toString(); gutterNumStr = (line.oldLine ?? '').toString();
color = 'red'; backgroundColor = '#4a1a1a';
prefixSymbol = '-'; prefixSymbol = '-';
break; break;
case 'context': case 'context':
@ -110,10 +110,10 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent })
return ( return (
<Box key={lineKey} flexDirection="row"> <Box key={lineKey} flexDirection="row">
<Text color="gray">{gutterNumStr.padEnd(4)} </Text> <Text color="gray">{gutterNumStr.padEnd(4)} </Text>
<Text color={color} dimColor={dim}> <Text backgroundColor={backgroundColor} dimColor={dim}>
{prefixSymbol}{' '} {prefixSymbol}{' '}
</Text> </Text>
<Text color={color} dimColor={dim}> <Text backgroundColor={backgroundColor} dimColor={dim}>
{line.content} {line.content}
</Text> </Text>
</Box> </Box>