MCPcopy Create free account
hub / github.com/modem-dev/hunk / sliceSpansWindow

Function sliceSpansWindow

src/ui/diff/renderRows.tsx:60–111  ·  view source on GitHub ↗

Slice styled spans to one visible window while preserving color runs.

(spans: RenderSpan[], offset: number, width: number)

Source from the content-addressed store, hash-verified

58
59/** Slice styled spans to one visible window while preserving color runs. */
60function sliceSpansWindow(spans: RenderSpan[], offset: number, width: number) {
61 if (width <= 0) {
62 return {
63 spans: [] as RenderSpan[],
64 usedWidth: 0,
65 };
66 }
67
68 const sliced: RenderSpan[] = [];
69 let remainingOffset = Math.max(0, offset);
70 let remaining = width;
71 let usedWidth = 0;
72
73 for (const span of spans) {
74 if (remaining <= 0) {
75 break;
76 }
77
78 const spanWidth = measureTextWidth(span.text);
79 if (spanWidth === 0) {
80 appendRenderSpan(sliced, span);
81 continue;
82 }
83
84 if (remainingOffset >= spanWidth) {
85 remainingOffset -= spanWidth;
86 continue;
87 }
88
89 const visible = sliceTextByWidth(span.text, remainingOffset, remaining);
90 remainingOffset = 0;
91
92 if (visible.text.length === 0) {
93 continue;
94 }
95
96 const nextSpan = {
97 ...span,
98 text: visible.text,
99 };
100
101 appendRenderSpan(sliced, nextSpan);
102
103 remaining -= visible.width;
104 usedWidth += visible.width;
105 }
106
107 return {
108 spans: sliced,
109 usedWidth,
110 };
111}
112
113const marker = diffRailMarker;
114const addNoteBadgeText = "[+]";

Callers 1

renderInlineSpansFunction · 0.85

Calls 3

measureTextWidthFunction · 0.90
sliceTextByWidthFunction · 0.90
appendRenderSpanFunction · 0.85

Tested by

no test coverage detected