| 13 | }>; |
| 14 | |
| 15 | export class BlessedTtyMeasuringStream extends Writable implements BlessedMeasuringOutput { |
| 16 | writeCount = 0; |
| 17 | totalBytes = 0; |
| 18 | |
| 19 | columns = process.stdout.columns ?? 120; |
| 20 | rows = process.stdout.rows ?? 40; |
| 21 | isTTY = true as const; |
| 22 | fd = (process.stdout as unknown as { fd?: number }).fd ?? 1; |
| 23 | |
| 24 | private readonly writeResolvers: Array<() => void> = []; |
| 25 | |
| 26 | override _write( |
| 27 | chunk: Buffer | string, |
| 28 | encoding: BufferEncoding, |
| 29 | callback: (error?: Error | null) => void, |
| 30 | ): void { |
| 31 | this.writeCount++; |
| 32 | this.totalBytes += |
| 33 | typeof chunk === "string" ? Buffer.byteLength(chunk, encoding) : chunk.byteLength; |
| 34 | |
| 35 | const done = (err?: Error | null) => { |
| 36 | const r = this.writeResolvers.shift(); |
| 37 | if (r) r(); |
| 38 | callback(err ?? null); |
| 39 | }; |
| 40 | |
| 41 | try { |
| 42 | const out = process.stdout as unknown as NodeJS.WriteStream; |
| 43 | if (typeof chunk === "string") out.write(chunk, encoding, done); |
| 44 | else out.write(chunk, done); |
| 45 | } catch (err) { |
| 46 | done(err instanceof Error ? err : new Error(String(err))); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | waitForWrite(): Promise<void> { |
| 51 | return new Promise<void>((resolve) => this.writeResolvers.push(resolve)); |
| 52 | } |
| 53 | |
| 54 | reset(): void { |
| 55 | this.writeCount = 0; |
| 56 | this.totalBytes = 0; |
| 57 | this.writeResolvers.length = 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export function createBlessedOutput(): BlessedMeasuringOutput { |
| 62 | if (process.stdout.isTTY !== true) { |
nothing calls this directly
no outgoing calls
no test coverage detected