Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugging capabilities #288

Merged
merged 1 commit into from
Apr 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ console.log(table.toString());
//frobnicate bar quuz
```

## Debugging

Later versions of cli-table3 supporting debugging your table data.

Enable and use debugging:

```
var table = new Table({ debug: 1 });
table.push([{}, {},}); // etc.
console.log(table.toString());
table.messages.forEach((message) => console.log(message));
```

If you are rendering multiple tables with debugging on run `Table.reset()` after
rendering each table.

## Build Targets

Clone the repository and run `yarn install` to install all its submodules, then run one of the following commands:
Expand Down
13 changes: 12 additions & 1 deletion src/cell.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { info, debug } = require('./debug');
const utils = require('./utils');

class Cell {
Expand Down Expand Up @@ -111,6 +112,12 @@ class Cell {
draw(lineNum, spanningCell) {
if (lineNum == 'top') return this.drawTop(this.drawRight);
if (lineNum == 'bottom') return this.drawBottom(this.drawRight);
let content = utils.truncate(this.content, 10, this.truncate);
if (!lineNum) {
info(`${this.y}-${this.x}: ${this.rowSpan - lineNum}x${this.colSpan} Cell ${content}`);
} else {
// debug(`${lineNum}-${this.x}: 1x${this.colSpan} RowSpanCell ${content}`);
}
let padLen = Math.max(this.height - this.lines.length, 0);
let padTop;
switch (this.vAlign) {
Expand Down Expand Up @@ -286,7 +293,10 @@ class ColSpanCell {
*/
constructor() {}

draw() {
draw(lineNum) {
if (typeof lineNum === 'number') {
debug(`${this.y}-${this.x}: 1x1 ColSpanCell`);
}
return '';
}

Expand Down Expand Up @@ -320,6 +330,7 @@ class RowSpanCell {
if (lineNum == 'bottom') {
return this.originalCell.draw('bottom');
}
debug(`${this.y}-${this.x}: 1x${this.colSpan} RowSpanCell for ${this.originalCell.content}`);
return this.originalCell.draw(this.offset + 1 + lineNum);
}

Expand Down
28 changes: 28 additions & 0 deletions src/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let messages = [];
let level = 0;

const debug = (msg, min) => {
if (level >= min) {
messages.push(msg);
}
};

debug.WARN = 1;
debug.INFO = 2;
debug.DEBUG = 3;

debug.reset = () => {
messages = [];
};

debug.setDebugLevel = (v) => {
level = v;
};

debug.warn = (msg) => debug(msg, debug.WARN);
debug.info = (msg) => debug(msg, debug.INFO);
debug.debug = (msg) => debug(msg, debug.DEBUG);

debug.debugMessages = () => messages;

module.exports = debug;
4 changes: 3 additions & 1 deletion src/layout-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { warn, debug } = require('./debug');
const Cell = require('./cell');
const { ColSpanCell, RowSpanCell } = Cell;

Expand Down Expand Up @@ -126,6 +127,7 @@ const { ColSpanCell, RowSpanCell } = Cell;
function fillInTable(table) {
let h_max = maxHeight(table);
let w_max = maxWidth(table);
debug(`Max rows: ${h_max}; Max cols: ${w_max}`);
for (let y = 0; y < h_max; y++) {
for (let x = 0; x < w_max; x++) {
if (!conflictExists(table, x, y)) {
Expand All @@ -140,10 +142,10 @@ const { ColSpanCell, RowSpanCell } = Cell;
opts.rowSpan++;
y2++;
}

let cell = new Cell(opts);
cell.x = opts.x;
cell.y = opts.y;
warn(`Missing cell at ${cell.y}-${cell.x}.`);
insertCell(cell, table[y]);
}
}
Expand Down
33 changes: 31 additions & 2 deletions src/table.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
const debug = require('./debug');
const utils = require('./utils');
const tableLayout = require('./layout-manager');

class Table extends Array {
constructor(options) {
constructor(opts) {
super();

this.options = utils.mergeOptions(options);
const options = utils.mergeOptions(opts);
Object.defineProperty(this, 'options', {
value: options,
enumerable: options.debug,
});

if (options.debug) {
switch (typeof options.debug) {
case 'boolean':
debug.setDebugLevel(debug.WARN);
break;
case 'number':
debug.setDebugLevel(options.debug);
break;
case 'string':
debug.setDebugLevel(parseInt(options.debug, 10));
break;
default:
debug.setDebugLevel(debug.WARN);
debug.warn(`Debug option is expected to be boolean, number, or string. Received a ${typeof options.debug}`);
}
Object.defineProperty(this, 'messages', {
get() {
return debug.debugMessages();
},
});
}
}

toString() {
Expand Down Expand Up @@ -65,6 +92,8 @@ class Table extends Array {
}
}

Table.reset = () => debug.reset();

function doDraw(row, lineNum, result) {
let line = [];
row.forEach(function (cell) {
Expand Down
35 changes: 35 additions & 0 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ describe('@api Table ', function () {
];
expect(table.toString()).toEqual(expected.join('\n'));
});
describe('debugging', () => {
afterEach(() => Table.reset());
it('is not accessible when disabled', () => {
let table = new Table();
expect(table.messages).toBeUndefined();
});
it('warns of missing cells', () => {
let table = new Table({ debug: true });
table.push([{ rowSpan: 2 }], [{}]);
table.toString();
expect(table.messages).toEqual(['Missing cell at 0-1.']);
});
it('provides cell info', () => {
let table = new Table({ debug: 2 });
table.push(['a', 'b', { content: 'c', rowSpan: 2 }], [{ content: 'd', colSpan: 2 }]);
table.toString();
expect(table.messages).toContain('0-0: 1x1 Cell a');
expect(table.messages).toContain('0-1: 1x1 Cell b');
expect(table.messages).toContain('0-2: 2x1 Cell c');
expect(table.messages).toContain('1-0: 1x2 Cell d');
});
it('provides rowSpan and colSpan cell debug info', () => {
let table = new Table({ debug: 3 });
table.push(['a', 'b', { content: 'c', rowSpan: 2 }], [{ content: 'd', colSpan: 2 }]);
table.toString();
expect(table.messages).toContain('1-1: 1x1 ColSpanCell');
expect(table.messages).toContain('1-2: 1x1 RowSpanCell for c');
});
it('provides debug info', () => {
let table = new Table({ debug: 3 });
table.push([{}, {}], [{}, {}]);
table.toString();
expect(table.messages).toContain('Max rows: 2; Max cols: 2');
});
});
});

/*
Expand Down