Skip to content

Commit 8b2fbab

Browse files
authored
Add debugging capabilities (#288)
1 parent a24050b commit 8b2fbab

File tree

6 files changed

+126
-4
lines changed

6 files changed

+126
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ console.log(table.toString());
167167
//frobnicate bar quuz
168168
```
169169

170+
## Debugging
171+
172+
Later versions of cli-table3 supporting debugging your table data.
173+
174+
Enable and use debugging:
175+
176+
```
177+
var table = new Table({ debug: 1 });
178+
table.push([{}, {},}); // etc.
179+
180+
console.log(table.toString());
181+
table.messages.forEach((message) => console.log(message));
182+
```
183+
184+
If you are rendering multiple tables with debugging on run `Table.reset()` after
185+
rendering each table.
186+
170187
## Build Targets
171188

172189
Clone the repository and run `yarn install` to install all its submodules, then run one of the following commands:

src/cell.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { info, debug } = require('./debug');
12
const utils = require('./utils');
23

34
class Cell {
@@ -111,6 +112,12 @@ class Cell {
111112
draw(lineNum, spanningCell) {
112113
if (lineNum == 'top') return this.drawTop(this.drawRight);
113114
if (lineNum == 'bottom') return this.drawBottom(this.drawRight);
115+
let content = utils.truncate(this.content, 10, this.truncate);
116+
if (!lineNum) {
117+
info(`${this.y}-${this.x}: ${this.rowSpan - lineNum}x${this.colSpan} Cell ${content}`);
118+
} else {
119+
// debug(`${lineNum}-${this.x}: 1x${this.colSpan} RowSpanCell ${content}`);
120+
}
114121
let padLen = Math.max(this.height - this.lines.length, 0);
115122
let padTop;
116123
switch (this.vAlign) {
@@ -286,7 +293,10 @@ class ColSpanCell {
286293
*/
287294
constructor() {}
288295

289-
draw() {
296+
draw(lineNum) {
297+
if (typeof lineNum === 'number') {
298+
debug(`${this.y}-${this.x}: 1x1 ColSpanCell`);
299+
}
290300
return '';
291301
}
292302

@@ -320,6 +330,7 @@ class RowSpanCell {
320330
if (lineNum == 'bottom') {
321331
return this.originalCell.draw('bottom');
322332
}
333+
debug(`${this.y}-${this.x}: 1x${this.colSpan} RowSpanCell for ${this.originalCell.content}`);
323334
return this.originalCell.draw(this.offset + 1 + lineNum);
324335
}
325336

src/debug.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
let messages = [];
2+
let level = 0;
3+
4+
const debug = (msg, min) => {
5+
if (level >= min) {
6+
messages.push(msg);
7+
}
8+
};
9+
10+
debug.WARN = 1;
11+
debug.INFO = 2;
12+
debug.DEBUG = 3;
13+
14+
debug.reset = () => {
15+
messages = [];
16+
};
17+
18+
debug.setDebugLevel = (v) => {
19+
level = v;
20+
};
21+
22+
debug.warn = (msg) => debug(msg, debug.WARN);
23+
debug.info = (msg) => debug(msg, debug.INFO);
24+
debug.debug = (msg) => debug(msg, debug.DEBUG);
25+
26+
debug.debugMessages = () => messages;
27+
28+
module.exports = debug;

src/layout-manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { warn, debug } = require('./debug');
12
const Cell = require('./cell');
23
const { ColSpanCell, RowSpanCell } = Cell;
34

@@ -126,6 +127,7 @@ const { ColSpanCell, RowSpanCell } = Cell;
126127
function fillInTable(table) {
127128
let h_max = maxHeight(table);
128129
let w_max = maxWidth(table);
130+
debug(`Max rows: ${h_max}; Max cols: ${w_max}`);
129131
for (let y = 0; y < h_max; y++) {
130132
for (let x = 0; x < w_max; x++) {
131133
if (!conflictExists(table, x, y)) {
@@ -140,10 +142,10 @@ const { ColSpanCell, RowSpanCell } = Cell;
140142
opts.rowSpan++;
141143
y2++;
142144
}
143-
144145
let cell = new Cell(opts);
145146
cell.x = opts.x;
146147
cell.y = opts.y;
148+
warn(`Missing cell at ${cell.y}-${cell.x}.`);
147149
insertCell(cell, table[y]);
148150
}
149151
}

src/table.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
const debug = require('./debug');
12
const utils = require('./utils');
23
const tableLayout = require('./layout-manager');
34

45
class Table extends Array {
5-
constructor(options) {
6+
constructor(opts) {
67
super();
78

8-
this.options = utils.mergeOptions(options);
9+
const options = utils.mergeOptions(opts);
10+
Object.defineProperty(this, 'options', {
11+
value: options,
12+
enumerable: options.debug,
13+
});
14+
15+
if (options.debug) {
16+
switch (typeof options.debug) {
17+
case 'boolean':
18+
debug.setDebugLevel(debug.WARN);
19+
break;
20+
case 'number':
21+
debug.setDebugLevel(options.debug);
22+
break;
23+
case 'string':
24+
debug.setDebugLevel(parseInt(options.debug, 10));
25+
break;
26+
default:
27+
debug.setDebugLevel(debug.WARN);
28+
debug.warn(`Debug option is expected to be boolean, number, or string. Received a ${typeof options.debug}`);
29+
}
30+
Object.defineProperty(this, 'messages', {
31+
get() {
32+
return debug.debugMessages();
33+
},
34+
});
35+
}
936
}
1037

1138
toString() {
@@ -65,6 +92,8 @@ class Table extends Array {
6592
}
6693
}
6794

95+
Table.reset = () => debug.reset();
96+
6897
function doDraw(row, lineNum, result) {
6998
let line = [];
7099
row.forEach(function (cell) {

test/table-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ describe('@api Table ', function () {
129129
];
130130
expect(table.toString()).toEqual(expected.join('\n'));
131131
});
132+
describe('debugging', () => {
133+
afterEach(() => Table.reset());
134+
it('is not accessible when disabled', () => {
135+
let table = new Table();
136+
expect(table.messages).toBeUndefined();
137+
});
138+
it('warns of missing cells', () => {
139+
let table = new Table({ debug: true });
140+
table.push([{ rowSpan: 2 }], [{}]);
141+
table.toString();
142+
expect(table.messages).toEqual(['Missing cell at 0-1.']);
143+
});
144+
it('provides cell info', () => {
145+
let table = new Table({ debug: 2 });
146+
table.push(['a', 'b', { content: 'c', rowSpan: 2 }], [{ content: 'd', colSpan: 2 }]);
147+
table.toString();
148+
expect(table.messages).toContain('0-0: 1x1 Cell a');
149+
expect(table.messages).toContain('0-1: 1x1 Cell b');
150+
expect(table.messages).toContain('0-2: 2x1 Cell c');
151+
expect(table.messages).toContain('1-0: 1x2 Cell d');
152+
});
153+
it('provides rowSpan and colSpan cell debug info', () => {
154+
let table = new Table({ debug: 3 });
155+
table.push(['a', 'b', { content: 'c', rowSpan: 2 }], [{ content: 'd', colSpan: 2 }]);
156+
table.toString();
157+
expect(table.messages).toContain('1-1: 1x1 ColSpanCell');
158+
expect(table.messages).toContain('1-2: 1x1 RowSpanCell for c');
159+
});
160+
it('provides debug info', () => {
161+
let table = new Table({ debug: 3 });
162+
table.push([{}, {}], [{}, {}]);
163+
table.toString();
164+
expect(table.messages).toContain('Max rows: 2; Max cols: 2');
165+
});
166+
});
132167
});
133168

134169
/*

0 commit comments

Comments
 (0)