UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

84 lines (67 loc) 1.76 kB
const utils = require('./utils'); const tableLayout = require('./layout-manager'); class Table extends Array { constructor(options) { super(); this.options = utils.mergeOptions(options); } toString() { let array = this; const headersPresent = this.options.head && this.options.head.length; if (headersPresent) { array = [this.options.head]; if (this.length) { array.push.apply(array, this); } } else { this.options.style.head = []; } const cells = tableLayout.makeTableLayout(array); cells.forEach(function (row) { row.forEach(function (cell) { cell.mergeTableOptions(this.options, cells); }, this); }, this); tableLayout.computeWidths(this.options.colWidths, cells); tableLayout.computeHeights(this.options.rowHeights, cells); cells.forEach(function (row) { row.forEach(function (cell) { cell.init(this.options); }, this); }, this); const result = []; for (let rowIndex = 0; rowIndex < cells.length; rowIndex++) { const row = cells[rowIndex]; const heightOfRow = this.options.rowHeights[rowIndex]; if ( rowIndex === 0 || !this.options.style.compact || (rowIndex == 1 && headersPresent) ) { doDraw(row, 'top', result); } for (let lineNum = 0; lineNum < heightOfRow; lineNum++) { doDraw(row, lineNum, result); } if (rowIndex + 1 == cells.length) { doDraw(row, 'bottom', result); } } return result.join('\n'); } get width() { const str = this.toString().split('\n'); return str[0].length; } } function doDraw(row, lineNum, result) { const line = []; row.forEach((cell) => { line.push(cell.draw(lineNum)); }); const str = line.join(''); if (str.length) { result.push(str); } } module.exports = Table;