console-table-printer 2.14.2 → 2.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -48
- package/dist/src/models/external-table.d.ts +55 -1
- package/dist/src/utils/console-utils.js +1 -1
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -22,17 +22,18 @@ npm install console-table-printer --save
|
|
|
22
22
|
```javascript
|
|
23
23
|
const { printTable } = require('console-table-printer');
|
|
24
24
|
|
|
25
|
-
//Create a
|
|
26
|
-
const
|
|
27
|
-
{
|
|
28
|
-
{
|
|
25
|
+
// Create a simple task list
|
|
26
|
+
const tasks = [
|
|
27
|
+
{ id: 1, task: 'Fix login bug', priority: 'High', status: 'In Progress' },
|
|
28
|
+
{ id: 2, task: 'Update documentation', priority: 'Medium', status: 'Done' },
|
|
29
|
+
{ id: 3, task: 'Add unit tests', priority: 'High', status: 'Todo' },
|
|
29
30
|
];
|
|
30
31
|
|
|
31
|
-
//
|
|
32
|
-
printTable(
|
|
32
|
+
// Print the table
|
|
33
|
+
printTable(tasks);
|
|
33
34
|
```
|
|
34
35
|
|
|
35
|
-

|
|
36
37
|
|
|
37
38
|
## 🚨🚨Announcement🚨🚨 Official Documentation is moved [Here](https://console-table.netlify.app/docs)
|
|
38
39
|
|
|
@@ -41,65 +42,60 @@ You can also create a Table instance and print it:
|
|
|
41
42
|
```javascript
|
|
42
43
|
const { Table } = require('console-table-printer');
|
|
43
44
|
|
|
44
|
-
//Create a
|
|
45
|
-
const
|
|
45
|
+
// Create a game leaderboard
|
|
46
|
+
const leaderboard = new Table();
|
|
46
47
|
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
{ Record: 'd', text: 'update is working', value: 300 },
|
|
48
|
+
// Add players with their scores
|
|
49
|
+
leaderboard.addRow({ rank: 1, player: 'Alice', score: 1250, level: 'Master' });
|
|
50
|
+
leaderboard.addRow({ rank: 2, player: 'Bob', score: 1180, level: 'Expert' });
|
|
51
|
+
leaderboard.addRows([
|
|
52
|
+
{ rank: 3, player: 'Charlie', score: 1050, level: 'Advanced' },
|
|
53
|
+
{ rank: 4, player: 'Diana', score: 920, level: 'Intermediate' },
|
|
54
54
|
]);
|
|
55
55
|
|
|
56
|
-
//
|
|
57
|
-
|
|
56
|
+
// Print the leaderboard
|
|
57
|
+
leaderboard.printTable();
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-

|
|
61
61
|
|
|
62
62
|
You can also put some color to your table like this:
|
|
63
63
|
|
|
64
64
|
```javascript
|
|
65
65
|
const p = new Table();
|
|
66
|
-
p.addRow({
|
|
67
|
-
p.addRow({
|
|
68
|
-
p.addRow({
|
|
66
|
+
p.addRow({ item: 'Pizza', price: 12.99, rating: '5/5' }, { color: 'red' });
|
|
67
|
+
p.addRow({ item: 'Burger', price: 8.99, rating: '4/5' }, { color: 'green' });
|
|
68
|
+
p.addRow({ item: 'Ramen', price: 15.99, rating: '5/5' }, { color: 'yellow' });
|
|
69
|
+
p.addRow({ item: 'Salad', price: 6.99, rating: '3/5' }, { color: 'cyan' });
|
|
69
70
|
p.printTable();
|
|
70
71
|
```
|
|
71
72
|
|
|
72
|
-

|
|
73
74
|
|
|
74
75
|
You can also put properties based on columns (color/alignment/title)
|
|
75
76
|
|
|
76
77
|
```javascript
|
|
77
78
|
const p = new Table({
|
|
79
|
+
title: 'Project Status',
|
|
78
80
|
columns: [
|
|
79
|
-
{ name: 'id', alignment: 'left', color: 'blue' },
|
|
80
|
-
{ name: '
|
|
81
|
-
{ name: '
|
|
81
|
+
{ name: 'id', alignment: 'left', color: 'blue' },
|
|
82
|
+
{ name: 'project', alignment: 'left' },
|
|
83
|
+
{ name: 'status', title: 'Current Status' },
|
|
82
84
|
],
|
|
83
85
|
colorMap: {
|
|
84
|
-
|
|
86
|
+
urgent: '\x1b[31m',
|
|
87
|
+
on_track: '\x1b[32m',
|
|
85
88
|
},
|
|
86
89
|
});
|
|
87
90
|
|
|
88
|
-
p.addRow({ id: 1,
|
|
89
|
-
p.addRow(
|
|
90
|
-
|
|
91
|
-
{ color: 'custom_green' } // your green
|
|
92
|
-
);
|
|
93
|
-
p.addRow(
|
|
94
|
-
{ id: 3, text: 'gelb bananen', value: 100, is_priority_today: 'Y' },
|
|
95
|
-
{ color: 'yellow' }
|
|
96
|
-
);
|
|
97
|
-
p.addRow({ id: 3, text: 'rosa hemd wie immer', value: 100 }, { color: 'cyan' });
|
|
91
|
+
p.addRow({ id: 1, project: 'Website Redesign', status: 'On Track' }, { color: 'on_track' });
|
|
92
|
+
p.addRow({ id: 2, project: 'Mobile App', status: 'Behind Schedule' }, { color: 'urgent' });
|
|
93
|
+
p.addRow({ id: 3, project: 'API Integration', status: 'Completed' }, { color: 'green' });
|
|
98
94
|
|
|
99
95
|
p.printTable();
|
|
100
96
|
```
|
|
101
97
|
|
|
102
|
-

|
|
103
99
|
|
|
104
100
|
## CLI
|
|
105
101
|
|
|
@@ -121,19 +117,23 @@ Official documentation has been moved here: [console-table-documentation](https:
|
|
|
121
117
|
|
|
122
118
|
```javascript
|
|
123
119
|
new Table({
|
|
124
|
-
title: '
|
|
120
|
+
title: '📊 Sales Report Q4 2024', // A text showsup on top of table (optional)
|
|
125
121
|
columns: [
|
|
126
|
-
{ name: '
|
|
127
|
-
{ name: '
|
|
128
|
-
{ name: '
|
|
122
|
+
{ name: 'region', alignment: 'left', color: 'blue' }, // with alignment and color
|
|
123
|
+
{ name: 'sales', alignment: 'right', maxLen: 30 }, // lines bigger than this will be splitted in multiple lines
|
|
124
|
+
{ name: 'growth', title: 'Growth %' }, // Title is what will be shown while printing, by default title = name
|
|
125
|
+
],
|
|
126
|
+
rows: [
|
|
127
|
+
{ region: 'North America', sales: '$2.5M', growth: '+15%' },
|
|
128
|
+
{ region: 'Europe', sales: '$1.8M', growth: '+8%' },
|
|
129
|
+
{ region: 'Asia Pacific', sales: '$3.2M', growth: '+22%' },
|
|
129
130
|
],
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
disabledColumns: ['column2'], // array of columns that you DONT want to see, these will always be hidden
|
|
131
|
+
sort: (row1, row2) => row2.sales - row1.sales, // sorting order of rows (optional), this is normal js sort function for Array.sort
|
|
132
|
+
filter: (row) => row.growth > '+10%', // filtering rows (optional)
|
|
133
|
+
enabledColumns: ['region', 'sales'], // array of columns that you want to see, all other will be ignored (optional)
|
|
134
|
+
disabledColumns: ['growth'], // array of columns that you DONT want to see, these will always be hidden
|
|
135
135
|
colorMap: {
|
|
136
|
-
|
|
136
|
+
high_growth: '\x1b[32m', // define customized color
|
|
137
137
|
},
|
|
138
138
|
charLength: {
|
|
139
139
|
'👋': 2,
|
|
@@ -2,39 +2,93 @@ import { ColorMap } from '../utils/colored-console-line';
|
|
|
2
2
|
import { ALIGNMENT, CharLengthDict, COLOR, Dictionary } from './common';
|
|
3
3
|
import { TableStyleDetails } from './internal-table';
|
|
4
4
|
export { ALIGNMENT, COLOR };
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for a table column
|
|
7
|
+
*/
|
|
5
8
|
export interface ColumnOptionsRaw {
|
|
9
|
+
/** Unique identifier for the column */
|
|
6
10
|
name: string;
|
|
11
|
+
/** Display name for the column header. If not provided, uses the name property */
|
|
7
12
|
title?: string;
|
|
13
|
+
/** Text alignment within the column: 'left', 'center', or 'right' */
|
|
8
14
|
alignment?: ALIGNMENT;
|
|
15
|
+
/** Text color for the column content */
|
|
9
16
|
color?: COLOR;
|
|
17
|
+
/** Maximum length of text in the column. Longer text will be wrapped to multiple lines */
|
|
10
18
|
maxLen?: number;
|
|
19
|
+
/** Minimum length of text in the column. Shorter text will be padded with spaces */
|
|
11
20
|
minLen?: number;
|
|
12
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Configuration for a computed column that generates values dynamically
|
|
24
|
+
*/
|
|
13
25
|
export interface ComputedColumn extends ColumnOptionsRaw {
|
|
26
|
+
/** Function that computes the column value for each row
|
|
27
|
+
* @param arg0 - The current row data object
|
|
28
|
+
* @param index - The index of the current row in the data array
|
|
29
|
+
* @param array - The complete array of row data
|
|
30
|
+
* @returns The computed value for this column
|
|
31
|
+
*/
|
|
14
32
|
function: (arg0: any, index: number, array: any[]) => any;
|
|
15
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Function type for sorting table rows
|
|
36
|
+
* @param row1 - First row to compare
|
|
37
|
+
* @param row2 - Second row to compare
|
|
38
|
+
* @returns Negative number if row1 should come before row2, positive if row2 should come before row1, 0 if equal
|
|
39
|
+
*/
|
|
16
40
|
export type RowSortFunction = (row1: any, row2: any) => number;
|
|
17
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Function type for filtering table rows
|
|
43
|
+
* @param row - The row data to evaluate
|
|
44
|
+
* @returns True if the row should be included, false if it should be filtered out
|
|
45
|
+
*/
|
|
46
|
+
export type RowFilterFunction = (row: any) => boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Default styling options applied to all columns unless overridden
|
|
49
|
+
*/
|
|
18
50
|
export interface DefaultColumnOptions {
|
|
51
|
+
/** Default text alignment for all columns */
|
|
19
52
|
alignment?: ALIGNMENT;
|
|
53
|
+
/** Default text color for all columns */
|
|
20
54
|
color?: COLOR;
|
|
55
|
+
/** Default title prefix for all columns */
|
|
21
56
|
title?: string;
|
|
57
|
+
/** Default maximum length for all columns */
|
|
22
58
|
maxLen?: number;
|
|
59
|
+
/** Default minimum length for all columns */
|
|
23
60
|
minLen?: number;
|
|
24
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Complete configuration options for table creation and styling
|
|
64
|
+
*/
|
|
25
65
|
export interface ComplexOptions {
|
|
66
|
+
/** Table styling configuration including borders and colors */
|
|
26
67
|
style?: TableStyleDetails;
|
|
68
|
+
/** Title displayed at the top of the table */
|
|
27
69
|
title?: string;
|
|
70
|
+
/** Array of column configurations */
|
|
28
71
|
columns?: ColumnOptionsRaw[];
|
|
72
|
+
/** Initial data rows for the table */
|
|
29
73
|
rows?: Dictionary[];
|
|
74
|
+
/** Function to sort rows before display */
|
|
30
75
|
sort?: RowSortFunction;
|
|
76
|
+
/** Function to filter rows before display */
|
|
31
77
|
filter?: RowFilterFunction;
|
|
78
|
+
/** Array of column names to include (all others will be hidden) */
|
|
32
79
|
enabledColumns?: string[];
|
|
80
|
+
/** Array of column names to exclude (these will always be hidden) */
|
|
33
81
|
disabledColumns?: string[];
|
|
82
|
+
/** Array of computed columns that generate values dynamically */
|
|
34
83
|
computedColumns?: ComputedColumn[];
|
|
84
|
+
/** Whether to add separator lines between rows */
|
|
35
85
|
rowSeparator?: boolean;
|
|
86
|
+
/** Whether to disable color output (useful for non-color terminals) */
|
|
36
87
|
shouldDisableColors?: boolean;
|
|
88
|
+
/** Custom color mapping for special characters or values */
|
|
37
89
|
colorMap?: ColorMap;
|
|
90
|
+
/** Custom character length mapping for special characters (e.g., emojis) */
|
|
38
91
|
charLength?: CharLengthDict;
|
|
92
|
+
/** Default options applied to all columns unless overridden */
|
|
39
93
|
defaultColumnOptions?: DefaultColumnOptions;
|
|
40
94
|
}
|
|
@@ -12,7 +12,7 @@ const findWidthInConsole = (str, charLength) => {
|
|
|
12
12
|
if (charLength) {
|
|
13
13
|
Object.entries(charLength).forEach(([key, value]) => {
|
|
14
14
|
// count appearance of the key in the string and remove from original string
|
|
15
|
-
|
|
15
|
+
const regex = new RegExp(key, 'g');
|
|
16
16
|
strLen += (str.match(regex) || []).length * value;
|
|
17
17
|
str = str.replace(key, '');
|
|
18
18
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "console-table-printer",
|
|
3
|
-
"version": "2.14.
|
|
3
|
+
"version": "2.14.4",
|
|
4
4
|
"repository": "github:console-table-printer/console-table-printer",
|
|
5
5
|
"description": "Printing pretty tables on console log",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"setup": "yarn",
|
|
10
10
|
"build": "tsc",
|
|
11
|
-
"format": "prettier --write \"**/*.{json,
|
|
11
|
+
"format": "prettier --write \"**/*.{json,ts,tsx,yml,js,jsx}\"",
|
|
12
12
|
"test": "jest --config jestconfig.json",
|
|
13
13
|
"test:coverage": "jest --config jestconfig.json --coverage",
|
|
14
|
-
"lint": "eslint
|
|
14
|
+
"lint": "eslint .",
|
|
15
15
|
"semantic-release": "semantic-release"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
|
@@ -26,23 +26,22 @@
|
|
|
26
26
|
"author": "Nahiyan Kamal",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@eslint/js": "^9.28.0",
|
|
29
30
|
"@semantic-release/changelog": "^6.0.3",
|
|
30
31
|
"@semantic-release/git": "^10.0.1",
|
|
31
32
|
"@types/jest": "^29.5.14",
|
|
32
|
-
"@
|
|
33
|
-
"
|
|
34
|
-
"eslint": "^9.27.0",
|
|
35
|
-
"eslint-config-airbnb-base": "^15.0.0",
|
|
33
|
+
"@types/node": "^24.0.3",
|
|
34
|
+
"eslint": "^9.28.0",
|
|
36
35
|
"eslint-config-prettier": "^10.1.5",
|
|
37
|
-
"eslint-plugin-
|
|
38
|
-
"eslint-plugin-prettier": "^5.4.0",
|
|
36
|
+
"eslint-plugin-prettier": "^5.4.1",
|
|
39
37
|
"husky": "^9.1.7",
|
|
40
38
|
"jest": "^29.7.0",
|
|
41
39
|
"prettier": "^3.5.3",
|
|
42
|
-
"pretty-quick": "^4.
|
|
40
|
+
"pretty-quick": "^4.2.2",
|
|
43
41
|
"semantic-release": "^24.2.5",
|
|
44
42
|
"ts-jest": "^29.3.4",
|
|
45
|
-
"typescript": "^5.8.3"
|
|
43
|
+
"typescript": "^5.8.3",
|
|
44
|
+
"typescript-eslint": "^8.33.1"
|
|
46
45
|
},
|
|
47
46
|
"homepage": "https://console-table.netlify.app",
|
|
48
47
|
"dependencies": {
|