fix: bg color for diff
This commit is contained in:
parent
12eb0d3d30
commit
63c9ecf24f
2 changed files with 15 additions and 54 deletions
|
|
@ -1,57 +1,18 @@
|
|||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { createTwoFilesPatch } from "diff";
|
||||
import { Tool } from "./types.js";
|
||||
|
||||
function generateDiff(oldContent: string, newContent: string, filePath: string): string {
|
||||
const oldLines = oldContent.split('\n');
|
||||
const newLines = newContent.split('\n');
|
||||
|
||||
let diff = `--- ${filePath}\n+++ ${filePath}\n`;
|
||||
|
||||
// Simple line-by-line diff
|
||||
const maxLines = Math.max(oldLines.length, newLines.length);
|
||||
let hunkStart = -1;
|
||||
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;
|
||||
return createTwoFilesPatch(
|
||||
filePath,
|
||||
filePath,
|
||||
oldContent,
|
||||
newContent,
|
||||
undefined,
|
||||
undefined,
|
||||
{ context: 3 }
|
||||
);
|
||||
}
|
||||
|
||||
export const writeFileTool: Tool = {
|
||||
|
|
|
|||
|
|
@ -83,19 +83,19 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent })
|
|||
{displayableLines.map((line, index) => {
|
||||
const lineKey = `diff-line-${index}`;
|
||||
let gutterNumStr = '';
|
||||
let color: string | undefined = undefined;
|
||||
let prefixSymbol = ' ';
|
||||
let dim = false;
|
||||
let backgroundColor: string | undefined = undefined;
|
||||
|
||||
switch (line.type) {
|
||||
case 'add':
|
||||
gutterNumStr = (line.newLine ?? '').toString();
|
||||
color = 'green';
|
||||
backgroundColor = '#1a4a1a';
|
||||
prefixSymbol = '+';
|
||||
break;
|
||||
case 'del':
|
||||
gutterNumStr = (line.oldLine ?? '').toString();
|
||||
color = 'red';
|
||||
backgroundColor = '#4a1a1a';
|
||||
prefixSymbol = '-';
|
||||
break;
|
||||
case 'context':
|
||||
|
|
@ -110,10 +110,10 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent })
|
|||
return (
|
||||
<Box key={lineKey} flexDirection="row">
|
||||
<Text color="gray">{gutterNumStr.padEnd(4)} </Text>
|
||||
<Text color={color} dimColor={dim}>
|
||||
<Text backgroundColor={backgroundColor} dimColor={dim}>
|
||||
{prefixSymbol}{' '}
|
||||
</Text>
|
||||
<Text color={color} dimColor={dim}>
|
||||
<Text backgroundColor={backgroundColor} dimColor={dim}>
|
||||
{line.content}
|
||||
</Text>
|
||||
</Box>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue