|
| 1 | +import type { ElementTable, On } from 'claude-code' |
| 2 | + |
| 3 | +import { splitRows } from './split-rows.js' |
| 4 | +import type { Cell, SplitRow } from './split-rows.js' |
| 5 | + |
| 6 | +/** Tools whose result is a diff this mod redraws. */ |
| 7 | +const TOOLS = ['Edit', 'Write'] |
| 8 | + |
| 9 | +/** Rows drawn before "… N more rows". */ |
| 10 | +const MAX_ROWS = 200 |
| 11 | + |
| 12 | +type Kit = Pick<ElementTable<'terminal'>, 'Box' | 'Text' | 'Code'> |
| 13 | + |
| 14 | +const isRecord = (value: unknown): value is Record<string, unknown> => |
| 15 | + typeof value === 'object' && value !== null |
| 16 | + |
| 17 | +/** |
| 18 | + * Draws the result block of an Edit or Write call as a split diff: removed |
| 19 | + * lines in the left column and added lines in the right, with line numbers. |
| 20 | + * |
| 21 | + * @param on the engine's registrar |
| 22 | + */ |
| 23 | +export function register(on: On) { |
| 24 | + on('ui.render', { component: 'ToolResult' }, async ($, e, next) => { |
| 25 | + const { tool, output, isErrored } = e.props |
| 26 | + if (!TOOLS.includes(tool) || isErrored || !isRecord(output)) return next(e) |
| 27 | + // An edit held for review wrote nothing, so there is no diff to show. |
| 28 | + if (output.staged === true) return next(e) |
| 29 | + |
| 30 | + const diff = splitRows(output.structuredPatch) |
| 31 | + // No hunks (a new file, an identical write): leave Claude Code's drawing alone. |
| 32 | + if (diff === null) return next(e) |
| 33 | + |
| 34 | + const kit: Kit = $.ui.resolve(e) |
| 35 | + const { Box, Text } = kit |
| 36 | + const path = typeof output.filePath === 'string' ? output.filePath : undefined |
| 37 | + |
| 38 | + const shown = diff.rows.slice(0, MAX_ROWS) |
| 39 | + const hidden = diff.rows.length - shown.length |
| 40 | + |
| 41 | + return Box({ |
| 42 | + flexDirection: 'column', |
| 43 | + children: [ |
| 44 | + // The built-in summary line, with its response marker. |
| 45 | + Box({ |
| 46 | + flexDirection: 'row', |
| 47 | + children: [ |
| 48 | + Text({ dimColor: true, children: [' ⎿ '] }), |
| 49 | + ...summary(Text, diff.added, diff.removed), |
| 50 | + ], |
| 51 | + }), |
| 52 | + // The rows sit in a frame under the summary text, as the built-in diff does. |
| 53 | + Box({ |
| 54 | + flexDirection: 'column', |
| 55 | + marginLeft: 5, |
| 56 | + borderStyle: 'round', |
| 57 | + borderDimColor: true, |
| 58 | + children: [ |
| 59 | + ...shown.map(row => drawRow(kit, row, path)), |
| 60 | + hidden > 0 ? Text({ dimColor: true, children: ['… ' + hidden + ' more rows'] }) : null, |
| 61 | + ], |
| 62 | + }), |
| 63 | + ], |
| 64 | + }) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +function drawRow(kit: Kit, row: SplitRow, path: string | undefined) { |
| 69 | + const { Box, Text } = kit |
| 70 | + if (row.kind === 'gap') { |
| 71 | + // The label sits in both columns, so a wide screen doesn't leave the right one blank. |
| 72 | + const label = () => Text({ dimColor: true, children: ['⋯ ' + row.hidden + ' unchanged lines'] }) |
| 73 | + // A blank line above and below sets the label apart; an empty 1-wide Box takes the separator's place so the columns line up. |
| 74 | + const column = { width: '50%', flexShrink: 1, paddingY: 1 } as const |
| 75 | + return Box({ |
| 76 | + flexDirection: 'row', |
| 77 | + children: [Box({ ...column, children: [label()] }), Box({ width: 1, flexShrink: 0 }), Box({ ...column, children: [label()] })], |
| 78 | + }) |
| 79 | + } |
| 80 | + if (row.changed && row.left !== null && row.right !== null) { |
| 81 | + return drawPair(kit, row.left, row.right, path) |
| 82 | + } |
| 83 | + return Box({ |
| 84 | + flexDirection: 'row', |
| 85 | + alignItems: 'stretch', |
| 86 | + children: [ |
| 87 | + drawCell(kit, row.left, row.changed ? 'removed' : 'same', path), |
| 88 | + separator(kit), |
| 89 | + drawCell(kit, row.right, row.changed ? 'added' : 'same', path), |
| 90 | + ], |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +function drawCell( |
| 95 | + { Box, Text, Code }: Kit, |
| 96 | + cell: Cell | null, |
| 97 | + kind: 'removed' | 'added' | 'same', |
| 98 | + path: string | undefined, |
| 99 | +) { |
| 100 | + const column = { width: '50%', flexShrink: 1, flexDirection: 'row' } as const |
| 101 | + |
| 102 | + // Nothing on this side of the row: keep the column, draw nothing in it. |
| 103 | + if (cell === null) return Box({ ...column, children: [Text({ children: [' '] })] }) |
| 104 | + |
| 105 | + // One-line hunk through the engine's diff renderer, so gutter, marker and |
| 106 | + // add/remove colors are exactly the built-in diff's. |
| 107 | + const n = cell.no |
| 108 | + const header = |
| 109 | + kind === 'removed' ? `@@ -${n},1 +${n},0 @@` : kind === 'added' ? `@@ -${n},0 +${n},1 @@` : `@@ -${n},1 +${n},1 @@` |
| 110 | + const sign = kind === 'removed' ? '-' : kind === 'added' ? '+' : ' ' |
| 111 | + |
| 112 | + return Box({ |
| 113 | + ...column, |
| 114 | + children: [ |
| 115 | + Code({ |
| 116 | + source: header + '\n' + sign + cell.text, |
| 117 | + format: 'diff', |
| 118 | + wrap: 'wrap', |
| 119 | + ...(path === undefined ? {} : { path }), |
| 120 | + }), |
| 121 | + ], |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * A removed line next to the line that replaced it. The engine draws the two as |
| 127 | + * one hunk, which is what makes it tint the words that changed; each column |
| 128 | + * then shows its own line of that hunk. Long lines are cut with an ellipsis, as |
| 129 | + * a column clipped to one row cannot wrap. |
| 130 | + */ |
| 131 | +function drawPair(kit: Kit, left: Cell, right: Cell, path: string | undefined) { |
| 132 | + const { Box, Code } = kit |
| 133 | + const hunk = () => |
| 134 | + Code({ |
| 135 | + source: `@@ -${left.no},1 +${right.no},1 @@\n-${left.text}\n+${right.text}`, |
| 136 | + format: 'diff', |
| 137 | + wrap: 'truncate-end', |
| 138 | + ...(path === undefined ? {} : { path }), |
| 139 | + }) |
| 140 | + const column = { width: '50%', flexShrink: 1, height: 1, overflow: 'hidden' } as const |
| 141 | + return Box({ |
| 142 | + flexDirection: 'row', |
| 143 | + children: [ |
| 144 | + Box({ ...column, children: [hunk()] }), |
| 145 | + separator(kit), |
| 146 | + Box({ |
| 147 | + ...column, |
| 148 | + children: [Box({ position: 'absolute', top: -1, left: 0, right: 0, children: [hunk()] })], |
| 149 | + }), |
| 150 | + ], |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * A dim vertical rule between the columns. It stretches to the row's height |
| 156 | + * (wrapped lines make rows taller), so it draws a tall line out of flow and |
| 157 | + * lets the clipped box cut it to the row. |
| 158 | + */ |
| 159 | +function separator({ Box, Text }: Kit) { |
| 160 | + return Box({ |
| 161 | + width: 1, |
| 162 | + flexShrink: 0, |
| 163 | + overflow: 'hidden', |
| 164 | + children: [ |
| 165 | + Box({ |
| 166 | + position: 'absolute', |
| 167 | + top: 0, |
| 168 | + bottom: 0, |
| 169 | + flexDirection: 'column', |
| 170 | + children: Array.from({ length: 20 }, () => Text({ dimColor: true, children: ['│'] })), |
| 171 | + }), |
| 172 | + ], |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +/** "Added 3 lines, removed 1 line", the counts bold, as the built-in diff words it. */ |
| 177 | +function summary(Text: Kit['Text'], added: number, removed: number) { |
| 178 | + const noun = (n: number) => (n > 1 ? ' lines' : ' line') |
| 179 | + const parts: ReturnType<Kit['Text']>[] = [] |
| 180 | + if (added > 0) { |
| 181 | + parts.push(Text({ children: ['Added '] }), Text({ bold: true, children: [String(added)] })) |
| 182 | + parts.push(Text({ children: [noun(added) + (removed > 0 ? ', ' : '')] })) |
| 183 | + } |
| 184 | + if (removed > 0) { |
| 185 | + parts.push(Text({ children: [added === 0 ? 'Removed ' : 'removed '] })) |
| 186 | + parts.push(Text({ bold: true, children: [String(removed)] }), Text({ children: [noun(removed)] })) |
| 187 | + } |
| 188 | + return parts |
| 189 | +} |
0 commit comments