Fast and customizable table module to render unicode table's on the command line
This module can be imported directly from the repo and from following registries.
Deno Registry
import { Table } from "https://deno.land/x/cliffy@<version>/table/mod.ts";
Nest Registry
import { Table } from "https://x.nest.land/cliffy@<version>/table/mod.ts";
Github
import { Table } from "https://raw.githubusercontent.com/c4spar/deno-cliffy/<version>/table/mod.ts";
To create a table you can simple create an instance of the Table
class and
pass the rows as arguments to the constructor. The example below will output a
simple table with three rows and without any styles. The only default option is
padding
which is set to 1
.
const table: Table = new Table(
["Baxter Herman", "Oct 1, 2020", "Harderwijk", "Slovenia"],
["Jescie Wolfe", "Dec 4, 2020", "Alto Hospicio", "Japan"],
["Allegra Cleveland", "Apr 16, 2020", "Avernas-le-Bauduin", "Samoa"],
["Aretha Gamble", "Feb 22, 2021", "Honolulu", "Georgia"],
);
console.log(table.toString());
$ deno run https://deno.land/x/cliffy/examples/table/basic_usage.ts
Since the Table
class is an Array
, you can call all the methods of the array
class like .from()
, .sort()
, .push()
, .unshift()
and friends.
const table: Table = Table.from([
["Baxter Herman", "Oct 1, 2020", "Harderwijk", "Slovenia"],
["Jescie Wolfe", "Dec 4, 2020", "Alto Hospicio", "Japan"],
["Allegra Cleveland", "Apr 16, 2020", "Avernas-le-Bauduin", "Samoa"],
]);
table.push(["Aretha Gamble", "Feb 22, 2021", "Honolulu", "Georgia"]);
table.sort();
table.render();
$ deno run https://deno.land/x/cliffy/examples/table/using_as_array.ts
To define a table header you can use the .header()
method. The header is not
affected by any Array
method like .sort()
because it is stored as a separate
property and not in the array stack. The .body()
method adds an array of rows
to the table and removes all existing rows.
new Table()
.header(["Name", "Date", "City", "Country"])
.body([
["Baxter Herman", "Oct 1, 2020", "Harderwijk", "Slovenia"],
["Jescie Wolfe", "Dec 4, 2020", "Alto Hospicio", "Japan"],
["Allegra Cleveland", "Apr 16, 2020", "Avernas-le-Bauduin", "Samoa"],
["Aretha Gamble", "Feb 22, 2021", "Honolulu", "Georgia"],
])
.render();
$ deno run https://deno.land/x/cliffy/examples/table/header_and_body.ts
To customize the table, the table class provides a few chainable option methods. To see a list of all available options go to the Talbe API section.
new Table()
.header(["Name", "Date", "City", "Country"])
.body([
["Baxter Herman", "Oct 1, 2020", "Harderwijk", "Slovenia"],
["Jescie Wolfe", "Dec 4, 2020", "Alto Hospicio", "Japan"],
["Allegra Cleveland", "Apr 16, 2020", "Avernas-le-Bauduin", "Samoa"],
["Aretha Gamble", "Feb 22, 2021", "Honolulu", "Georgia"],
])
.maxColWidth(10)
.padding(1)
.indent(2)
.border(true)
.render();
$ deno run https://deno.land/x/cliffy/examples/table/table_options.ts
It is also possible to customize single rows and cell. To do this you can use
the Row
and Cell
class. The Row
class is also an Array
class like the
Table
class. To see a list of all available options go to the Row or
Cell API section.
import {
Cell,
Row,
Table,
} from "https://deno.land/x/cliffy@<version>/table/mod.ts";
new Table()
.header(Row.from(["Name", "Date", "City", "Country"]).border(true))
.body([
[
"Baxter Herman",
Cell.from("Oct 1, 2020").border(true),
"Row 1 Column 3",
"Harderwijk",
"Slovenia",
],
new Row("Jescie Wolfe", "Dec 4, 2020", "Alto Hospicio", "Japan").border(
true,
),
["Allegra Cleveland", "Apr 16, 2020", "Avernas-le-Bauduin", "Samoa"],
["Aretha Gamble", "Feb 22, 2021", "Honolulu", "Georgia"],
])
.render();
$ deno run https://deno.land/x/cliffy/examples/table/rows_and_cells.ts
Colspan and rowspan allows a single table cell to span the width/height of more than one column and/or row.
Table.from([
[
Cell.from("Row 1 & 2 Column 1").rowSpan(2),
"Row 1 Column 2",
"Row 1 Column 3",
],
[Cell.from("Row 2 Column 2 & 3").colSpan(2)],
[
Cell.from("Row 3 & 4 Column 1").rowSpan(2),
"Row 3 Column 2",
"Row 3 Column 3",
],
[Cell.from("Row 4 Column 2 & 3").colSpan(2)],
["Row 5 Column 1", Cell.from("Row 5 & 6 Column 2 & 3").rowSpan(2).colSpan(2)],
["Row 6 Column 1"],
])
.border(true)
.render();
$ deno run https://deno.land/x/cliffy/examples/table/colspan_and_rowspan.ts
- Table
- .header(row)
- .body(rows)
- .clone()
- .toString()
- .render()
- .minColWidth(width,override)
- .maxColWidth(width,override)
- .indent(width,override)
- .padding(padding,override)
- .border(enable,override)
- .chars(chars)
- .getHeader()
- .getBody()
- .getMinColWidth()
- .getMaxColWidth()
- .getIndent()
- .getPadding()
- .getBorder()
- Row
- Cell
Sets the table header row.
Argument | Type | Required | Description |
---|---|---|---|
row | IRow |
Yes | Can be an Array of string 's and Cell 's |
Return type: this
Adds an array of rows to the table and removes all existing rows.
Argument | Type | Required | Description |
---|---|---|---|
rows | Array<IRow> |
Yes | Array of row's. A row can be an Array of string 's and Cell 's |
Return type: this
Clones the table.
Return type: this
Generates the table string.
Return type: string
Outputs the result of the .toString()
method with Deno.stdout.writeSnyc()
.
Return type: this
Set min column with.
Argument | Type | Required | Description |
---|---|---|---|
width | `number | Array` | Yes |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Set max column with.
Argument | Type | Required | Description |
---|---|---|---|
width | `number | Array` | Yes |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Indent the table output.
Argument | Type | Required | Description |
---|---|---|---|
width | number |
Yes | Indent width. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Set column padding. If border is enabled the padding will be applyed on the left and the right side of each cell.
Argument | Type | Required | Description |
---|---|---|---|
padding | `number | number[]` | Yes |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Enable table border. Doesn't override row and cell settings.
Argument | Type | Required | Description |
---|---|---|---|
enable | boolean |
Yes | Enable or disable table border. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Override default border characters. Doesn't override row and cell settings. To
change the default border characters globally you can use the static
Table.chars(chars)
method.
Argument | Type | Required | Description |
---|---|---|---|
chars | IBorderOptions |
Yes | An object with border characters. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Here is an example of the default border characters:
{
top: "─",
topMid: "┬",
topLeft: "┌",
topRight: "┐",
bottom: "─",
bottomMid: "┴",
bottomLeft: "└",
bottomRight: "┘",
left: "│",
leftMid: "├",
mid: "─",
midMid: "┼",
right: "│",
rightMid: "┤",
middle: "│",
};
Return type: this
Returns the header row.
Return type: Row | undefined
Returns all body rows.
Return type: IRow[]
Get min columns width.
Return type: number | number[]
Get max columns width.
Return type: number | number[]
Get indent width.
Return type: number
Get padding.
Return type: number | number[]
Check if border is enabled on the table.
Return type: boolean
Clones the row.
Return type: this
Enable row border. Override table settings but not cell settings.
Argument | Type | Required | Description |
---|---|---|---|
enable | boolean |
Yes | Enable or disable table border. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Check if border is enabled on the row.
Return type: boolean
Clones the cell.
Return type: this
Enable cell border. Overrides table and row settings.
Argument | Type | Required | Description |
---|---|---|---|
enable | boolean |
Yes | Enable or disable table border. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Allows a single table cell to span the width of more than one cell or column.
Can be combined with .rowSpan()
.
Argument | Type | Required | Description |
---|---|---|---|
span | number |
Yes | Number of columns to span the cell. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Allows a single table cell to span the height of more than one cell or row. Can
be combined with .colSpan()
.
Argument | Type | Required | Description |
---|---|---|---|
span | number |
Yes | Number of rows to span the cell. |
override | boolean |
No | Set override to false to prevent overriding existing values. |
Return type: this
Check if border is enabled on the cell.
Return type: boolean
Get cell column span.
Return type: number
Get cell row span.
Return type: number
Any kind of contribution is welcome! Please take a look at the contributing guidelines.