From 068b505d5e34478e6d41a2d58d8c1a0ed7001a88 Mon Sep 17 00:00:00 2001 From: DeWitt Clinton Date: Sun, 25 May 2025 10:26:51 -0700 Subject: [PATCH] Reduce excessive diff separators in CLI. (#535) Increases the threshold for rendering diff separators in the CLI's diff display. Previously, a separator was shown for gaps of more than one context line, leading to excessive separators in diffs with many small changes close together (Issue #534). By increasing `MAX_CONTEXT_LINES_WITHOUT_GAP` to 5, we allow for more context lines before a separator is added, significantly reducing visual clutter in such diffs. Added a test case to `DiffRenderer.test.tsx` to verify that separators are not rendered for small gaps within the new threshold. --- .../components/messages/DiffRenderer.test.tsx | 30 +++++++++++++++++++ .../ui/components/messages/DiffRenderer.tsx | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/ui/components/messages/DiffRenderer.test.tsx b/packages/cli/src/ui/components/messages/DiffRenderer.test.tsx index e3a94f9f..39c327e7 100644 --- a/packages/cli/src/ui/components/messages/DiffRenderer.test.tsx +++ b/packages/cli/src/ui/components/messages/DiffRenderer.test.tsx @@ -140,4 +140,34 @@ index 123..456 100644 expect(output).toContain('added line'); expect(output).toContain('context line 10'); }); + + it('should not render a gap indicator for small gaps (<= MAX_CONTEXT_LINES_WITHOUT_GAP)', () => { + const diffWithSmallGap = ` +diff --git a/file.txt b/file.txt +index abc..def 100644 +--- a/file.txt ++++ b/file.txt +@@ -1,5 +1,5 @@ + context line 1 + context line 2 + context line 3 + context line 4 + context line 5 +@@ -11,5 +11,5 @@ + context line 11 + context line 12 + context line 13 + context line 14 + context line 15 +`; + const { lastFrame } = render( + , + ); + const output = lastFrame(); + expect(output).not.toContain('═'); // Ensure no separator is rendered + + // Verify that lines before and after the gap are rendered + expect(output).toContain('context line 5'); + expect(output).toContain('context line 11'); + }); }); diff --git a/packages/cli/src/ui/components/messages/DiffRenderer.tsx b/packages/cli/src/ui/components/messages/DiffRenderer.tsx index bf00537d..8780d9c2 100644 --- a/packages/cli/src/ui/components/messages/DiffRenderer.tsx +++ b/packages/cli/src/ui/components/messages/DiffRenderer.tsx @@ -188,7 +188,7 @@ const renderDiffContent = ( : `diff-box-${crypto.createHash('sha1').update(JSON.stringify(parsedLines)).digest('hex')}`; let lastLineNumber: number | null = null; - const MAX_CONTEXT_LINES_WITHOUT_GAP = 1; + const MAX_CONTEXT_LINES_WITHOUT_GAP = 5; return (