Print compact tables with no decorations #24

Merged
merged 4 commits into from Jun 22, 2013
View
@@ -74,6 +74,71 @@ table.push(
console.log(table.toString());
```
+### Custom styles
+The ```chars``` property controls how the table is drawn:
+```javascript
+var table = new Table({
+ chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
+ , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
+ , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
+ , 'right': '' , 'right-mid': '' , 'middle': '' }
+});
+
+table.push(
+ ['foo', 'bar', 'baz']
+ , ['frob', 'bar', 'quuz']
+);
+
+console.log(table.toString());
+// Outputs:
+//
+//╔══════╤═════╤══════╗
+//║ foo │ bar │ baz ║
+//╟──────┼─────┼──────╢
+//║ frob │ bar │ quuz ║
+//╚══════╧═════╧══════╝
+```
+
+Empty decoration lines will be skipped, to avoid vertical separator rows just
+set the 'mid', 'left-mid', 'mid-mid', 'right-mid' to the empty string:
+```javascript
+var table = new Table({ chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''} });
+table.push(
+ ['foo', 'bar', 'baz']
+ , ['frobnicate', 'bar', 'quuz']
+);
+
+console.log(table.toString());
+// Outputs: (note the lack of the horizontal line between rows)
+//┌────────────┬─────┬──────┐
+//│ foo │ bar │ baz │
+//│ frobnicate │ bar │ quuz │
+//└────────────┴─────┴──────┘
+```
+
+By setting all chars to empty with the exception of 'middle' being set to a
+single space and by setting padding to zero, it's possible to get the most
+compact layout with no decorations:
+```javascript
+var table = new Table({
+ chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
+ , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
+ , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
+ , 'right': '' , 'right-mid': '' , 'middle': ' ' },
+ style: { 'padding-left': 0, 'padding-right': 0 }
+});
+
+table.push(
+ ['foo', 'bar', 'baz']
+ , ['frobnicate', 'bar', 'quuz']
+);
+
+console.log(table.toString());
+// Outputs:
+//foo bar baz
+//frobnicate bar quuz
+```
+
## Running tests
Clone the repository with all its submodules and run:
@@ -34,6 +34,7 @@ function Table (options){
, 'mid-mid': ''
, 'right': ''
, 'right-mid': ''
+ , 'middle': ''
}
, truncate: ''
, colWidths: []
@@ -146,16 +147,17 @@ Table.prototype.toString = function (){
line = line.substr(0, width) + intersection + line.substr(width + 1);
});
- ret += applyStyles(options.style.border, line);
+ return applyStyles(options.style.border, line);
};
// draws the top line
function lineTop (){
- line(chars.top
- , chars['top-left'] || chars.top
- , chars['top-right'] || chars.top
- , chars['top-mid']);
- ret += "\n";
+ var l = line(chars.top
+ , chars['top-left'] || chars.top
+ , chars['top-right'] || chars.top
+ , chars['top-mid']);
+ if (l)
+ ret += l + "\n";
};
function generateRow (items, style) {
@@ -213,13 +215,15 @@ Table.prototype.toString = function (){
ret += "\n" + applyStyles(options.style.border, chars.left);
}
- ret += line.join(applyStyles(options.style.border, chars.right)) + applyStyles(options.style.border, chars.right);
+ ret += line.join(applyStyles(options.style.border, chars.middle)) + applyStyles(options.style.border, chars.right);
});
return applyStyles(options.style.border, chars.left) + ret;
};
function applyStyles(styles, subject) {
+ if (!subject)
+ return '';
styles.forEach(function(style) {
subject = subject[style];
});
@@ -257,12 +261,12 @@ Table.prototype.toString = function (){
lineTop();
else {
if (!style.compact || i<(!!head.length) ?1:0 || cells.length == 0){
- line(chars.mid
- , chars['left-mid']
- , chars['right-mid']
- , chars['mid-mid']);
-
- ret += "\n"
+ var l = line(chars.mid
+ , chars['left-mid']
+ , chars['right-mid']
+ , chars['mid-mid']);
+ if (l)
+ ret += l + "\n"
}
}
@@ -273,10 +277,15 @@ Table.prototype.toString = function (){
};
});
- line(chars.bottom
- , chars['bottom-left'] || chars.bottom
- , chars['bottom-right'] || chars.bottom
- , chars['bottom-mid']);
+ var l = line(chars.bottom
+ , chars['bottom-left'] || chars.bottom
+ , chars['bottom-right'] || chars.bottom
+ , chars['bottom-mid']);
+ if (l)
+ ret += l;
+ else
+ // trim the last '\n' if we didn't add the bottom decoration
+ ret = ret.slice(0, -1);
return ret;
};
View
@@ -115,5 +115,137 @@ module.exports = {
];
table.toString().should.eql(expected.join("\n"));
- }
+ },
+
+ 'test custom chars': function (){
+ var table = new Table({
+ chars: {
+ 'top': ''
+ , 'top-mid': ''
+ , 'top-left': ''
+ , 'top-right': ''
+ , 'bottom': ''
+ , 'bottom-mid': ''
+ , 'bottom-left': ''
+ , 'bottom-right': ''
+ , 'left': ''
+ , 'left-mid': ''
+ , 'right': ''
+ , 'right-mid': ''
+ },
+ style: {
+ head: []
+ , border: []
+ }
+ });
+
+ table.push(
+ ['foo', 'bar', 'baz']
+ , ['frob', 'bar', 'quuz']
+ );
+
+ var expected = [
+ '╔══════╤═════╤══════╗'
+ , '║ foo │ bar │ baz ║'
+ , '╟──────┼─────┼──────╢'
+ , '║ frob │ bar │ quuz ║'
+ , '╚══════╧═════╧══════╝'
+ ];
+
+ table.toString().should.eql(expected.join("\n"));
+ },
+
+ 'test compact shortand': function (){
+ var table = new Table({
+ style: {
+ head: []
+ , border: []
+ , compact : true
+ }
+ });
+
+ table.push(
+ ['foo', 'bar', 'baz']
+ , ['frob', 'bar', 'quuz']
+ );
+
+ var expected = [
+ '┌──────┬─────┬──────┐'
+ , '│ foo │ bar │ baz │'
+ , '│ frob │ bar │ quuz │'
+ , '└──────┴─────┴──────┘'
+ ];
+
+ table.toString().should.eql(expected.join("\n"));
+ },
+
+ 'test compact empty mid line': function (){
+ var table = new Table({
+ chars: {
+ 'mid': ''
+ , 'left-mid': ''
+ , 'mid-mid': ''
+ , 'right-mid': ''
+ },
+ style: {
+ head: []
+ , border: []
+ }
+ });
+
+ table.push(
+ ['foo', 'bar', 'baz']
+ , ['frob', 'bar', 'quuz']
+ );
+
+ var expected = [
+ '┌──────┬─────┬──────┐'
+ , '│ foo │ bar │ baz │'
+ , '│ frob │ bar │ quuz │'
+ , '└──────┴─────┴──────┘'
+ ];
+
+ table.toString().should.eql(expected.join("\n"));
+ },
+
+ 'test decoration lines disabled': function (){
+ var table = new Table({
+ chars: {
+ 'top': ''
+ , 'top-mid': ''
+ , 'top-left': ''
+ , 'top-right': ''
+ , 'bottom': ''
+ , 'bottom-mid': ''
+ , 'bottom-left': ''
+ , 'bottom-right': ''
+ , 'left': ''
+ , 'left-mid': ''
+ , 'mid': ''
+ , 'mid-mid': ''
+ , 'right': ''
+ , 'right-mid': ''
+ , 'middle': ' ' // a single space
+ },
+ style: {
+ head: []
+ , border: []
+ , 'padding-left': 0
+ , 'padding-right': 0
+ }
+ });
+
+ table.push(
+ ['foo', 'bar', 'baz']
+ , ['frobnicate', 'bar', 'quuz']
+ );
+
+ var expected = [
+ 'foo bar baz '
+ , 'frobnicate bar quuz'
+ ];
+
+ table.toString().should.eql(expected.join("\n"));
+ },
+
};