diff --git a/lib/index.js b/lib/index.js index f91dd94..a59c5f7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,7 +12,7 @@ this.colmaxlen = []; }; privates.ListItBuffer.prototype.nl = function() { - this.lines.push([]); + this.lines.push(new Row()); return this; }; privates.ListItBuffer.prototype.d = function(s) { @@ -20,13 +20,16 @@ this.nl(); } var row = this.lines.length - 1; - var col = this.lines[row].length; + var col = this.lines[row].getCellLength(); if(col >= this.colmaxlen.length) { - this.colmaxlen.push(s.length); - } else if(this.colmaxlen[col] < s.length) { - this.colmaxlen[col] = s.length; + var column = new Column(); + this.colmaxlen.push(column); + col = this.colmaxlen.length - 1; } - this.lines[row].push(s); + this.colmaxlen[col].expandWidth(s.length); + var cell = new DataCell(); + cell.setData(s); + this.lines[row].pushCell(cell); return this; }; privates.ListItBuffer.prototype.toString = function() { @@ -36,12 +39,8 @@ var rows = []; this.lines.forEach(function(line) { var cols = []; - for(var col = 0; col < line.length; col++) { - var m = this.colmaxlen[col]; - var s = line[col]; - while(s.length < m) { - s += ' '; - } + for(var col = 0; col < line.getCellLength(); col++) { + var s = this.colmaxlen[col].formatCell(line.getCell(col)); cols.push(s); } rows.push(cols.join(' ')); @@ -49,4 +48,56 @@ return rows.join("\n");; }; module.exports = exports; + + // + // DataCell class + // + var DataCell = function() { + this.data = ""; + }; + DataCell.prototype.getData = function() { + return this.data; + }; + DataCell.prototype.setData = function(data) { + this.data = data; + }; + + // + // Column class + // + var Column = function() { + this.width = 0; + }; + Column.prototype.getWidth = function() { + return this.width; + }; + Column.prototype.expandWidth = function(width) { + if(width > this.width) { + this.width = width; + } + }; + Column.prototype.formatCell = function(cell) { + var m = this.getWidth(); + var s = cell.getData(); + while(s.length < m) { + s += ' '; + } + return s; + }; + + // + // Row class + // + var Row = function() { + this.cells = []; + }; + Row.prototype.getCellLength = function() { + return this.cells.length; + }; + Row.prototype.getCell= function(idx) { + return this.cells[idx]; + }; + Row.prototype.pushCell = function(cell) { + return this.cells.push(cell); + }; }());