colored diff

This commit is contained in:
Nate 2025-07-14 00:38:01 -07:00
parent fde6098ef2
commit 12eb0d3d30
2 changed files with 155 additions and 14 deletions

124
src/ui/ColoredDiff.tsx Normal file
View file

@ -0,0 +1,124 @@
import { Box, Text } from "ink";
import React from "react";
interface DiffLine {
type: 'add' | 'del' | 'context' | 'hunk' | 'other';
oldLine?: number;
newLine?: number;
content: string;
}
function parseDiffWithLineNumbers(diffContent: string): DiffLine[] {
const lines = diffContent.split('\n');
const result: DiffLine[] = [];
let currentOldLine = 0;
let currentNewLine = 0;
let inHunk = false;
const hunkHeaderRegex = /^@@ -(\d+),?\d* \+(\d+),?\d* @@/;
for (const line of lines) {
const hunkMatch = line.match(hunkHeaderRegex);
if (hunkMatch) {
currentOldLine = parseInt(hunkMatch[1], 10);
currentNewLine = parseInt(hunkMatch[2], 10);
inHunk = true;
result.push({ type: 'hunk', content: line });
currentOldLine--;
currentNewLine--;
continue;
}
if (!inHunk) {
if (
line.startsWith('--- ') ||
line.startsWith('+++ ') ||
line.startsWith('diff --git') ||
line.startsWith('index ')
)
continue;
continue;
}
if (line.startsWith('+')) {
currentNewLine++;
result.push({
type: 'add',
newLine: currentNewLine,
content: line.substring(1),
});
} else if (line.startsWith('-')) {
currentOldLine++;
result.push({
type: 'del',
oldLine: currentOldLine,
content: line.substring(1),
});
} else if (line.startsWith(' ')) {
currentOldLine++;
currentNewLine++;
result.push({
type: 'context',
oldLine: currentOldLine,
newLine: currentNewLine,
content: line.substring(1),
});
} else if (line.startsWith('\\')) {
result.push({ type: 'other', content: line });
}
}
return result;
}
export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) => {
const parsedLines = parseDiffWithLineNumbers(diffContent);
if (parsedLines.length === 0) {
return <Text color="gray">No changes detected.</Text>;
}
const displayableLines = parsedLines.filter(
(l) => l.type !== 'hunk' && l.type !== 'other',
);
return (
<Box flexDirection="column">
{displayableLines.map((line, index) => {
const lineKey = `diff-line-${index}`;
let gutterNumStr = '';
let color: string | undefined = undefined;
let prefixSymbol = ' ';
let dim = false;
switch (line.type) {
case 'add':
gutterNumStr = (line.newLine ?? '').toString();
color = 'green';
prefixSymbol = '+';
break;
case 'del':
gutterNumStr = (line.oldLine ?? '').toString();
color = 'red';
prefixSymbol = '-';
break;
case 'context':
gutterNumStr = (line.newLine ?? '').toString();
dim = true;
prefixSymbol = ' ';
break;
default:
return null;
}
return (
<Box key={lineKey} flexDirection="row">
<Text color="gray">{gutterNumStr.padEnd(4)} </Text>
<Text color={color} dimColor={dim}>
{prefixSymbol}{' '}
</Text>
<Text color={color} dimColor={dim}>
{line.content}
</Text>
</Box>
);
})}
</Box>
);
};

View file

@ -1,6 +1,7 @@
import { Box, Text } from "ink";
import React from "react";
import { getToolDisplayName } from "../tools.js";
import { ColoredDiff } from "./ColoredDiff.js";
interface ToolResultSummaryProps {
toolName?: string;
@ -11,14 +12,37 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
toolName,
content,
}) => {
if (!content) {
return (
<Box>
<Text color="gray">⎿ </Text>
<Text color="gray"> No output</Text>
</Box>
);
}
const lines = content.split("\n").length;
const chars = content.length;
const displayName = toolName ? getToolDisplayName(toolName) : "Tool";
// Handle write_file with diff specially
if (toolName === "write_file" && content.includes("Diff:\n")) {
const diffSection = content.split("Diff:\n")[1];
if (diffSection) {
return (
<Box flexDirection="column">
<Box>
<Text color="gray">⎿ </Text>
<Text color="green"> File written successfully</Text>
</Box>
<ColoredDiff diffContent={diffSection} />
</Box>
);
}
}
// Handle all other cases with text summary
const getSummary = () => {
if (!content) return "No output";
const lines = content.split("\n").length;
const chars = content.length;
const displayName = toolName ? getToolDisplayName(toolName) : "Tool";
// Handle specific tool output formatting
switch (toolName) {
case "read_file":
// Try to extract file path from content if it contains line numbers
@ -30,13 +54,6 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
return `${displayName} tool output (${lines} lines)`;
case "write_file":
// For write_file, we want to display the diff content if it exists
if (content.includes("Diff:\n")) {
const diffSection = content.split("Diff:\n")[1];
if (diffSection) {
return `File written successfully\n${diffSection}`;
}
}
return content.includes("Successfully created file") ? "File created successfully" : "File written successfully";
case "list_files":