diff --git a/src/cell.js b/src/cell.js index 44c6062..7ac4695 100644 --- a/src/cell.js +++ b/src/cell.js @@ -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'; + } } } } diff --git a/src/layout-manager.js b/src/layout-manager.js index a8cf9de..a01c5fd 100644 --- a/src/layout-manager.js +++ b/src/layout-manager.js @@ -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) }; })(); @@ -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); + } }; } diff --git a/test/table-layout-test.js b/test/table-layout-test.js index d221b5c..cebe167 100644 --- a/test/table-layout-test.js +++ b/test/table-layout-test.js @@ -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 () { diff --git a/test/table-test.js b/test/table-test.js index 3d2e5b3..09c586f 100644 --- a/test/table-test.js +++ b/test/table-test.js @@ -158,9 +158,6 @@ describe('@api Table', function () { [''] ); - //console.log(table.toString()); - //console.log(table.options.rowHeights); - var expected = [ '┌──┬──┐' , '│ │ │' @@ -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')) + }); }); /*