From 12eb0d3d30bedcd232f48a68993bb32fdac5e16e Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 00:38:01 -0700 Subject: [PATCH] colored diff --- src/ui/ColoredDiff.tsx | 124 +++++++++++++++++++++++++++++++++++ src/ui/ToolResultSummary.tsx | 45 +++++++++---- 2 files changed, 155 insertions(+), 14 deletions(-) create mode 100644 src/ui/ColoredDiff.tsx diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx new file mode 100644 index 000000000..a375e1e6d --- /dev/null +++ b/src/ui/ColoredDiff.tsx @@ -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 No changes detected.; + } + + const displayableLines = parsedLines.filter( + (l) => l.type !== 'hunk' && l.type !== 'other', + ); + + return ( + + {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 ( + + {gutterNumStr.padEnd(4)} + + {prefixSymbol}{' '} + + + {line.content} + + + ); + })} + + ); +}; \ No newline at end of file diff --git a/src/ui/ToolResultSummary.tsx b/src/ui/ToolResultSummary.tsx index 6a4c32288..c55201514 100644 --- a/src/ui/ToolResultSummary.tsx +++ b/src/ui/ToolResultSummary.tsx @@ -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 = ({ toolName, content, }) => { + if (!content) { + return ( + + ⎿ + No output + + ); + } + + 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 ( + + + ⎿ + File written successfully + + + + ); + } + } + + // 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 = ({ 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":