Skip to content

Commit 3268618

Browse files
committed
feat(tui): add minimal diff display mode
Add tui.diff_display config option to collapse Edit/Write tool diffs by default, showing only file name and line statistics. - Add diff_display config (full | minimal) to Config.TUI - Add parseDiffStats() and formatDiffStats() utility functions - Enhance Edit tool with collapse/expand functionality - Enhance Write tool with collapse/expand functionality - Add comprehensive unit tests (12 tests, all passing) In minimal mode: - Edit: shows "+N, -M lines (press Enter to expand)" - Write: shows "N lines (press Enter to expand)" - Click to toggle between collapsed and expanded views - Full diff remains in metadata for AI context Fixes anomalyco#9089
1 parent 4aacab2 commit 3268618

4 files changed

Lines changed: 298 additions & 50 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 148 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from "@opentui/core"
2929
import { Prompt, type PromptRef } from "@tui/component/prompt"
3030
import type { AssistantMessage, Part, ToolPart, UserMessage, TextPart, ReasoningPart } from "@opencode-ai/sdk/v2"
31+
import { parseDiffStats, formatDiffStats } from "@/cli/cmd/tui/util/diff"
3132
import { useLocal } from "@tui/context/local"
3233
import { Locale } from "@/util/locale"
3334
import type { Tool } from "@/tool/tool"
@@ -1600,12 +1601,27 @@ function Bash(props: ToolProps<typeof BashTool>) {
16001601
}
16011602

16021603
function Write(props: ToolProps<typeof WriteTool>) {
1604+
const ctx = use()
16031605
const { theme, syntax } = useTheme()
1606+
const sync = useSync()
1607+
16041608
const code = createMemo(() => {
16051609
if (!props.input.content) return ""
16061610
return props.input.content
16071611
})
16081612

1613+
// Check if minimal mode is enabled
1614+
const diffDisplay = createMemo(() => sync.data.config.tui?.diff_display ?? "full")
1615+
1616+
// Calculate line count
1617+
const lineCount = createMemo(() => code().split("\n").length)
1618+
1619+
// Expanded/collapsed state
1620+
const [expanded, setExpanded] = createSignal(false)
1621+
1622+
// Should show collapsed view
1623+
const showCollapsed = createMemo(() => diffDisplay() === "minimal" && !expanded())
1624+
16091625
const diagnostics = createMemo(() => {
16101626
const filePath = Filesystem.normalizePath(props.input.filePath ?? "")
16111627
return props.metadata.diagnostics?.[filePath] ?? []
@@ -1614,24 +1630,56 @@ function Write(props: ToolProps<typeof WriteTool>) {
16141630
return (
16151631
<Switch>
16161632
<Match when={props.metadata.diagnostics !== undefined}>
1617-
<BlockTool title={"# Wrote " + normalizePath(props.input.filePath!)} part={props.part}>
1618-
<line_number fg={theme.textMuted} minWidth={3} paddingRight={1}>
1619-
<code
1620-
conceal={false}
1621-
fg={theme.text}
1622-
filetype={filetype(props.input.filePath!)}
1623-
syntaxStyle={syntax()}
1624-
content={code()}
1625-
/>
1626-
</line_number>
1627-
<Show when={diagnostics().length}>
1628-
<For each={diagnostics()}>
1629-
{(diagnostic) => (
1630-
<text fg={theme.error}>
1631-
Error [{diagnostic.range.start.line}:{diagnostic.range.start.character}]: {diagnostic.message}
1632-
</text>
1633-
)}
1634-
</For>
1633+
<BlockTool
1634+
title={"# Wrote " + normalizePath(props.input.filePath!)}
1635+
part={props.part}
1636+
onClick={() => {
1637+
if (diffDisplay() === "minimal") {
1638+
setExpanded(!expanded())
1639+
}
1640+
}}
1641+
>
1642+
<Show
1643+
when={showCollapsed()}
1644+
fallback={
1645+
// Full content display (when expanded or full mode)
1646+
<>
1647+
<line_number fg={theme.textMuted} minWidth={3} paddingRight={1}>
1648+
<code
1649+
conceal={false}
1650+
fg={theme.text}
1651+
filetype={filetype(props.input.filePath!)}
1652+
syntaxStyle={syntax()}
1653+
content={code()}
1654+
/>
1655+
</line_number>
1656+
<Show when={diagnostics().length}>
1657+
<For each={diagnostics()}>
1658+
{(diagnostic) => (
1659+
<text fg={theme.error}>
1660+
Error [{diagnostic.range.start.line}:{diagnostic.range.start.character}]: {diagnostic.message}
1661+
</text>
1662+
)}
1663+
</For>
1664+
</Show>
1665+
</>
1666+
}
1667+
>
1668+
{/* Collapsed view */}
1669+
<box paddingLeft={3} paddingTop={1} paddingBottom={1}>
1670+
<text fg={theme.textMuted}>
1671+
{lineCount()} lines [press Enter to expand]
1672+
</text>
1673+
</box>
1674+
<Show when={diagnostics().length}>
1675+
<For each={diagnostics()}>
1676+
{(diagnostic) => (
1677+
<text fg={theme.error} paddingLeft={1}>
1678+
Error [{diagnostic.range.start.line}:{diagnostic.range.start.character}]: {diagnostic.message}
1679+
</text>
1680+
)}
1681+
</For>
1682+
</Show>
16351683
</Show>
16361684
</BlockTool>
16371685
</Match>
@@ -1781,6 +1829,21 @@ function Edit(props: ToolProps<typeof EditTool>) {
17811829

17821830
const diffContent = createMemo(() => props.metadata.diff)
17831831

1832+
// Check if minimal mode is enabled
1833+
const diffDisplay = createMemo(() => ctx.sync.data.config.tui?.diff_display ?? "full")
1834+
1835+
// Parse diff statistics
1836+
const diffStats = createMemo(() => {
1837+
if (!props.metadata.diff) return null
1838+
return parseDiffStats(props.metadata.diff)
1839+
})
1840+
1841+
// Expanded/collapsed state
1842+
const [expanded, setExpanded] = createSignal(false)
1843+
1844+
// Should show collapsed view
1845+
const showCollapsed = createMemo(() => diffDisplay() === "minimal" && !expanded())
1846+
17841847
const diagnostics = createMemo(() => {
17851848
const filePath = Filesystem.normalizePath(props.input.filePath ?? "")
17861849
const arr = props.metadata.diagnostics?.[filePath] ?? []
@@ -1790,39 +1853,74 @@ function Edit(props: ToolProps<typeof EditTool>) {
17901853
return (
17911854
<Switch>
17921855
<Match when={props.metadata.diff !== undefined}>
1793-
<BlockTool title={"← Edit " + normalizePath(props.input.filePath!)} part={props.part}>
1794-
<box paddingLeft={1}>
1795-
<diff
1796-
diff={diffContent()}
1797-
view={view()}
1798-
filetype={ft()}
1799-
syntaxStyle={syntax()}
1800-
showLineNumbers={true}
1801-
width="100%"
1802-
wrapMode={ctx.diffWrapMode()}
1803-
fg={theme.text}
1804-
addedBg={theme.diffAddedBg}
1805-
removedBg={theme.diffRemovedBg}
1806-
contextBg={theme.diffContextBg}
1807-
addedSignColor={theme.diffHighlightAdded}
1808-
removedSignColor={theme.diffHighlightRemoved}
1809-
lineNumberFg={theme.diffLineNumber}
1810-
lineNumberBg={theme.diffContextBg}
1811-
addedLineNumberBg={theme.diffAddedLineNumberBg}
1812-
removedLineNumberBg={theme.diffRemovedLineNumberBg}
1813-
/>
1814-
</box>
1815-
<Show when={diagnostics().length}>
1816-
<box>
1817-
<For each={diagnostics()}>
1818-
{(diagnostic) => (
1819-
<text fg={theme.error}>
1820-
Error [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}]{" "}
1821-
{diagnostic.message}
1822-
</text>
1823-
)}
1824-
</For>
1856+
<BlockTool
1857+
title={"← Edit " + normalizePath(props.input.filePath!)}
1858+
part={props.part}
1859+
onClick={() => {
1860+
if (diffDisplay() === "minimal") {
1861+
setExpanded(!expanded())
1862+
}
1863+
}}
1864+
>
1865+
<Show
1866+
when={showCollapsed()}
1867+
fallback={
1868+
// Full diff display (when expanded or full mode)
1869+
<>
1870+
<box paddingLeft={1}>
1871+
<diff
1872+
diff={diffContent()}
1873+
view={view()}
1874+
filetype={ft()}
1875+
syntaxStyle={syntax()}
1876+
showLineNumbers={true}
1877+
width="100%"
1878+
wrapMode={ctx.diffWrapMode()}
1879+
fg={theme.text}
1880+
addedBg={theme.diffAddedBg}
1881+
removedBg={theme.diffRemovedBg}
1882+
contextBg={theme.diffContextBg}
1883+
addedSignColor={theme.diffHighlightAdded}
1884+
removedSignColor={theme.diffHighlightRemoved}
1885+
lineNumberFg={theme.diffLineNumber}
1886+
lineNumberBg={theme.diffContextBg}
1887+
addedLineNumberBg={theme.diffAddedLineNumberBg}
1888+
removedLineNumberBg={theme.diffRemovedLineNumberBg}
1889+
/>
1890+
</box>
1891+
<Show when={diagnostics().length}>
1892+
<box>
1893+
<For each={diagnostics()}>
1894+
{(diagnostic) => (
1895+
<text fg={theme.error}>
1896+
Error [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}]{" "}
1897+
{diagnostic.message}
1898+
</text>
1899+
)}
1900+
</For>
1901+
</box>
1902+
</Show>
1903+
</>
1904+
}
1905+
>
1906+
{/* Collapsed view */}
1907+
<box paddingLeft={3} paddingTop={1} paddingBottom={1}>
1908+
<text fg={theme.textMuted}>
1909+
{formatDiffStats(diffStats()!)} [press Enter to expand]
1910+
</text>
18251911
</box>
1912+
<Show when={diagnostics().length}>
1913+
<box paddingLeft={1}>
1914+
<For each={diagnostics()}>
1915+
{(diagnostic) => (
1916+
<text fg={theme.error}>
1917+
Error [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}]{" "}
1918+
{diagnostic.message}
1919+
</text>
1920+
)}
1921+
</For>
1922+
</box>
1923+
</Show>
18261924
</Show>
18271925
</BlockTool>
18281926
</Match>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Diff utility functions for TUI display
3+
*/
4+
5+
/**
6+
* Parse unified diff format to extract line statistics
7+
* @param diff - Unified diff string
8+
* @returns Object with added and removed line counts
9+
* @example
10+
* parseDiffStats(`
11+
* --- a/file.ts
12+
* +++ b/file.ts
13+
* @@ -1,3 +1,5 @@
14+
* const x = 1
15+
* +const y = 2
16+
* +const z = 3
17+
* `)
18+
* // Returns: { added: 2, removed: 0 }
19+
*/
20+
export function parseDiffStats(diff: string): { added: number; removed: number } {
21+
const lines = diff.split("\n")
22+
let added = 0
23+
let removed = 0
24+
25+
for (const line of lines) {
26+
if (line.startsWith("+") && !line.startsWith("+++")) added++
27+
else if (line.startsWith("-") && !line.startsWith("---")) removed++
28+
}
29+
30+
return { added, removed }
31+
}
32+
33+
/**
34+
* Format diff statistics for display
35+
* @param stats - Object with added and removed line counts
36+
* @returns Formatted string like "+5 lines" or "+5, -3 lines"
37+
* @example
38+
* formatDiffStats({ added: 5, removed: 3 }) // "+5, -3 lines"
39+
* formatDiffStats({ added: 0, removed: 3 }) // "-3 lines"
40+
*/
41+
export function formatDiffStats(stats: { added: number; removed: number }): string {
42+
const parts: string[] = []
43+
if (stats.added > 0) parts.push(`+${stats.added}`)
44+
if (stats.removed > 0) parts.push(`-${stats.removed}`)
45+
46+
if (parts.length === 0) return "0 lines"
47+
return parts.join(", ") + " lines"
48+
}

‎packages/opencode/src/config/config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,10 @@ export namespace Config {
790790
.enum(["auto", "stacked"])
791791
.optional()
792792
.describe("Control diff rendering style: 'auto' adapts to terminal width, 'stacked' always shows single column"),
793+
diff_display: z
794+
.enum(["full", "minimal"])
795+
.optional()
796+
.describe("Control diff display mode: 'full' shows complete diff, 'minimal' shows collapsed by default with line statistics"),
793797
})
794798

795799
export const Server = z
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { parseDiffStats, formatDiffStats } from "@/cli/cmd/tui/util/diff"
3+
4+
describe("diff utility functions", () => {
5+
describe("parseDiffStats", () => {
6+
test("parses added lines", () => {
7+
const diff = `
8+
--- a/test.ts
9+
+++ b/test.ts
10+
@@ -1,3 +1,5 @@
11+
const x = 1
12+
+const y = 2
13+
+const z = 3
14+
`
15+
expect(parseDiffStats(diff)).toEqual({ added: 2, removed: 0 })
16+
})
17+
18+
test("parses removed lines", () => {
19+
const diff = `
20+
--- a/test.ts
21+
+++ b/test.ts
22+
@@ -1,3 +1,2 @@
23+
const x = 1
24+
-const y = 2
25+
`
26+
expect(parseDiffStats(diff)).toEqual({ added: 0, removed: 1 })
27+
})
28+
29+
test("parses mixed changes", () => {
30+
const diff = `
31+
--- a/test.ts
32+
+++ b/test.ts
33+
@@ -1,5 +1,6 @@
34+
const x = 1
35+
-const old = 2
36+
+const new = 2
37+
+const extra = 3
38+
`
39+
expect(parseDiffStats(diff)).toEqual({ added: 2, removed: 1 })
40+
})
41+
42+
test("handles empty diff", () => {
43+
expect(parseDiffStats("")).toEqual({ added: 0, removed: 0 })
44+
})
45+
46+
test("ignores diff headers", () => {
47+
const diff = `
48+
--- a/test.ts
49+
+++ b/test.ts
50+
@@ -1,3 +1,5 @@
51+
-old line
52+
+new line
53+
`
54+
expect(parseDiffStats(diff)).toEqual({ added: 1, removed: 1 })
55+
})
56+
57+
test("handles multi-hunk diff", () => {
58+
const diff = `
59+
--- a/test.ts
60+
+++ b/test.ts
61+
@@ -1,3 +1,4 @@
62+
const x = 1
63+
+const y = 2
64+
@@ -5,3 +6,5 @@
65+
const a = 1
66+
+const b = 2
67+
-const c = 3
68+
`
69+
expect(parseDiffStats(diff)).toEqual({ added: 2, removed: 1 })
70+
})
71+
})
72+
73+
describe("formatDiffStats", () => {
74+
test("formats added lines", () => {
75+
expect(formatDiffStats({ added: 5, removed: 0 })).toBe("+5 lines")
76+
})
77+
78+
test("formats removed lines", () => {
79+
expect(formatDiffStats({ added: 0, removed: 3 })).toBe("-3 lines")
80+
})
81+
82+
test("formats mixed changes", () => {
83+
expect(formatDiffStats({ added: 5, removed: 3 })).toBe("+5, -3 lines")
84+
})
85+
86+
test("formats no changes", () => {
87+
expect(formatDiffStats({ added: 0, removed: 0 })).toBe("0 lines")
88+
})
89+
90+
test("formats single added line", () => {
91+
expect(formatDiffStats({ added: 1, removed: 0 })).toBe("+1 lines")
92+
})
93+
94+
test("formats single removed line", () => {
95+
expect(formatDiffStats({ added: 0, removed: 1 })).toBe("-1 lines")
96+
})
97+
})
98+
})

0 commit comments

Comments
 (0)