-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathlog-update.ts
52 lines (42 loc) · 1.07 KB
/
log-update.ts
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
import {type Writable} from 'node:stream';
import ansiEscapes from 'ansi-escapes';
import cliCursor from 'cli-cursor';
export type LogUpdate = {
clear: () => void;
done: () => void;
(str: string): void;
};
const create = (stream: Writable, {showCursor = false} = {}): LogUpdate => {
let previousLineCount = 0;
let previousOutput = '';
let hasHiddenCursor = false;
const render = (str: string) => {
if (!showCursor && !hasHiddenCursor) {
cliCursor.hide();
hasHiddenCursor = true;
}
const output = str + '\n';
if (output === previousOutput) {
return;
}
previousOutput = output;
stream.write(ansiEscapes.eraseLines(previousLineCount) + output);
previousLineCount = output.split('\n').length;
};
render.clear = () => {
stream.write(ansiEscapes.eraseLines(previousLineCount));
previousOutput = '';
previousLineCount = 0;
};
render.done = () => {
previousOutput = '';
previousLineCount = 0;
if (!showCursor) {
cliCursor.show();
hasHiddenCursor = false;
}
};
return render;
};
const logUpdate = {create};
export default logUpdate;