console-grid

Features
- Console log a grid
- Support tree style rows
- Custom cell formatter
- Column align/sorting
- Multiple lines header
- Support colorful cells
Install
npm i console-grid
Usage
const CG = require("console-grid");
CG({
"columns": ["", "Name", "Value"],
"rows": [
[1, "Tom", "Value 1"],
[2, "Jerry", "Value 2"]
]
});
βββββ¬ββββββββ¬ββββββββββ
β β Name β Value β
βββββΌββββββββΌββββββββββ€
β 1 β Tom β Value 1 β
β 2 β Jerry β Value 2 β
βββββ΄ββββββββ΄ββββββββββ
const CG = require("console-grid");
CG({
"options": {
"headerVisible": false
},
"columns": ["", "Name", "Value"],
"rows": [
[1, "Tom", "Value 1"],
[2, "Jerry", "Value 2"]
]
});
βββββ¬ββββββββ¬ββββββββββ
β 1 β Tom β Value 1 β
β 2 β Jerry β Value 2 β
βββββ΄ββββββββ΄ββββββββββ
With column minWidth and maxWidth (Multiple Line Header):
const CG = require("console-grid");
CG({
"columns": ["", {
"name": "Name",
"minWidth": 15
}, {
"name": "Value",
"maxWidth": 20
}, {
"name": "Multiple Line Header",
"maxWidth": 15
}],
"rows": [
[1, "Hello", "Long Text Value", "Long Text Value"],
[2, "Hello There", "Long Text Value Long Text Value", "Long Text Value Long Text Value"]
]
});
βββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββ¬ββββββββββββββββββ
β β β β Multiple Line β
β β Name β Value β Header β
βββββΌββββββββββββββββββΌβββββββββββββββββββββββΌββββββββββββββββββ€
β 1 β Hello β Long Text Value β Long Text Value β
β 2 β Hello There β Long Text Value L... β Long Text Va... β
βββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββ΄ββββββββββββββββββ
With column align and padding:
const CG = require("console-grid");
CG({
"options": {
"padding": 2
},
"columns": [{
"id": "default",
"name": "Default"
}, {
"id": "left",
"name": "Left",
"align": "left"
}, {
"id": "center",
"name": "Center",
"align": "center"
}, {
"id": "right",
"name": "Right",
"align": "right"
}, {
"id": "right",
"name": "Multiple Line Right",
"maxWidth": 12,
"align": "right"
}],
"rows": [{
"default": "Cell",
"left": "Markdown",
"center": "Start",
"right": "123.0"
}, {
"default": "Content",
"left": "Grid",
"center": "Complete",
"right": "8.1"
}]
});
βββββββββββββ¬βββββββββββββ¬βββββββββββββ¬ββββββββββ¬βββββββββββββββββ
β β β β β Multiple β
β Default β Left β Center β Right β Line Right β
βββββββββββββΌβββββββββββββΌβββββββββββββΌββββββββββΌβββββββββββββββββ€
β Cell β Markdown β Start β 123.0 β 123.0 β
β Content β Grid β Complete β 8.1 β 8.1 β
βββββββββββββ΄βββββββββββββ΄βββββββββββββ΄ββββββββββ΄βββββββββββββββββ
With tree rows (nullPlaceholder/number align and formatter):
const CG = require("console-grid");
CG({
"columns": [{
"id": "name",
"name": "Name",
"type": "string",
"maxWidth": 30
}, {
"id": "value",
"name": "Value",
"type": "string",
"maxWidth": 7
}, {
"id": "null",
"name": "Null"
}, {
"id": "number",
"type": "number",
"name": "Number",
"maxWidth": 12
}],
"rows": [{
"name": "Row 1",
"value": "1",
"number": 1
}, {
"name": "Row Name",
"value": "2",
"number": 2
}, {
"name": "Row Long Name Long Name Long Name",
"value": "3",
"number": 3
}, {
"name": "Group",
"value": "4",
"number": 4,
"subs": [{
"name": "Sub Group 1",
"value": "5",
"number": 5,
"subs": [{
"name": "Sub Group 1 Sub Row 1",
"value": "6",
"number": 6
}]
}, {
"name": "Sub Row 1",
"value": "7",
"number": 7
}, {
"name": "Sub Row 2",
"value": "8",
"number": 8
}]
}]
});
ββββββββββββββββββββββββββββββββββ¬ββββββββ¬βββββββ¬βββββββββ
β Name β Value β Null β Number β
ββββββββββββββββββββββββββββββββββΌββββββββΌβββββββΌβββββββββ€
β β Row 1 β 1 β - β 1.00 β
β β Row Name β 2 β - β 2.00 β
β β Row Long Name Long Name L... β 3 β - β 3.00 β
β β Group β 4 β - β 4.00 β
β β Sub Group 1 β 5 β - β 5.00 β
β β β Sub Group 1 Sub Row 1 β 6 β - β 6.00 β
β β Sub Row 1 β 7 β - β 7.00 β
β β Sub Row 2 β 8 β - β 8.00 β
ββββββββββββββββββββββββββββββββββ΄ββββββββ΄βββββββ΄βββββββββ
With inner border:
const CG = require("console-grid");
CG({
"columns": [{
"id": "name",
"name": "Name"
}, {
"id": "value",
"name": "Value"
}],
"rows": [{
"name": "Total",
"value": 80
}, {
"innerBorder": true
}, {
"name": "Item 1",
"value": 30
}, {
"name": "Item 2",
"value": 50
}]
});
ββββββββββ¬ββββββββ
β Name β Value β
ββββββββββΌββββββββ€
β Total β 80 β
ββββββββββΌββββββββ€
β Item 1 β 30 β
β Item 2 β 50 β
ββββββββββ΄ββββββββ
With column sorting:
const CG = require("console-grid");
CG({
"options": {
"sortField": "value",
"sortAsc": false
},
"columns": [{
"id": "name",
"name": "Name"
}, {
"id": "value",
"name": "Value",
"type": "number"
}],
"rows": [{
"name": "Item 1",
"value": 80
}, {
"name": "Item 2",
"value": 30
}, {
"name": "Item 3",
"value": 50
}]
});
ββββββββββ¬ββββββββ
β Name β Value β
ββββββββββΌββββββββ€
β Item 1 β 80 β
β Item 3 β 50 β
β Item 2 β 30 β
ββββββββββ΄ββββββββ
With special character:
const CG = require("console-grid");
CG({
"columns": ["Special", "Character"],
"rows": [
["Chinese,δΈζ", "12γζ οΌηΉγγ"],
["γγγ’γ€γ΅γ¦γ€γ", "βββΓββ€β¬"],
["γγ
γ‘γ
γ
γ
γ
γ
", "β β΅ββ
£βΊΚΙts"],
["ζ±εηΉι«", "ΠΠΠΠΡΡΡΡ"]
]
});
ββββββββββββββββββββ¬βββββββββββββββββββ
β Special β Character β
ββββββββββββββββββββΌβββββββββββββββββββ€
β Chinese,δΈζ β 12γζ οΌηΉγγ β
β γγγ’γ€γ΅γ¦γ€γ β βββΓββ€β¬ β
β γγ
γ‘γ
γ
γ
γ
γ
β β β΅ββ
£βΊΚΙts β
β ζ±εηΉι« β ΠΠΠΠΡΡΡΡ β
ββββββββββββββββββββ΄βββββββββββββββββββ
- Unresolved: some special characters has unexpected width, especially on different output terminals (depends on fonts)
With colorful cells (using eight-colors):
const EC = require("eight-colors");
const CG = require("console-grid");
const data = {
columns: ['Name', EC.cyan('Color Text'), EC.bg.cyan('Color Background')],
rows: [
['Red', EC.red('red text'), EC.bg.red('red bg')],
['Green', EC.green('green text'), EC.bg.green('green text')]
]
};
CG(data);
// silent output and remove color
data.options = {
silent: true
};
const lines = CG(data);
console.log(EC.remove(lines.join('\n')));

Data Format Definition: CGDF
{
options: Object,
columns: Array,
rows: Array
}
Default Options
{
silent: false,
headerVisible: false,
padding: 1,
defaultMinWidth: 1,
defaultMaxWidth: 50,
sortField: '',
sortAsc: false,
treeId: 'name',
treeIcon: 'β ',
treeLink: 'β ',
treeLast: 'β ',
treeIndent: ' ',
nullPlaceholder: '-',
borderH: 'β',
borderV: 'β',
borderTL: 'β',
borderTC: 'β¬',
borderTR: 'β',
borderCL: 'β',
borderCC: 'βΌ',
borderCR: 'β€',
borderBL: 'β',
borderBC: 'β΄',
borderBR: 'β'
}
Column Properties
{
id: String,
name: String,
type: String,
align : String,
minWidth: Number,
maxWidth: Number,
formatter: Function
}
Row Properties
{
innerBorder: Boolean,
subs: Array
}
CHANGELOG
CHANGELOG.md
Markdown Grid
markdown-grid - Markdown Grid Generator