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

Fix wordWrap with colSpan #29

Merged
merged 1 commit into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Cell.prototype.mergeTableOptions = function(tableOptions,cells){
var fixedWidth = tableOptions.colWidths[this.x];
if(tableOptions.wordWrap && fixedWidth){
fixedWidth -= this.paddingLeft + this.paddingRight;
if(this.colSpan){
var i = 1;
while(i<this.colSpan){
fixedWidth += tableOptions.colWidths[this.x + i];
i++;
}
}
this.lines = utils.colorizeLines(utils.wordWrap(fixedWidth,this.content));
}
else {
Expand Down
81 changes: 75 additions & 6 deletions test/cell-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('Cell',function(){
expect(cell.width).to.equal(15);
});

it('will add colWidths if colSpan > 1',function(){
it('will add colWidths if colSpan > 1 with wordWrap false',function(){
var tableOptions = defaultOptions();
tableOptions.colWidths = [5,10,15];

Expand All @@ -372,6 +372,76 @@ describe('Cell',function(){
cell.init(tableOptions);
expect(cell.width).to.equal(32);
});

it('will add colWidths if colSpan > 1 with wordWrap true',function(){
var tableOptions = defaultOptions();
tableOptions.colWidths = [5,10,15];
tableOptions.wordWrap = true;

var cell = new Cell({colSpan:2});
cell.x=0;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
expect(cell.width).to.equal(16);

cell = new Cell({colSpan:2});
cell.x=1;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
expect(cell.width).to.equal(26);

cell = new Cell({colSpan:3});
cell.x=0;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
expect(cell.width).to.equal(32);
});

it('will use multiple columns for wordWrap text when using colSpan and wordWrap together',function(){
var tableOptions = defaultOptions();
tableOptions.colWidths = [7, 7, 17];
tableOptions.wordWrap = true;

var cell = new Cell({content:"the quick brown fox", colSpan:2});
cell.x=0;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
expect(cell.lines.length).to.equal(2);
expect(cell.lines[0]).to.contain('quick');
expect(cell.lines[1]).to.contain('fox');

cell = new Cell({content:"the quick brown fox", colSpan:2});
cell.x=1;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
console.log(cell.lines);
expect(cell.lines.length).to.equal(1);
expect(cell.lines[0]).to.contain('fox');

cell = new Cell({content:"the quick brown fox", colSpan:3});
cell.x=0;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
console.log(cell.lines);
expect(cell.lines.length).to.equal(1);
expect(cell.lines[0]).to.contain('fox');
});

it('will only use one column for wordWrap text when not using colSpan',function(){
var tableOptions = defaultOptions();
tableOptions.colWidths = [7, 7, 7];
tableOptions.wordWrap = true;

var cell = new Cell({content:"the quick brown fox"});
cell.x=0;
cell.mergeTableOptions(tableOptions);
cell.init(tableOptions);
expect(cell.lines.length).to.equal(4);
expect(cell.lines[1]).to.contain('quick');
expect(cell.lines[3]).to.contain('fox');
});


});

describe('height', function(){
Expand Down Expand Up @@ -652,8 +722,8 @@ describe('Cell',function(){
cell.drawRight = true;
expect(cell.draw(0)).to.equal(colors.gray('L') + colors.red(' hello ') + colors.gray('R'));
});
});
});

describe('second line of text',function(){
beforeEach(function () {
cell.width = 9;
Expand Down Expand Up @@ -688,8 +758,8 @@ describe('Cell',function(){
cell.drawRight = true;
expect(cell.draw(1)).to.equal('M howdy R');
});
});
});

describe('truncated line of text',function(){
beforeEach(function () {
cell.width = 9;
Expand Down Expand Up @@ -933,4 +1003,3 @@ describe('Cell',function(){
});
});
});