diff --git a/src/table.tsx b/src/table.tsx index fd56dd7..ae55576 100644 --- a/src/table.tsx +++ b/src/table.tsx @@ -20,7 +20,7 @@ import { ScalarDict, TableProps, } from './types.js' -import {allKeysInCollection, getColumns, getHeadings, intersperse, sortData} from './utils.js' +import {allKeysInCollection, getColumns, getHeadings, intersperse, maybeStripAnsi, sortData} from './utils.js' /** * Determines the configured width based on the provided width value. @@ -64,23 +64,26 @@ function determineWidthToUse(columns: Column[], configuredWidth: number): export function Table(props: TableProps) { const { - borderColor, - borderStyle = 'all', data, filter, horizontalAlignment = 'left', maxWidth, + noStyle = false, orientation = 'horizontal', overflow = 'truncate', padding = 1, sort, title, - titleOptions, verticalAlignment = 'top', } = props - const headerOptions = {bold: true, color: 'blue', ...props.headerOptions} satisfies HeaderOptions - const processedData = sortData(filter ? data.filter((row) => filter(row)) : data, sort) + const headerOptions = noStyle ? {} : ({bold: true, color: 'blue', ...props.headerOptions} satisfies HeaderOptions) + const borderStyle = noStyle ? 'none' : (props.borderStyle ?? 'all') + const borderColor = noStyle ? undefined : props.borderColor + const borderProps = {color: borderColor} + const titleOptions = noStyle ? {} : props.titleOptions + + const processedData = maybeStripAnsi(sortData(filter ? data.filter((row) => filter(row)) : data, sort), noStyle) const config: Config = { borderStyle, columns: props.columns ?? allKeysInCollection(data), @@ -95,9 +98,6 @@ export function Table(props: TableProps) { const headings = getHeadings(config) const columns = getColumns(config, headings) - const borderProps = { - color: borderColor, - } const dataComponent = row({ borderProps, @@ -160,7 +160,7 @@ export function Table(props: TableProps) { borderLeft={false} borderRight={false} flexDirection="column" - borderStyle="single" + borderStyle={noStyle ? undefined : 'single'} borderColor={borderColor} > {/* print all data in key:value pairs */} diff --git a/src/types.ts b/src/types.ts index b3ac4e2..7265ec6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -206,6 +206,10 @@ export type TableProps = { * Styling options for the title of the table. */ titleOptions?: TextOptions + /** + * Disable all styling for the table. + */ + noStyle?: boolean } export type Config = { diff --git a/src/utils.ts b/src/utils.ts index 13ffaec..a2c33ca 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -155,3 +155,21 @@ export function getHeadings(config: Config): Partial }), ) as Partial } + +export function maybeStripAnsi(data: T, noStyle: boolean): T { + if (!noStyle) return data + + const newData = [] + + for (const row in data) { + if (row in data) { + const newRow = Object.fromEntries( + Object.entries(data[row]).map(([key, value]) => [key, typeof value === 'string' ? stripAnsi(value) : value]), + ) + + newData.push(newRow) + } + } + + return newData as T +}