HiddenPhox/src/lib/table.js

74 lines
1.6 KiB
JavaScript

module.exports = class {
constructor(titles) {
this._rows = [titles];
this._widths = [];
for (let i = 0; i < this._rows.length; i++) {
for (let _i = 0; _i < this._rows[i].length; _i++) {
this._widths.push(this._rows[i][_i].length + 2);
}
}
}
_updateWidth(row) {
for (const index in row) {
const entry = row[index];
const width = entry.length + 2;
if (width > this._widths[index]) {
this._widths[index] = width;
}
}
}
addRow(row) {
this._rows.push(row);
this._updateWidth(row);
}
render() {
function drawRow(ctx, row, index) {
const columns = [];
for (let i = 0; i < row.length; i++) {
columns.push(row[i].toString().padEnd(ctx._widths[i]));
}
return columns;
}
const toDraw = [];
const queue = this._rows.splice(1);
for (const row in queue) {
const _row = drawRow(this, queue[row]);
toDraw.push(_row.join("|"));
}
this._updateWidth(this._rows[0]);
const trows = [];
for (const index in this._rows[0]) {
const field = this._rows[0][index];
let out = field;
const width = this._widths[index];
out = out.padEnd(width);
trows.push(out);
}
const title_row = trows.join("|");
let seperator_row = "";
for (const index in this._widths) {
seperator_row += "-".repeat(this._widths[index]);
if (index != this._widths.length - 1) {
seperator_row += "+";
}
}
const drawn = [title_row, seperator_row];
for (const index in toDraw) {
drawn.push(toDraw[index]);
}
return drawn.join("\n");
}
};