-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.js
More file actions
318 lines (257 loc) · 8.64 KB
/
Copy pathindex.js
File metadata and controls
318 lines (257 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import process from 'node:process';
import ansiEscapes from 'ansi-escapes';
import cliCursor from 'cli-cursor';
import wrapAnsi from 'wrap-ansi';
import sliceAnsi from 'slice-ansi';
import stripAnsi from 'strip-ansi';
import stringWidth from 'string-width';
const getWidth = (stream, defaultWidth) => stream.columns ?? defaultWidth ?? 80;
const SYNCHRONIZED_OUTPUT_ENABLE = '\u001B[?2026h';
const SYNCHRONIZED_OUTPUT_DISABLE = '\u001B[?2026l';
const countLines = text => text.split('\n').length;
const getFrameHeight = text => {
const unstyledText = stripAnsi(text);
return unstyledText === '' ? 0 : unstyledText.split('\n').length;
};
const fitToTerminalHeight = (stream, wrappedText, defaultHeight) => {
const terminalHeight = stream.rows ?? defaultHeight ?? 24;
// Zero height: intentionally produce no output
if (terminalHeight === 0) {
return {text: '', wasClipped: wrappedText !== ''};
}
const unstyled = stripAnsi(wrappedText);
const lines = unstyled.split('\n');
const toRemove = Math.max(0, lines.length - terminalHeight);
if (toRemove === 0) {
return {text: wrappedText, wasClipped: false};
}
// Compute visible-column cut to match sliceAnsi's position tracking.
// sliceAnsi counts each \n as 1 column, but stringWidth returns 0 for \n.
let cut = 0;
for (let index = 0; index < toRemove; index++) {
cut += stringWidth(lines[index]) + 1;
}
let clippedText = sliceAnsi(wrappedText, cut);
let clippedLineCount = getFrameHeight(clippedText);
// Normalize the estimated cut so it matches sliceAnsi's width model.
while (clippedLineCount > terminalHeight) {
cut++;
clippedText = sliceAnsi(wrappedText, cut);
clippedLineCount = getFrameHeight(clippedText);
}
while (cut > 0) {
const previousClippedText = sliceAnsi(wrappedText, cut - 1);
if (getFrameHeight(previousClippedText) > terminalHeight) {
break;
}
cut--;
clippedText = previousClippedText;
}
return {text: clippedText, wasClipped: cut > 0};
};
/**
Find common prefix and suffix between frames.
*/
const diffFrames = (previousLines, nextLines) => {
let start = 0;
for (; start < previousLines.length && start < nextLines.length; start++) {
if (previousLines[start] !== nextLines[start]) {
break;
}
}
let endPrevious = previousLines.length - 1;
let endNext = nextLines.length - 1;
while (endPrevious >= start && endNext >= start && previousLines[endPrevious] === nextLines[endNext]) {
endPrevious--;
endNext--;
}
return {start, endPrevious, endNext};
};
/**
Build a single escape sequence patch to transform previous -> next.
*/
const buildPatch = ({
prevCount,
start,
endPrevious,
endNext,
nextLines,
nextWrappedEndsWithNewline,
}) => {
let sequence = '';
// Move cursor from the trailing blank line to the first changed line.
const cursorLine = prevCount - 1;
const cursorLineDelta = start - cursorLine;
if (cursorLineDelta > 0) {
sequence += ansiEscapes.cursorDown(cursorLineDelta);
}
if (cursorLineDelta < 0) {
sequence += ansiEscapes.cursorUp(-cursorLineDelta);
}
sequence += ansiEscapes.cursorLeft;
// Clear the changed block from the previous frame.
const linesToClear = Math.max(0, endPrevious - start + 1);
for (let index = 0; index < linesToClear; index++) {
sequence += ansiEscapes.eraseLine;
if (index < linesToClear - 1) {
sequence += ansiEscapes.cursorDown();
}
}
if (linesToClear > 1) {
sequence += ansiEscapes.cursorUp(linesToClear - 1);
}
sequence += ansiEscapes.cursorLeft;
// Write the new changed block.
const wroteSlice = nextLines.slice(start, endNext + 1);
let writtenLineBreakCount = 0;
if (wroteSlice.length > 0) {
const chunk = wroteSlice.join('\n');
const shouldWriteTrailingNewline = nextWrappedEndsWithNewline
&& endNext < nextLines.length - 1
&& !chunk.endsWith('\n');
sequence += chunk;
writtenLineBreakCount = countLines(chunk) - 1;
// Ensure we do not leave trailing characters on the last written line
sequence += ansiEscapes.eraseEndLine;
if (shouldWriteTrailingNewline) {
sequence += '\n';
writtenLineBreakCount++;
}
}
// Reposition cursor to the final trailing blank line for the next call
const currentLine = start + writtenLineBreakCount;
const finalLine = nextLines.length - 1;
const lineDelta = finalLine - currentLine;
if (lineDelta > 0) {
sequence += ansiEscapes.cursorDown(lineDelta);
}
if (lineDelta < 0) {
sequence += ansiEscapes.cursorUp(-lineDelta);
}
return sequence;
};
export function createLogUpdate(stream, {showCursor = false, defaultWidth, defaultHeight} = {}) {
let previousLineCount = 0;
let previousWidth = getWidth(stream, defaultWidth);
let previousOutput = '';
const useSynchronizedOutput = stream.isTTY === true;
const write = output => {
if (output === '') {
return;
}
if (useSynchronizedOutput) {
stream.write(SYNCHRONIZED_OUTPUT_ENABLE + output + SYNCHRONIZED_OUTPUT_DISABLE);
return;
}
stream.write(output);
};
/**
Normalize, wrap, and height-clip into a concrete frame.
Returns both the wrapped string and an array of lines, where an empty frame (for `rows === 0`) is represented by `lines.length === 0`.
*/
const computeFrame = (text, width, clipToHeight = true) => {
// Preserve user's trailing newlines, ensure at least one
const textString = String(text);
const raw = textString.endsWith('\n') ? textString : `${textString}\n`;
const wrapped = wrapAnsi(raw, width, {trim: false, hard: true, wordWrap: false});
const {text: frameText, wasClipped} = clipToHeight ? fitToTerminalHeight(stream, wrapped, defaultHeight) : {text: wrapped, wasClipped: false};
// Derive lines. Special-case empty string to represent 0 lines.
const lines = frameText === '' ? [] : frameText.split('\n');
return {wrapped: frameText, lines, wasClipped};
};
const reset = () => {
previousOutput = '';
previousWidth = getWidth(stream, defaultWidth);
previousLineCount = 0;
};
const render = (...arguments_) => {
if (!showCursor) {
cliCursor.hide();
}
const width = getWidth(stream, defaultWidth);
const {wrapped, lines, wasClipped} = computeFrame(arguments_.join(' '), width);
// If nothing would be written (rows === 0 after clipping), erase previous output and update state.
if (lines.length === 0) {
if (previousLineCount > 0) {
write(ansiEscapes.eraseLines(previousLineCount));
}
previousOutput = wrapped;
previousWidth = width;
previousLineCount = 0;
return;
}
// Fast no-op path
if (wrapped === previousOutput && previousWidth === width) {
return;
}
// First frame: just write
if (previousLineCount === 0) {
write(wrapped);
previousOutput = wrapped;
previousWidth = width;
previousLineCount = lines.length;
return;
}
// Width changed or content clipped: full erase + write (diffing is invalid across wraps/clips)
if (previousWidth !== width || wasClipped) {
write(ansiEscapes.eraseLines(previousLineCount) + wrapped);
previousOutput = wrapped;
previousWidth = width;
previousLineCount = lines.length;
return;
}
const previousLines = previousOutput === '' ? [] : previousOutput.split('\n');
let {start, endPrevious, endNext} = diffFrames(previousLines, lines);
// When lines are inserted or removed, the suffix shifts position.
// Include it in the rewrite so it renders at the correct row.
if (previousLines.length !== lines.length) {
endPrevious = previousLines.length - 1;
endNext = lines.length - 1;
}
// If nothing changed (including trailing blank logic), bail
if (start === lines.length && previousLineCount === lines.length) {
return;
}
// If common prefix length is zero, simpler and correct to full erase.
if (start === 0) {
write(ansiEscapes.eraseLines(previousLineCount) + wrapped);
previousOutput = wrapped;
previousWidth = width;
previousLineCount = lines.length;
return;
}
const patch = buildPatch({
prevCount: previousLineCount,
start,
endPrevious,
endNext,
nextLines: lines,
nextWrappedEndsWithNewline: wrapped.endsWith('\n'),
});
write(patch);
previousOutput = wrapped;
previousWidth = width;
previousLineCount = lines.length;
};
render.clear = () => {
write(ansiEscapes.eraseLines(previousLineCount));
reset();
};
render.done = () => {
reset();
if (!showCursor) {
cliCursor.show();
}
};
render.persist = (...arguments_) => {
const erasePrevious = previousLineCount > 0 ? ansiEscapes.eraseLines(previousLineCount) : '';
const width = getWidth(stream, defaultWidth);
const {wrapped: wrappedText} = computeFrame(arguments_.join(' '), width, false);
write(erasePrevious + wrappedText);
reset();
};
return render;
}
const logUpdate = createLogUpdate(process.stdout);
export default logUpdate;
export const logUpdateStderr = createLogUpdate(process.stderr);