summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/messages/DiffRenderer.tsx
diff options
context:
space:
mode:
authorTaylor Mullen <[email protected]>2025-04-17 18:06:21 -0400
committerN. Taylor Mullen <[email protected]>2025-04-17 15:29:34 -0700
commitcfc697a96d2e716a75e1c3b7f0f34fce81abaf1e (patch)
treee06bcba67ca71a874048aa887b17457dbd409bdf /packages/cli/src/ui/components/messages/DiffRenderer.tsx
parent7928c1727f0b208ed34850cc89bbb36ea3dd23e5 (diff)
Run `npm run format`
- Also updated README.md accordingly. Part of https://b.corp.google.com/issues/411384603
Diffstat (limited to 'packages/cli/src/ui/components/messages/DiffRenderer.tsx')
-rw-r--r--packages/cli/src/ui/components/messages/DiffRenderer.tsx69
1 files changed, 50 insertions, 19 deletions
diff --git a/packages/cli/src/ui/components/messages/DiffRenderer.tsx b/packages/cli/src/ui/components/messages/DiffRenderer.tsx
index 5cae9004..a45efe2a 100644
--- a/packages/cli/src/ui/components/messages/DiffRenderer.tsx
+++ b/packages/cli/src/ui/components/messages/DiffRenderer.tsx
@@ -1,5 +1,5 @@
import React from 'react';
-import { Box, Text } from 'ink'
+import { Box, Text } from 'ink';
interface DiffLine {
type: 'add' | 'del' | 'context' | 'hunk' | 'other';
@@ -30,29 +30,53 @@ function parseDiffWithLineNumbers(diffContent: string): DiffLine[] {
continue;
}
if (!inHunk) {
- // Skip standard Git header lines more robustly
- if (line.startsWith('--- ') || line.startsWith('+++ ') || line.startsWith('diff --git') || line.startsWith('index ') || line.startsWith('similarity index') || line.startsWith('rename from') || line.startsWith('rename to') || line.startsWith('new file mode') || line.startsWith('deleted file mode')) continue;
+ // Skip standard Git header lines more robustly
+ if (
+ line.startsWith('--- ') ||
+ line.startsWith('+++ ') ||
+ line.startsWith('diff --git') ||
+ line.startsWith('index ') ||
+ line.startsWith('similarity index') ||
+ line.startsWith('rename from') ||
+ line.startsWith('rename to') ||
+ line.startsWith('new file mode') ||
+ line.startsWith('deleted file mode')
+ )
+ continue;
// If it's not a hunk or header, skip (or handle as 'other' if needed)
continue;
}
if (line.startsWith('+')) {
currentNewLine++; // Increment before pushing
- result.push({ type: 'add', newLine: currentNewLine, content: line.substring(1) });
+ result.push({
+ type: 'add',
+ newLine: currentNewLine,
+ content: line.substring(1),
+ });
} else if (line.startsWith('-')) {
currentOldLine++; // Increment before pushing
- result.push({ type: 'del', oldLine: currentOldLine, content: line.substring(1) });
+ result.push({
+ type: 'del',
+ oldLine: currentOldLine,
+ content: line.substring(1),
+ });
} else if (line.startsWith(' ')) {
currentOldLine++; // Increment before pushing
currentNewLine++;
- result.push({ type: 'context', oldLine: currentOldLine, newLine: currentNewLine, content: line.substring(1) });
- } else if (line.startsWith('\\')) { // Handle "\ No newline at end of file"
+ result.push({
+ type: 'context',
+ oldLine: currentOldLine,
+ newLine: currentNewLine,
+ content: line.substring(1),
+ });
+ } else if (line.startsWith('\\')) {
+ // Handle "\ No newline at end of file"
result.push({ type: 'other', content: line });
}
}
return result;
}
-
interface DiffRendererProps {
diffContent: string;
filename?: string;
@@ -61,7 +85,10 @@ interface DiffRendererProps {
const DEFAULT_TAB_WIDTH = 4; // Spaces per tab for normalization
-const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEFAULT_TAB_WIDTH }) => {
+const DiffRenderer: React.FC<DiffRendererProps> = ({
+ diffContent,
+ tabWidth = DEFAULT_TAB_WIDTH,
+}) => {
if (!diffContent || typeof diffContent !== 'string') {
return <Text color="yellow">No diff content.</Text>;
}
@@ -69,14 +96,15 @@ const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEF
const parsedLines = parseDiffWithLineNumbers(diffContent);
// 1. Normalize whitespace (replace tabs with spaces) *before* further processing
- const normalizedLines = parsedLines.map(line => ({
+ const normalizedLines = parsedLines.map((line) => ({
...line,
- content: line.content.replace(/\t/g, ' '.repeat(tabWidth))
+ content: line.content.replace(/\t/g, ' '.repeat(tabWidth)),
}));
// Filter out non-displayable lines (hunks, potentially 'other') using the normalized list
- const displayableLines = normalizedLines.filter(l => l.type !== 'hunk' && l.type !== 'other');
-
+ const displayableLines = normalizedLines.filter(
+ (l) => l.type !== 'hunk' && l.type !== 'other',
+ );
if (displayableLines.length === 0) {
return (
@@ -93,7 +121,7 @@ const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEF
if (line.content.trim() === '') continue;
const firstCharIndex = line.content.search(/\S/); // Find index of first non-whitespace char
- const currentIndent = (firstCharIndex === -1) ? 0 : firstCharIndex; // Indent is 0 if no non-whitespace found
+ const currentIndent = firstCharIndex === -1 ? 0 : firstCharIndex; // Indent is 0 if no non-whitespace found
baseIndentation = Math.min(baseIndentation, currentIndent);
}
// If baseIndentation remained Infinity (e.g., no displayable lines with content), default to 0
@@ -102,7 +130,6 @@ const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEF
}
// --- End Modification ---
-
return (
<Box borderStyle="round" borderColor="gray" flexDirection="column">
{/* Iterate over the lines that should be displayed (already normalized) */}
@@ -139,9 +166,13 @@ const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEF
return (
// Using your original rendering structure
<Box key={key} flexDirection="row">
- <Text color="gray">{gutterNumStr} </Text>
- <Text color={color} dimColor={dim}>{prefixSymbol} </Text>
- <Text color={color} dimColor={dim} wrap="wrap">{displayContent}</Text>
+ <Text color="gray">{gutterNumStr} </Text>
+ <Text color={color} dimColor={dim}>
+ {prefixSymbol}{' '}
+ </Text>
+ <Text color={color} dimColor={dim} wrap="wrap">
+ {displayContent}
+ </Text>
</Box>
);
})}
@@ -149,4 +180,4 @@ const DiffRenderer: React.FC<DiffRendererProps> = ({ diffContent, tabWidth = DEF
);
};
-export default DiffRenderer; \ No newline at end of file
+export default DiffRenderer;