Ascii Table
Easy table output for node debugging, but you could probably do more with it, since its just a string.
Table of Contents
- Usage
- Example
- API
- Static Methods
- Instance Methods
- setBorder([edge], [fill], [top], [bottom])
- removeBorder()
- setAlign(idx, direction)
- setAlignLeft(idx)
- setAlignCenter(idx)
- setAlignRight(idx)
- setTitle(title)
- getTitle()
- setTitleAlign(direction)
- setTitleAlignLeft()
- setTitleAlignCenter()
- setTitleAlignRight()
- sort([iterator])
- sortColumn(idx, [iterator])
- setHeading(heading, [...])
- setHeadingAlign(direction)
- setHeadingAlignLeft()
- setHeadingAlignCenter()
- setHeadingAlignRight()
- addRow(row, [...])
- addRowMatrix(rows)
- setJustify([enabled])
- toString()
- toJSON()
- fromJSON(obj)
- clear()
- clearRows()
- Install
- Contributors
- License
Usage
Node.js
var AsciiTable =
Browser
Note: If using in the browser, it will be placed under window.AsciiTable
Example
Basic usage
var table = 'A Title'table console
.----------------.
| A Title |
|----------------|
| | Name | Age |
|---|------|-----|
| 1 | Bob | 52 |
| 2 | John | 34 |
| 3 | Jim | 83 |
'----------------'
We can make a simple table without a title or headings as well.
var table = table console
.------------------------------------.
| a | apple | Some longer string |
| b | banana | hi |
| c | carrot | meow |
| e | elephants | |
'------------------------------------'
API
Static Methods
AsciiTable
See: AsciiTable.factory
for details on instantiation
AsciiTable.factory([title], [options])
Table instance creator
title
- table title (optional, defaultnull
)options
- table options (optional)prefix
- string prefix to add to each line on render
Note: If an object is passed in place of the title
, the fromJSON
method will be used to populate the table.
Example:
var table = AsciiTable var table = AsciiTable
AsciiTable.align(direction, val, len, [pad])
Shortcut to one of the three following methods
direction
- alignment direction (AsciiTable.LEFT
,AsciiTable.CENTER
,AsciiTable.RIGHT
)val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default' '
)
Example:
tablealignAsciiTableLEFT 'hey' 7 // 'hey '
AsciiTable.alignLeft(val, len, [pad])
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default' '
)
Example:
table // 'hey----'
AsciiTable.alignCenter(val, len, [pad])
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default' '
)
Example:
table // ' hey '
AsciiTable.alignRight(val, len, [pad])
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default' '
)
Example:
table // ' hey'
AsciiTable.alignAuto(val, len, [pad])
Attempt to do intelligent alignment of provided val
, String
input will
be left aligned, Number
types will be right aligned.
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default' '
)
Example:
tablealignAsciiTableLEFT 'hey' 7 // 'hey '
AsciiTable.arrayFill(len, [val])
Create a new array at the given len, filled with the given value, mainly used internally
len
- length of arrayval
- fill value (optional)
Example:
AsciiTable // [0, 0, 0, 0]
Instance Methods
instance.setBorder([edge], [fill], [top], [bottom])
Set the border characters for rendering, if no arguments are passed it will be
reset to defaults. If a single edge
arg is passed, it will be used for all borders.
edge
- horizontal edges (optional, default|
)fill
- vertical edges (optional, default-
)top
- top corners (optional, default.
)bottom
- bottom corners (optional, default'
)
Example:
var table = 'Stars'table console
************************
* Stars *
************************
* oh * look *
************************
* so much * star power *
************************
instance.removeBorder()
Example:
table console
# Fruit Thing
--- ----------- --------------------
a apple Some longer string
b banana hi
c carrot meow
e elephants
instance.setAlign(idx, direction)
idx
- column index to aligndirection
- alignment direction, (AsciiTable.LEFT
,AsciiTable.CENTER
,AsciiTable.RIGHT
)
Example:
table console
.-------------------------------------.
| a | apple | Some longer string |
| b | banana | hi |
| c | carrot | meow |
| e | elephants | |
'-------------------------------------'
instance.setAlignLeft(idx)
Alias to instance.setAlign(idx, AsciiTable.LEFT)
instance.setAlignCenter(idx)
Alias to instance.setAlign(idx, AsciiTable.CENTER)
instance.setAlignRight(idx)
Alias to instance.setAlign(idx, AsciiTable.RIGHT)
instance.setTitle(title)
title
- table title
Example:
var table = 'Old Title' table
instance.getTitle()
Get the current title of the table
Example:
table // 'New Title'
instance.setTitleAlign(direction)
direction
- table alignment direction
Example:
instance.setTitleAlignLeft()
Alias to instance.setTitleAlign(AsciiTable.LEFT)
instance.setTitleAlignCenter()
Alias to instance.setTitleAlign(AsciiTable.CENTER)
instance.setTitleAlignRight()
Alias to instance.setTitleAlign(AsciiTable.RIGHT)
instance.sort(iterator)
iterator
- sorting method to run against the rows
Example:
tableconsole
.----------------.
| 2 | John | 34 |
| 1 | Bob | 52 |
| 3 | Jim | 83 |
'----------------'
instance.sortColumn(index, iterator)
Sorting shortcut for targeting a specific column
index
- column idx to sortiterator
- sorting method to run against column values
Example:
// This is quivalent to the `sort` example abovetable
instance.setHeading(heading, [...])
Set the column headings for the table, takes arguments the same way as addRow
heading
- heading array or arguments
Example:
table // or: table
instance.setHeadingAlign(direction)
direction
-
Example:
instance.setHeadingAlignLeft()
Alias to instance.setHeadingAlignLeft(AsciiTable.LEFT)
instance.setHeadingAlignCenter()
Alias to instance.setHeadingAlignLeft(AsciiTable.CENTER)
instance.setHeadingAlignRight()
Alias to instance.setHeadingAlignLeft(AsciiTable.RIGHT)
instance.addRow(row, [...])
Rows can be added using a single array argument, or the arguments if multiple args are used when calling the method.
row
- array or arguments of column values
Example:
var table = table console
.---------------.
| 1 | Bob | 52 |
| 2 | John | 34 |
'---------------'
instance.addRowMatrix(rows)
Bulk addRow
operation
rows
- multidimentional array of rows
Example:
table
instance.setJustify(enabled)
Justify all columns to be the same width
enabled
- boolean for turning justify on or off,undefined
considered true
Example:
table console
.-----------------------.
| 1 | two | three |
'-----------------------'
instance.toString()
Render the instance as a string for output
Alias: [valueOf
, render
]
instance.toJSON()
Return the JSON representation of the table, this also allows us to call
JSON.stringify
on the instance.
Example:
var table = 'Title' table consoleconsole
title: 'Title' heading: 'id' 'name' rows: 1 'Bob' 2 'Steve'
{"title":"Title","heading":["id","name"],"rows":[[1,"Bob"],[2,"Steve"]]}
instance.fromJSON(obj)
Populate the table from json object, should match the toJSON
output above.
Alias: [parse
]
Example:
var table =
instance.clear()
Clear / reset all table data
Alias: [reset
]
instance.clearRows()
Reset all row data, maintains title and headings.
Install
With npm
npm install ascii-table
Contributors
License
(The MIT License)
Copyright (c) 2013 Beau Sorensen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.