Skip to content

Commit

Permalink
feat: add makeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 22, 2024
1 parent 2da3c0a commit 69af4d2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
43 changes: 43 additions & 0 deletions examples/make-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import ansis from 'ansis'
import terminalLink from 'terminal-link'

import {TableOptions, makeTable} from '../src/index.js'

const data = [
{
age: 20,
employed: ansis.bold('true'),
id: terminalLink('36329', 'https://example.com/36329'),
name: 'Alice',
},
{
age: 21,
employed: ansis.bold('true'),
id: terminalLink('49032', 'https://example.com/49032'),
name: ansis.dim('Bob'),
},
{
age: 22,
employed: ansis.bold('false'),
id: terminalLink('51786', 'https://example.com/51786'),
name: 'Charlie',
},
]

const basic: TableOptions<(typeof data)[number]> = {
borderStyle: 'all',
columns: ['id', {key: 'name', name: 'First Name'}, 'age', 'employed'],
data,
headerOptions: {
bold: true,
color: '#905de8',
formatter: 'sentenceCase',
},
sort: {
id: 'desc',
},
verticalAlignment: 'center',
}

const table = makeTable(basic)
console.log(table)
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {printTable, printTables} from './table.js'
export {makeTable, printTable, printTables} from './table.js'
export type {TableOptions} from './types.js'
30 changes: 28 additions & 2 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,12 @@ function renderTableInChunks<T extends Record<string, unknown>>(props: TableOpti
}

/**
* Renders a table with the given data.
* @param options see {@link TableOptions}
* Prints a table based on the provided options. If the data length exceeds 50,000 entries,
* the table is rendered in chunks to handle large datasets efficiently.
*
* @template T - A generic type that extends a record with string keys and unknown values.
* @param {TableOptions<T>} options - The options for rendering the table, including data and other configurations.
* @returns {void}
*/
export function printTable<T extends Record<string, unknown>>(options: TableOptions<T>): void {
if (options.data.length > 50_000) {
Expand All @@ -561,6 +565,20 @@ export function printTable<T extends Record<string, unknown>>(options: TableOpti
output.maybePrintLastFrame()
}

/**
* Generates a table as a string based on the provided options.
*
* @template T - A generic type extending a record with string keys and unknown values.
* @param {TableOptions<T>} options - The options to configure the table.
* @returns {string} The rendered table as a string.
*/
export function makeTable<T extends Record<string, unknown>>(options: TableOptions<T>): string {
const output = new Output()
const instance = render(<Table {...options} />, {stdout: output.stream})
instance.unmount()
return output.stream.lastFrame() ?? ''
}

function Container(props: ContainerProps) {
return (
<Box flexWrap="wrap" flexDirection={props.direction ?? 'row'} {...props}>
Expand All @@ -569,6 +587,14 @@ function Container(props: ContainerProps) {
)
}

/**
* Prints multiple tables to the console.
*
* @template T - An array of records where each record represents a table.
* @param {Object.<keyof T, TableOptions<T[keyof T]>>} tables - An object containing table options for each table.
* @param {Omit<ContainerProps, 'children'>} [options] - Optional container properties excluding 'children'.
* @throws {Error} Throws an error if the total number of rows across all tables exceeds 30,000.
*/
export function printTables<T extends Record<string, unknown>[]>(
tables: {[P in keyof T]: TableOptions<T[P]>},
options?: Omit<ContainerProps, 'children'>,
Expand Down

0 comments on commit 69af4d2

Please sign in to comment.