|
| 1 | +# table-console |
| 2 | + |
| 3 | +JavaScript library for displaying table in the console |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Customizable border |
| 8 | + - Four built-in styles |
| 9 | + - Option to create custom style |
| 10 | +- Text and background color styling and text decorations |
| 11 | +- Text alignment (left, center, right) |
| 12 | +- Padding (left, right) |
| 13 | +- Horizontal lines customization (disabled, only header, enabled) |
| 14 | +- Zero dependency |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install table-console |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +```js |
| 25 | +var Table = require("table-console"); |
| 26 | +var t = new Table({ |
| 27 | + padding: 2, |
| 28 | + headerLine: false, |
| 29 | +}); |
| 30 | +t.insertRows([ |
| 31 | + ['First value', 'Second value'], |
| 32 | + ['First value', 'Second value'] |
| 33 | +]); |
| 34 | + |
| 35 | +console.log(t.toString()); |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +### Constructor options |
| 41 | + |
| 42 | +- style - `string` or `number`, default value `"unicode"` \ |
| 43 | + Build-in style of border |
| 44 | + |
| 45 | + - `"unicode"` or `1` (default) |
| 46 | + |
| 47 | + ```text |
| 48 | + ┌─────────────┬──────────────┐ |
| 49 | + │ First value │ Second value │ |
| 50 | + ├─────────────┼──────────────┤ |
| 51 | + │ Third value │ Fourth value │ |
| 52 | + └─────────────┴──────────────┘ |
| 53 | + ``` |
| 54 | + |
| 55 | + - `"unicode bold"` or `2` |
| 56 | + |
| 57 | + ```text |
| 58 | + ┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓ |
| 59 | + ┃ First value ┃ Second value ┃ |
| 60 | + ┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫ |
| 61 | + ┃ Third value ┃ Fourth value ┃ |
| 62 | + ┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛ |
| 63 | + ``` |
| 64 | +
|
| 65 | + - `"unicode double"` or `3` |
| 66 | +
|
| 67 | + ```text |
| 68 | + ╔═════════════╦══════════════╗ |
| 69 | + ║ First value ║ Second value ║ |
| 70 | + ╠═════════════╬══════════════╣ |
| 71 | + ║ Third value ║ Fourth value ║ |
| 72 | + ╚═════════════╩══════════════╝ |
| 73 | + ``` |
| 74 | +
|
| 75 | + - `"ascii"` or `4` |
| 76 | +
|
| 77 | + ```text |
| 78 | + +-------------+--------------+ |
| 79 | + | First value | Second value | |
| 80 | + |-------------+--------------| |
| 81 | + | Third value | Fourth value | |
| 82 | + +-------------+--------------+ |
| 83 | + ``` |
| 84 | +
|
| 85 | +- borders - `object` \ |
| 86 | + Custom border |
| 87 | +
|
| 88 | + E.g. |
| 89 | +
|
| 90 | + ```js |
| 91 | + borders: { |
| 92 | + topLeft: "┌", top: "─", topMid: "┬", topRight: "┐", |
| 93 | + midLeft: "├", mid: "─", midMid: "┼", midRight: "┤", |
| 94 | + botLeft: "└", bot: "─", botMid: "┴", botRight: "┘", |
| 95 | + sep: "│", |
| 96 | + } |
| 97 | + ``` |
| 98 | +
|
| 99 | +- horizontalLines - `boolean`, default value `false` \ |
| 100 | + Put horizontal lines after each row |
| 101 | +
|
| 102 | +- headerLine - `boolean`, default value `true` \ |
| 103 | + Put horizontal lines after first row |
| 104 | +
|
| 105 | +- padding, leftPadding, rightPadding - `number`, default value `1` \ |
| 106 | + Padding in cells |
| 107 | + leftPadding and rightPadding is preferred over padding if both are present. |
| 108 | +
|
| 109 | +### Methods |
| 110 | +
|
| 111 | +- **insertRow(row)** \ |
| 112 | + Inserts one row to bottom of the table |
| 113 | +
|
| 114 | + Parameters: |
| 115 | + - **row** - `Array` containing row cells |
| 116 | +
|
| 117 | +- **insertRows(rows)** \ |
| 118 | + Inserts multiple rows to bottom of the table |
| 119 | +
|
| 120 | + Parameters: |
| 121 | + - **rows** - `Array` containing rows |
| 122 | +
|
| 123 | +- **removeRow(row)** \ |
| 124 | + Removes one row from the table |
| 125 | +
|
| 126 | + Parameters: |
| 127 | + - **row** - row id |
| 128 | +
|
| 129 | +- **getRows()** \ |
| 130 | + Returns all rows in the table |
| 131 | +
|
| 132 | +- **toString()** \ |
| 133 | + Returns string representation of the table |
| 134 | +
|
| 135 | +- **insertHorizontalLine()** \ |
| 136 | + Inserts horizontal line to bottom of the table |
| 137 | +
|
| 138 | +- **getCell(row, col)** \ |
| 139 | + Return cell value |
| 140 | +
|
| 141 | + Parameters: |
| 142 | + - **row** - row id |
| 143 | + - **col** - column id |
| 144 | +
|
| 145 | +- **setCell(row, col, value)** \ |
| 146 | + Sets cell value |
| 147 | +
|
| 148 | + Parameters: |
| 149 | + - **row** - row id |
| 150 | + - **col** - column id |
| 151 | + - **value** - new value |
| 152 | +
|
| 153 | +- **setCellAttrs(row, col, attrs)** \ |
| 154 | + Sets cell attributes |
| 155 | +
|
| 156 | + Parameters: |
| 157 | + - **row** - row id |
| 158 | + - **col** - column id |
| 159 | + - **attrs** - attributes |
| 160 | + - **color** - `string` - `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`, `brightRed`, `brightGreen`, `brightYellow`, `brightBlue`, `brightMagenta`, `brightCyan`, `brightWhite` |
| 161 | + - **bgColor** - `string` - `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`, `brightRed`, `brightGreen`, `brightYellow`, `brightBlue`, `brightMagenta`, `brightCyan`, `brightWhite` |
| 162 | + - **decorations** - `Array` - `bold`, `dim`, `italic`, `underline`, `overline`, `inverse`, `strikethrough`, `slowBlink`, `rapidBlink` |
| 163 | + - **align** - `string` - `left`, `center`, `right` |
| 164 | +
|
| 165 | +- **setRowAttrs(row, attrs)** \ |
| 166 | + Sets row attributes |
| 167 | +
|
| 168 | + Parameters: |
| 169 | + - **row** - row id |
| 170 | + - **attrs** - same as `setCellAttrs` |
| 171 | +
|
| 172 | +- **setColAttrs(col, attrs)** \ |
| 173 | + Sets column attributes |
| 174 | +
|
| 175 | + Parameters: |
| 176 | + - **col** - column id |
| 177 | + - **attrs** - same as `setCellAttrs` |
0 commit comments