UNPKG

easy-cli-framework

Version:

A framework for building CLI applications that are robust and easy to maintain. Supports theming, configuration files, interactive prompts, and more.

111 lines (110 loc) 3.01 kB
import { DisplayOptions, EasyCLITheme } from '.'; /** * A column in a themed table * * @template TItem The object type for the items in the table * @interface ThemedTableColumn * * @property {string} name The name of the column * @property {(item: TItem) => string} data A function that returns the value to display * @property {DisplayOptions | ((item: TItem) => DisplayOptions)} [style] The style for the column * @property {DisplayOptions} [headerStyle] The style for the header * @property {number} [width] The width of the column * @property {'left' | 'middle' | 'right'} [align] The alignment of the column * * @example * ```typescript * { * name: 'Name', * data: item => item.name * style: item => item.age > 30 ? 'warn' : 'default', * headerStyle: 'info', * width: 20, * }; * ``` */ export type ThemedTableColumn<TItem = Record<string, any>> = { name: string; data: (item: TItem) => string | number; style?: DisplayOptions | ((item: TItem) => DisplayOptions); headerStyle?: DisplayOptions; width?: number; align?: 'left' | 'middle' | 'right'; }; /** * Options for the themed table * * @template TItem * @interface ThemedTableOptions * @type {object} * * @property {EasyCLITheme} theme The theme to use * @property {ThemedTableColumn<TItem>[]} columns The columns for the table * @property {number} [totalWidth=120] The total width of the table * * @example * ```typescript * { * theme: new EasyCLITheme(), * columns: [ * { name: 'Name', data: item => item.name }, * { name: 'Age', data: item => item.age }, * ], * } * ``` */ export type ThemedTableOptions<TItem = Record<string, any>> = { theme: EasyCLITheme; columns: ThemedTableColumn<TItem>[]; totalWidth?: number; }; /** * A themed table that extends a cli-table * @template TItem * @class ThemedTable * * @param {ThemedTableOptions<TItem>} options The options for the themed table * * @example * ```typescript * const theme = new EasyCLITheme(); * const table = new ThemedTable({ * theme, * columns: [ * { name: 'Name', data: item => item.name }, * { name: 'Age', data: item => item.age }, * ], * }); * * table.render([ * { name: 'Alice', age: 25 }, * { name: 'Bob', age: 30 }, * ]); *``` */ export declare class ThemedTable<TItem extends Record<string, any>> { private theme; private columns; private totalWidth; constructor({ theme, columns, totalWidth }: ThemedTableOptions<TItem>); /** * Calculate the remaining default column width * @private * @returns {number} The remaining default column width */ private calculateRemaingingDefaultColumnWidth; /** * Render the table to the console * * @param {TItem[]} items The items to render * * @example * ```typescript * table.render([ * { name: 'Alice', age: 25 }, * { name: 'Bob', age: 30 }, * ]); * ``` */ render(items: TItem[]): void; }