node-tab: Unix-style tables for command-line utilities
This module implements functions for reading and writing Unix-style command line
tables. The model mimics that of traditional Unix commands like "ps", which
emit a table with a default set of columns that can be customized using command
line options. This module implements facilities to both ingest and emit these
tables.
PID TTY TIME CMD60881 ttys000 0:00.19 -bash61674 ttys000 0:00.17 vim README.md
Using objects for rows
If you want, data (rows) can be specified with objects instead of arrays, where
the object keys keys correspond to field labels. This example outputs the same
as the previous one:
Instead of accumulating the data in memory and emitting the whole table, you can
create a TableOutputStream object with the field configuration and then emit
individual records:
var out = new mod_tab.TableOutputStream({ 'columns': [ 'PID', 'TTY', 'TIME', 'CMD' ]});out.writeRow([ '60881', 'ttys000', '0:00.19', '-bash' ]);/* or */out.writeRow({ 'PID': '61674', 'TTY': 'ttys000', 'TIME': '0:00.17', 'CMD': 'vim README.md'});
Other options
You can also omit the header row or change the column and row separators:
You can also specify a different delimiter from the default (which is any
whitespace) using a string or regular expression for the 'delim' property, as
in:
/* split on colons instead of whitespace */var input = new mod_tab.TableInputStream({ 'delim': ':' 'stream': process.stdin, 'columns': [ 'pid', 'tty', 'time', 'cmd' ]});/* split on semicolon or comma */var input = new mod_tab.TableInputStream({ 'delim': /[,;]/, 'stream': process.stdin, 'columns': [ 'pid', 'tty', 'time', 'cmd' ]});