Skip to content

Commit

Permalink
Merge pull request #19 from oclif/mdonnalley/wrap-stdout
Browse files Browse the repository at this point in the history
fix: only print last valid frame
  • Loading branch information
iowillhoit authored Oct 8, 2024
2 parents 1fa57ce + b763bf7 commit cf7697f
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cliTruncate from 'cli-truncate'
import {Box, Text, render} from 'ink'
import {WriteStream} from 'node:tty'
import {sha1} from 'object-hash'
import React from 'react'
import stripAnsi from 'strip-ansi'
Expand Down Expand Up @@ -384,14 +385,33 @@ export function Skeleton(props: React.PropsWithChildren & {readonly height?: num
return <Box flexDirection="column">{texts}</Box>
}

/**
* A custom WriteStream that captures the frames written to stdout.
* This allows us to avoid an issue where Ink rerenders the component twice
* because it uses ansiEscapes.clearTerminal, which doesn't seem to have
* the desired effect in powershell.
*/
class Stdout extends WriteStream {
private frames: string[] = []
public lastFrame(): string | undefined {
return this.frames.filter((f) => stripAnsi(f) !== '').at(-1)
}

write(data: string): boolean {
this.frames.push(data)
return true
}
}

/**
* Renders a table with the given data.
* @param options see {@link TableOptions}
*/
export function printTable<T extends Record<string, unknown>>(options: TableOptions<T>): void {
const instance = render(<Table {...options} />)
const stdout = new Stdout(1)
const instance = render(<Table {...options} />, {stdout})
instance.unmount()
process.stdout.write('\n')
process.stdout.write(`${stdout.lastFrame()}\n`)
}

function Container(props: ContainerProps) {
Expand All @@ -406,6 +426,7 @@ export function printTables<T extends Record<string, unknown>[]>(
tables: {[P in keyof T]: TableOptions<T[P]>},
options?: Omit<ContainerProps, 'children'>,
): void {
const stdout = new Stdout(1)
const leftMargin = options?.marginLeft ?? options?.margin ?? 0
const rightMargin = options?.marginRight ?? options?.margin ?? 0
const columns = process.stdout.columns - (leftMargin + rightMargin)
Expand All @@ -422,7 +443,8 @@ export function printTables<T extends Record<string, unknown>[]>(
<Table key={sha1(table)} {...table} />
))}
</Container>,
{stdout},
)
instance.unmount()
process.stdout.write('\n')
process.stdout.write(`${stdout.lastFrame()}\n`)
}

0 comments on commit cf7697f

Please sign in to comment.