MCPcopy
hub / github.com/claude-code-best/claude-code / renderColorDiff

Function renderColorDiff

src/components/StructuredDiff.tsx:51–104  ·  view source on GitHub ↗
(
  patch: StructuredPatchHunk,
  firstLine: string | null,
  filePath: string,
  fileContent: string | null,
  theme: string,
  width: number,
  dim: boolean,
  splitGutter: boolean,
)

Source from the content-addressed store, hash-verified

49}
50
51function renderColorDiff(
52 patch: StructuredPatchHunk,
53 firstLine: string | null,
54 filePath: string,
55 fileContent: string | null,
56 theme: string,
57 width: number,
58 dim: boolean,
59 splitGutter: boolean,
60): CachedRender | null {
61 const ColorDiff = expectColorDiff();
62 if (!ColorDiff) return null;
63
64 // Defensive: if the gutter would eat the whole render width (narrow
65 // terminal), skip the split. Rust already wraps to `width` so the
66 // single-column output stays correct; we just lose noSelect. Without
67 // this, sliceAnsi(line, gutterWidth) would return empty content and
68 // RawAnsi(width<=0) is untested.
69 const rawGutterWidth = splitGutter ? computeGutterWidth(patch) : 0;
70 const gutterWidth = rawGutterWidth > 0 && rawGutterWidth < width ? rawGutterWidth : 0;
71
72 const key = `${theme}|${width}|${dim ? 1 : 0}|${gutterWidth}|${firstLine ?? ''}|${filePath}`;
73
74 let perHunk = RENDER_CACHE.get(patch);
75 const hit = perHunk?.get(key);
76 if (hit) return hit;
77
78 const lines = new ColorDiff(patch, firstLine, filePath, fileContent).render(theme, width, dim);
79 if (lines === null) return null;
80
81 // Pre-split the gutter column once (cold-cache). sliceAnsi preserves
82 // styles across the cut; the Rust module already pads the gutter to
83 // gutterWidth so the narrow RawAnsi column's width matches its cells.
84 let gutters: string[] | null = null;
85 let contents: string[] | null = null;
86 if (gutterWidth > 0) {
87 gutters = lines.map(l => sliceAnsi(l, 0, gutterWidth));
88 contents = lines.map(l => sliceAnsi(l, gutterWidth));
89 }
90
91 const entry: CachedRender = { lines, gutterWidth, gutters, contents };
92
93 if (!perHunk) {
94 perHunk = new Map();
95 RENDER_CACHE.set(patch, perHunk);
96 }
97 // Cap the inner map: width is part of the key, so terminal resize while a
98 // diff is visible accumulates a full render copy per distinct width. Four
99 // variants (two widths × dim on/off) covers the steady state; beyond that
100 // the user is actively resizing and old widths are stale.
101 if (perHunk.size >= 4) perHunk.clear();
102 perHunk.set(key, entry);
103 return entry;
104}
105
106export const StructuredDiff = memo(function StructuredDiff({
107 patch,

Callers 1

StructuredDiff.tsxFile · 0.85

Calls 7

expectColorDiffFunction · 0.85
computeGutterWidthFunction · 0.85
setMethod · 0.80
getMethod · 0.65
sliceAnsiFunction · 0.50
renderMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected