console-table-printer
Advanced tools
Comparing version
@@ -5,37 +5,91 @@ import { ColorMap } from '../utils/colored-console-line'; | ||
export { ALIGNMENT, COLOR }; | ||
/** | ||
* Configuration options for a table column | ||
*/ | ||
export interface ColumnOptionsRaw { | ||
/** Unique identifier for the column */ | ||
name: string; | ||
/** Display name for the column header. If not provided, uses the name property */ | ||
title?: string; | ||
/** Text alignment within the column: 'left', 'center', or 'right' */ | ||
alignment?: ALIGNMENT; | ||
/** Text color for the column content */ | ||
color?: COLOR; | ||
/** Maximum length of text in the column. Longer text will be wrapped to multiple lines */ | ||
maxLen?: number; | ||
/** Minimum length of text in the column. Shorter text will be padded with spaces */ | ||
minLen?: number; | ||
} | ||
/** | ||
* Configuration for a computed column that generates values dynamically | ||
*/ | ||
export interface ComputedColumn extends ColumnOptionsRaw { | ||
/** Function that computes the column value for each row | ||
* @param arg0 - The current row data object | ||
* @param index - The index of the current row in the data array | ||
* @param array - The complete array of row data | ||
* @returns The computed value for this column | ||
*/ | ||
function: (arg0: any, index: number, array: any[]) => any; | ||
} | ||
/** | ||
* Function type for sorting table rows | ||
* @param row1 - First row to compare | ||
* @param row2 - Second row to compare | ||
* @returns Negative number if row1 should come before row2, positive if row2 should come before row1, 0 if equal | ||
*/ | ||
export type RowSortFunction = (row1: any, row2: any) => number; | ||
/** | ||
* Function type for filtering table rows | ||
* @param row - The row data to evaluate | ||
* @returns True if the row should be included, false if it should be filtered out | ||
*/ | ||
export type RowFilterFunction = (row: any) => boolean; | ||
/** | ||
* Default styling options applied to all columns unless overridden | ||
*/ | ||
export interface DefaultColumnOptions { | ||
/** Default text alignment for all columns */ | ||
alignment?: ALIGNMENT; | ||
/** Default text color for all columns */ | ||
color?: COLOR; | ||
/** Default title prefix for all columns */ | ||
title?: string; | ||
/** Default maximum length for all columns */ | ||
maxLen?: number; | ||
/** Default minimum length for all columns */ | ||
minLen?: number; | ||
} | ||
/** | ||
* Complete configuration options for table creation and styling | ||
*/ | ||
export interface ComplexOptions { | ||
/** Table styling configuration including borders and colors */ | ||
style?: TableStyleDetails; | ||
/** Title displayed at the top of the table */ | ||
title?: string; | ||
/** Array of column configurations */ | ||
columns?: ColumnOptionsRaw[]; | ||
/** Initial data rows for the table */ | ||
rows?: Dictionary[]; | ||
/** Function to sort rows before display */ | ||
sort?: RowSortFunction; | ||
/** Function to filter rows before display */ | ||
filter?: RowFilterFunction; | ||
/** Array of column names to include (all others will be hidden) */ | ||
enabledColumns?: string[]; | ||
/** Array of column names to exclude (these will always be hidden) */ | ||
disabledColumns?: string[]; | ||
/** Array of computed columns that generate values dynamically */ | ||
computedColumns?: ComputedColumn[]; | ||
/** Whether to add separator lines between rows */ | ||
rowSeparator?: boolean; | ||
/** Whether to disable color output (useful for non-color terminals) */ | ||
shouldDisableColors?: boolean; | ||
/** Custom color mapping for special characters or values */ | ||
colorMap?: ColorMap; | ||
/** Custom character length mapping for special characters (e.g., emojis) */ | ||
charLength?: CharLengthDict; | ||
/** Default options applied to all columns unless overridden */ | ||
defaultColumnOptions?: DefaultColumnOptions; | ||
} |
{ | ||
"name": "console-table-printer", | ||
"version": "2.14.3", | ||
"version": "2.14.4", | ||
"repository": "github:console-table-printer/console-table-printer", | ||
@@ -33,2 +33,3 @@ "description": "Printing pretty tables on console log", | ||
"@types/jest": "^29.5.14", | ||
"@types/node": "^24.0.3", | ||
"eslint": "^9.28.0", | ||
@@ -35,0 +36,0 @@ "eslint-config-prettier": "^10.1.5", |
@@ -25,13 +25,14 @@ <h1 align="center">console-table-printer</h1> | ||
//Create a table | ||
const testCases = [ | ||
{ Rank: 3, text: 'I would like some Yellow', value: 100 }, | ||
{ Rank: 4, text: 'I hope batch update is working', value: 300 }, | ||
// Create a simple task list | ||
const tasks = [ | ||
{ id: 1, task: 'Fix login bug', priority: 'High', status: 'In Progress' }, | ||
{ id: 2, task: 'Update documentation', priority: 'Medium', status: 'Done' }, | ||
{ id: 3, task: 'Add unit tests', priority: 'High', status: 'Todo' }, | ||
]; | ||
printTable(testCases); | ||
// Print the table | ||
printTable(tasks); | ||
``` | ||
 | ||
 | ||
@@ -45,19 +46,18 @@ ## 🚨🚨Announcement🚨🚨 Official Documentation is moved [Here](https://console-table.netlify.app/docs) | ||
//Create a table | ||
const p = new Table(); | ||
// Create a game leaderboard | ||
const leaderboard = new Table(); | ||
// add rows with color | ||
p.addRow({ Record: 'a', text: 'red wine please', value: 10.212 }); | ||
p.addRow({ Record: 'b', text: 'green gemuse please', value: 20.0 }); | ||
p.addRows([ | ||
// adding multiple rows are possible | ||
{ Record: 'c', text: 'gelb bananen bitte', value: 100 }, | ||
{ Record: 'd', text: 'update is working', value: 300 }, | ||
// Add players with their scores | ||
leaderboard.addRow({ rank: 1, player: 'Alice', score: 1250, level: 'Master' }); | ||
leaderboard.addRow({ rank: 2, player: 'Bob', score: 1180, level: 'Expert' }); | ||
leaderboard.addRows([ | ||
{ rank: 3, player: 'Charlie', score: 1050, level: 'Advanced' }, | ||
{ rank: 4, player: 'Diana', score: 920, level: 'Intermediate' }, | ||
]); | ||
p.printTable(); | ||
// Print the leaderboard | ||
leaderboard.printTable(); | ||
``` | ||
 | ||
 | ||
@@ -68,9 +68,10 @@ You can also put some color to your table like this: | ||
const p = new Table(); | ||
p.addRow({ description: 'red wine', value: 10.212 }, { color: 'red' }); | ||
p.addRow({ description: 'green gemuse', value: 20.0 }, { color: 'green' }); | ||
p.addRow({ description: 'gelb bananen', value: 100 }, { color: 'yellow' }); | ||
p.addRow({ item: 'Pizza', price: 12.99, rating: '5/5' }, { color: 'red' }); | ||
p.addRow({ item: 'Burger', price: 8.99, rating: '4/5' }, { color: 'green' }); | ||
p.addRow({ item: 'Ramen', price: 15.99, rating: '5/5' }, { color: 'yellow' }); | ||
p.addRow({ item: 'Salad', price: 6.99, rating: '3/5' }, { color: 'cyan' }); | ||
p.printTable(); | ||
``` | ||
 | ||
 | ||
@@ -81,22 +82,17 @@ You can also put properties based on columns (color/alignment/title) | ||
const p = new Table({ | ||
title: 'Project Status', | ||
columns: [ | ||
{ name: 'id', alignment: 'left', color: 'blue' }, // with alignment and color | ||
{ name: 'text', alignment: 'right' }, | ||
{ name: 'is_priority_today', title: 'Is This Priority?' }, // with Title as separate Text | ||
{ name: 'id', alignment: 'left', color: 'blue' }, | ||
{ name: 'project', alignment: 'left' }, | ||
{ name: 'status', title: 'Current Status' }, | ||
], | ||
colorMap: { | ||
custom_green: '\x1b[32m', // define customized color | ||
urgent: '\x1b[31m', | ||
on_track: '\x1b[32m', | ||
}, | ||
}); | ||
p.addRow({ id: 1, text: 'red wine', value: 10.212 }, { color: 'green' }); | ||
p.addRow( | ||
{ id: 2, text: 'green gemuse', value: 20.0 }, | ||
{ color: 'custom_green' } // your green | ||
); | ||
p.addRow( | ||
{ id: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' }, | ||
{ color: 'yellow' } | ||
); | ||
p.addRow({ id: 3, text: 'rosa hemd wie immer', value: 100 }, { color: 'cyan' }); | ||
p.addRow({ id: 1, project: 'Website Redesign', status: 'On Track' }, { color: 'on_track' }); | ||
p.addRow({ id: 2, project: 'Mobile App', status: 'Behind Schedule' }, { color: 'urgent' }); | ||
p.addRow({ id: 3, project: 'API Integration', status: 'Completed' }, { color: 'green' }); | ||
@@ -106,3 +102,3 @@ p.printTable(); | ||
 | ||
 | ||
@@ -129,15 +125,19 @@ ## CLI | ||
new Table({ | ||
title: 'Title of the Table', // A text showsup on top of table (optoinal) | ||
title: '📊 Sales Report Q4 2024', // A text showsup on top of table (optional) | ||
columns: [ | ||
{ name: 'column1', alignment: 'left', color: 'red' }, // with alignment and color | ||
{ name: 'column2', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines | ||
{ name: 'column3', title: 'Column3' }, // Title is what will be shown while printing, by default title = name | ||
{ name: 'region', alignment: 'left', color: 'blue' }, // with alignment and color | ||
{ name: 'sales', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines | ||
{ name: 'growth', title: 'Growth %' }, // Title is what will be shown while printing, by default title = name | ||
], | ||
rows: [{ column1: 'row1' }, { column2: 'row2' }, { column3: 'row3' }], | ||
sort: (row1, row2) => row2.column1 - row1.column1, // sorting order of rows (optional), this is normal js sort function for Array.sort | ||
filter: (row) => row.column1 < 3, // filtering rows (optional) | ||
enabledColumns: ['column1'], // array of columns that you want to see, all other will be ignored (optional) | ||
disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden | ||
rows: [ | ||
{ region: 'North America', sales: '$2.5M', growth: '+15%' }, | ||
{ region: 'Europe', sales: '$1.8M', growth: '+8%' }, | ||
{ region: 'Asia Pacific', sales: '$3.2M', growth: '+22%' }, | ||
], | ||
sort: (row1, row2) => row2.sales - row1.sales, // sorting order of rows (optional), this is normal js sort function for Array.sort | ||
filter: (row) => row.growth > '+10%', // filtering rows (optional) | ||
enabledColumns: ['region', 'sales'], // array of columns that you want to see, all other will be ignored (optional) | ||
disabledColumns: ['growth'], // array of columns that you DONT want to see, these will always be hidden | ||
colorMap: { | ||
custom_green: '\x1b[32m', // define customized color | ||
high_growth: '\x1b[32m', // define customized color | ||
}, | ||
@@ -144,0 +144,0 @@ charLength: { |
48833
6.14%867
6.64%16
6.67%