Skip to content

Commit 95faad8

Browse files
committed
Preserve out-of-bound writes to stdout/err
1 parent bfb4867 commit 95faad8

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

packages/knip/src/ConsoleStreamer.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export class ConsoleStreamer {
77
isEnabled = false;
88
isWatch = false;
99
private lines = 0;
10+
private stdoutBaseline = 0;
11+
private stderrBaseline = 0;
1012

1113
constructor(options: MainOptions) {
1214
this.isEnabled = options.isShowProgress;
1315
this.isWatch = options.isWatch;
16+
this.snapshot();
1417
}
1518

1619
private clearLines(count: number) {
@@ -27,6 +30,18 @@ export class ConsoleStreamer {
2730
process.stdout.write('\x1b[2J\x1b[1;1f');
2831
}
2932

33+
private snapshot() {
34+
this.stdoutBaseline = process.stdout.bytesWritten ?? 0;
35+
this.stderrBaseline = process.stderr.bytesWritten ?? 0;
36+
}
37+
38+
private hadExternalWrites() {
39+
return (
40+
(process.stdout.bytesWritten ?? 0) > this.stdoutBaseline ||
41+
(process.stderr.bytesWritten ?? 0) > this.stderrBaseline
42+
);
43+
}
44+
3045
private update(messages: string[]) {
3146
this.clear();
3247
process.stdout.write(`${messages.join('\n')}\n`);
@@ -35,13 +50,18 @@ export class ConsoleStreamer {
3550

3651
cast(message: string | string[], sub?: string) {
3752
if (!this.isEnabled) return;
53+
if (this.hadExternalWrites()) this.lines = 0;
3854
if (Array.isArray(message)) this.update(message);
3955
else this.update([`${message}${!sub || sub === '.' ? '' : ` (${sub})`}…`]);
56+
this.snapshot();
4057
}
4158

4259
clear() {
4360
if (!this.isEnabled) return;
61+
if (this.hadExternalWrites()) this.lines = 0;
4462
if (this.isWatch) this.clearScreen();
4563
else this.clearLines(this.lines);
64+
this.lines = 0;
65+
this.snapshot();
4666
}
4767
}

0 commit comments

Comments
 (0)