Skip to content

Commit

Permalink
feat: support noStyle option
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 12, 2024
1 parent b8985f6 commit 0de83e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -64,23 +64,26 @@ function determineWidthToUse<T>(columns: Column<T>[], configuredWidth: number):

export function Table<T extends ScalarDict>(props: TableProps<T>) {
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<T> = {
borderStyle,
columns: props.columns ?? allKeysInCollection(data),
Expand All @@ -95,9 +98,6 @@ export function Table<T extends ScalarDict>(props: TableProps<T>) {

const headings = getHeadings(config)
const columns = getColumns(config, headings)
const borderProps = {
color: borderColor,
}

const dataComponent = row<T>({
borderProps,
Expand Down Expand Up @@ -160,7 +160,7 @@ export function Table<T extends ScalarDict>(props: TableProps<T>) {
borderLeft={false}
borderRight={false}
flexDirection="column"
borderStyle="single"
borderStyle={noStyle ? undefined : 'single'}
borderColor={borderColor}
>
{/* print all data in key:value pairs */}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ export type TableProps<T extends ScalarDict> = {
* Styling options for the title of the table.
*/
titleOptions?: TextOptions
/**
* Disable all styling for the table.
*/
noStyle?: boolean
}

export type Config<T> = {
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,21 @@ export function getHeadings<T extends ScalarDict>(config: Config<T>): Partial<T>
}),
) as Partial<T>
}

export function maybeStripAnsi<T extends ScalarDict[]>(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
}

0 comments on commit 0de83e9

Please sign in to comment.