|
12 | 12 | this.colmaxlen = [];
|
13 | 13 | };
|
14 | 14 | privates.ListItBuffer.prototype.nl = function() {
|
15 |
| - this.lines.push([]); |
| 15 | + this.lines.push(new Row()); |
16 | 16 | return this;
|
17 | 17 | };
|
18 | 18 | privates.ListItBuffer.prototype.d = function(s) {
|
19 | 19 | if(this.lines.length <= 0) {
|
20 | 20 | this.nl();
|
21 | 21 | }
|
22 | 22 | var row = this.lines.length - 1;
|
23 |
| - var col = this.lines[row].length; |
| 23 | + var col = this.lines[row].getCellLength(); |
24 | 24 | if(col >= this.colmaxlen.length) {
|
25 |
| - this.colmaxlen.push(s.length); |
26 |
| - } else if(this.colmaxlen[col] < s.length) { |
27 |
| - this.colmaxlen[col] = s.length; |
| 25 | + var column = new Column(); |
| 26 | + this.colmaxlen.push(column); |
| 27 | + col = this.colmaxlen.length - 1; |
28 | 28 | }
|
29 |
| - this.lines[row].push(s); |
| 29 | + this.colmaxlen[col].expandWidth(s.length); |
| 30 | + var cell = new DataCell(); |
| 31 | + cell.setData(s); |
| 32 | + this.lines[row].pushCell(cell); |
30 | 33 | return this;
|
31 | 34 | };
|
32 | 35 | privates.ListItBuffer.prototype.toString = function() {
|
|
36 | 39 | var rows = [];
|
37 | 40 | this.lines.forEach(function(line) {
|
38 | 41 | var cols = [];
|
39 |
| - for(var col = 0; col < line.length; col++) { |
40 |
| - var m = this.colmaxlen[col]; |
41 |
| - var s = line[col]; |
42 |
| - while(s.length < m) { |
43 |
| - s += ' '; |
44 |
| - } |
| 42 | + for(var col = 0; col < line.getCellLength(); col++) { |
| 43 | + var s = this.colmaxlen[col].formatCell(line.getCell(col)); |
45 | 44 | cols.push(s);
|
46 | 45 | }
|
47 | 46 | rows.push(cols.join(' '));
|
48 | 47 | }, this);
|
49 | 48 | return rows.join("\n");;
|
50 | 49 | };
|
51 | 50 | module.exports = exports;
|
| 51 | + |
| 52 | + // |
| 53 | + // DataCell class |
| 54 | + // |
| 55 | + var DataCell = function() { |
| 56 | + this.data = ""; |
| 57 | + }; |
| 58 | + DataCell.prototype.getData = function() { |
| 59 | + return this.data; |
| 60 | + }; |
| 61 | + DataCell.prototype.setData = function(data) { |
| 62 | + this.data = data; |
| 63 | + }; |
| 64 | + |
| 65 | + // |
| 66 | + // Column class |
| 67 | + // |
| 68 | + var Column = function() { |
| 69 | + this.width = 0; |
| 70 | + }; |
| 71 | + Column.prototype.getWidth = function() { |
| 72 | + return this.width; |
| 73 | + }; |
| 74 | + Column.prototype.expandWidth = function(width) { |
| 75 | + if(width > this.width) { |
| 76 | + this.width = width; |
| 77 | + } |
| 78 | + }; |
| 79 | + Column.prototype.formatCell = function(cell) { |
| 80 | + var m = this.getWidth(); |
| 81 | + var s = cell.getData(); |
| 82 | + while(s.length < m) { |
| 83 | + s += ' '; |
| 84 | + } |
| 85 | + return s; |
| 86 | + }; |
| 87 | + |
| 88 | + // |
| 89 | + // Row class |
| 90 | + // |
| 91 | + var Row = function() { |
| 92 | + this.cells = []; |
| 93 | + }; |
| 94 | + Row.prototype.getCellLength = function() { |
| 95 | + return this.cells.length; |
| 96 | + }; |
| 97 | + Row.prototype.getCell= function(idx) { |
| 98 | + return this.cells[idx]; |
| 99 | + }; |
| 100 | + Row.prototype.pushCell = function(cell) { |
| 101 | + return this.cells.push(cell); |
| 102 | + }; |
52 | 103 | }());
|
0 commit comments