From 1d44eb32680c15e428f9f0d7b77a4fc8483f383c Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 8 Oct 2024 10:42:53 -0600 Subject: [PATCH] fix: dont use stream if test --- src/table.tsx | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/table.tsx b/src/table.tsx index d7b38d3..366ae4b 100644 --- a/src/table.tsx +++ b/src/table.tsx @@ -393,6 +393,7 @@ export function Skeleton(props: React.PropsWithChildren & {readonly height?: num */ class Stream extends WriteStream { private frames: string[] = [] + public lastFrame(): string | undefined { return this.frames.filter((f) => stripAnsi(f) !== '').at(-1) } @@ -403,12 +404,20 @@ class Stream extends WriteStream { } } -function makeOptions() { - if (process.env.NODE_ENV === 'test') { - return {} +class Output { + public stream: Stream | WriteStream + + public constructor(fd = 1) { + this.stream = process.env.NODE_ENV === 'test' ? process.stdout : new WriteStream(fd) } - return {stdout: new Stream(1)} + public maybePrintLastFrame() { + if (this.stream instanceof Stream) { + process.stdout.write(`${this.stream.lastFrame()}\n`) + } else { + process.stdout.write('\n') + } + } } /** @@ -416,10 +425,10 @@ function makeOptions() { * @param options see {@link TableOptions} */ export function printTable>(options: TableOptions): void { - const stdout = new Stream(0) - const instance = render(, {...makeOptions()}) + const output = new Output() + const instance = render(
, {stdout: output.stream}) instance.unmount() - process.stdout.write(`${stdout.lastFrame()}\n`) + output.maybePrintLastFrame() } function Container(props: ContainerProps) { @@ -434,7 +443,7 @@ export function printTables[]>( tables: {[P in keyof T]: TableOptions}, options?: Omit, ): void { - const stdout = new Stream(0) + const output = new Output() const leftMargin = options?.marginLeft ?? options?.margin ?? 0 const rightMargin = options?.marginRight ?? options?.margin ?? 0 const columns = process.stdout.columns - (leftMargin + rightMargin) @@ -451,8 +460,8 @@ export function printTables[]>(
))} , - {...makeOptions()}, + {stdout: output.stream}, ) instance.unmount() - process.stdout.write(`${stdout.lastFrame()}\n`) + output.maybePrintLastFrame() }