Skip to content

Commit

Permalink
support for rows as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Pacheco committed Jul 18, 2012
1 parent 23acc2f commit c6cbf1e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
30 changes: 30 additions & 0 deletions examples/ex.objects.js
@@ -0,0 +1,30 @@
var mod_tab = require('../lib/tab');

mod_tab.emitTable({
'columns': [ {
'label': 'PID',
'align': 'right',
'width': 6
}, {
'label': 'TTY',
'width': 7
}, {
'label': 'TIME',
'align': 'right',
'width': 10
}, {
'label': 'CMD'
} ],

'rows': [ {
'PID': '60881',
'TTY': 'ttys000',
'TIME': '0:00.19',
'CMD': '-bash'
}, {
'PID': '61674',
'TTY': 'ttys000',
'TIME': '0:00.17',
'CMD': 'vim README.md'
} ]
});
13 changes: 13 additions & 0 deletions examples/ex.stream.js
@@ -0,0 +1,13 @@
var mod_tab = require('../lib/tab');

var out = new mod_tab.TableOutputStream({
'columns': [ 'PID', 'TTY', 'TIME', 'CMD' ]
});

out.writeRow([ '60881', 'ttys000', '0:00.19', '-bash' ]);
out.writeRow({
'PID': '61674',
'TTY': 'ttys000',
'TIME': '0:00.17',
'CMD': 'vim README.md'
});
19 changes: 13 additions & 6 deletions lib/tab.js
Expand Up @@ -93,12 +93,19 @@ TableOutputStream.prototype.writeRow = function (row)
this.tos_header = false;
}

n = Math.min(row.length, this.tos_cols.length);
for (i = 0; i < n; i++)
this.tos_cols[i].emit(row[i]);

for (; i < this.tos_cols.length; i++)
this.tos_cols[i].emit('');
if (Array.isArray(row)) {
n = Math.min(row.length, this.tos_cols.length);
for (i = 0; i < n; i++)
this.tos_cols[i].emit(row[i]);

for (; i < this.tos_cols.length; i++)
this.tos_cols[i].emit('');
} else {
for (i = 0; i < this.tos_cols.length; i++) {
this.tos_cols[i].emit(
row[this.tos_cols[i]['label']] || '');
}
}

this.tos_out.write(this.tos_endrecord);
};
Expand Down

0 comments on commit c6cbf1e

Please sign in to comment.