Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
minor rendering fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 19, 2014
1 parent 9b3b2bb commit 3a6ea88
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ Cell.prototype._topLeftChar = function(offset){
leftChar = offset == 0 ? 'midMid' : 'bottomMid';
if(this.cells){ //TODO: cells should always exist - some tests don't fill it in though
var spanAbove = this.cells[this.y-1][x] instanceof Cell.ColSpanCell;
var spanLeft = offset == 0 && this.cells[this.y][x-1] instanceof Cell.RowSpanCell;
if(spanAbove){
leftChar = offset == 0 ? 'topMid' : 'mid';
}
if(spanLeft){
leftChar = 'leftMid';
if(offset == 0){
var i = 1;
while(this.cells[this.y][x-i] instanceof Cell.ColSpanCell){
i++;
}
if(this.cells[this.y][x-i] instanceof Cell.RowSpanCell){
leftChar = 'leftMid';
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/layout-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ module.exports = {
addRowSpanCells: addRowSpanCells,
maxWidth:maxWidth,
fillInTable:fillInTable,
computeWidths:makeComputeWidths('colSpan','desiredWidth','x',0),
computeWidths:makeComputeWidths('colSpan','desiredWidth','x',1),
computeHeights:makeComputeWidths('rowSpan','desiredHeight','y',1)
};
})();
Expand Down Expand Up @@ -229,5 +229,8 @@ function makeComputeWidths(colSpan,desiredWidth,x,forcedMin){
}
}
_.extend(vals,result);
for(var j = 0; j < vals.length; j++){
vals[j] = Math.max(forcedMin, vals[j] || 0);
}
};
}
4 changes: 2 additions & 2 deletions test/table-layout-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ describe('tableLayout', function () {
expect(widths).to.eql([8, 3, 5]);
});

it('assumes undefined desiredWidth is 0', function () {
it('assumes undefined desiredWidth is 1', function () {
var widths = [];
var cells = [[{x:0,y:0}], [{x:0,y:1}], [{x:0,y:2}]];
computeWidths(widths, cells);
expect(widths).to.eql([0])
expect(widths).to.eql([1])
});

it('takes into account colSpan and wont over expand', function () {
Expand Down
111 changes: 108 additions & 3 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ describe('@api Table', function () {
['']
);

//console.log(table.toString());
//console.log(table.options.rowHeights);

var expected = [
'┌──┬──┐'
, '│ │ │'
Expand All @@ -173,6 +170,114 @@ describe('@api Table', function () {

expect(table.toString()).to.equal(expected.join('\n'));
});

it('stairstep spans - empty cells autofilled',function(){
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'',rowSpan:3,colSpan:2},'hi'],
[],
[{content:'',rowSpan:2,colSpan:2}],
[]
);

var expected = [
'┌───┬────┬──┐'
, '│ │ hi │ │' // top-right and bottom-left cells are automatically created to fill the empty space
, '│ ├────┤ │'
, '│ │ │ │'
, '│ ├────┴──┤'
, '│ │ │'
, '├───┤ │'
, '│ │ │'
, '└───┴───────┘'
];

console.log(table.toString());
expect(table.toString()).to.equal(expected.join('\n'));
});

it('truncation symbol shows if there are undisplayed lines',function(){
var table = new Table({rowHeights:[1],style:{head:[],border:[]}});

table.push(['hi\nhello']);


var expected = [
'┌───────┐'
, '│ hi… │'
, '└───────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('colSpan width expansion',function(){
var table = new Table({style:{head:[],border:[]}});
table.push(
[{colSpan:2,content:'hello there'}],
['hi', 'hi']
);

var expected = [
'┌─────────────┐'
, '│ hello there │'
, '├──────┬──────┤'
, '│ hi │ hi │'
, '└──────┴──────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('colSpan width expansion - specified widths',function(){
var table = new Table({colWidths:[4],style:{head:[],border:[]}});
table.push(
[{colSpan:2,content:'hello there'}],
['hi',{hAlign:'center',content:'hi'}]
);

var expected = [
'┌─────────────┐'
, '│ hello there │'
, '├────┬────────┤'
, '│ hi │ hi │'
, '└────┴────────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('colSpan width expansion - null widths',function(){
var table = new Table({colWidths:[null, 4],style:{head:[],border:[]}});
table.push(
[{colSpan:2,content:'hello there'}],
[{hAlign:'right',content:'hi'}, 'hi']
);

var expected = [
'┌─────────────┐'
, '│ hello there │'
, '├────────┬────┤'
, '│ hi │ hi │'
, '└────────┴────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('colorful truncation',function(){
var table = new Table({colWidths:[5],style:{head:[],border:[]}});

table.push([colors.red('hello')]);
var expected = [
'┌─────┐'
, '│ ' + colors.red('he') + '… │'
, '└─────┘'
];

expect(table.toString()).to.equal(expected.join('\n'))
});
});

/*
Expand Down

0 comments on commit 3a6ea88

Please sign in to comment.