| 27 | * forwards all other intercepted `stdout` and `stderr` logs above it. |
| 28 | */ |
| 29 | export class TTYWindowRenderer { |
| 30 | private options: Required<Options>; |
| 31 | private streams!: Record<StreamType, Writable['write']>; |
| 32 | private buffer: { type: StreamType; message: string }[] = []; |
| 33 | private renderInterval: NodeJS.Timeout | undefined = undefined; |
| 34 | |
| 35 | private windowHeight = 0; |
| 36 | private finished = false; |
| 37 | private cleanups: (() => void)[] = []; |
| 38 | |
| 39 | constructor(options: Options) { |
| 40 | this.options = { |
| 41 | interval: DEFAULT_RENDER_INTERVAL, |
| 42 | ...options, |
| 43 | }; |
| 44 | |
| 45 | this.streams = { |
| 46 | output: options.outputStream.write.bind(options.outputStream), |
| 47 | error: options.errorStream.write.bind(options.errorStream), |
| 48 | }; |
| 49 | |
| 50 | this.cleanups.push( |
| 51 | this.interceptStream(process.stdout, 'output'), |
| 52 | this.interceptStream(process.stderr, 'error'), |
| 53 | ); |
| 54 | |
| 55 | restoreCursor(); |
| 56 | this.write(HIDE_CURSOR, 'output'); |
| 57 | |
| 58 | this.start(); |
| 59 | } |
| 60 | |
| 61 | start() { |
| 62 | this.finished = false; |
| 63 | this.renderInterval = setInterval( |
| 64 | () => this.flushBuffer(), |
| 65 | this.options.interval, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | stop() { |
| 70 | this.flushBuffer(); |
| 71 | // this.clearWindow(); |
| 72 | this.write(SHOW_CURSOR, 'output'); |
| 73 | this.cleanups.splice(0).map((fn) => fn()); |
| 74 | clearInterval(this.renderInterval); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Write all buffered output and stop buffering. |
| 79 | * All intercepted writes are forwarded to actual write after this. |
| 80 | */ |
| 81 | finish() { |
| 82 | this.finished = true; |
| 83 | this.flushBuffer(); |
| 84 | clearInterval(this.renderInterval); |
| 85 | } |
| 86 |
nothing calls this directly
no outgoing calls
no test coverage detected