UNPKG

entodicton

Version:

65 lines (57 loc) 1.4 kB
class Lines { constructor (widths) { this.widths = widths this.lines = [] this.rows = [] } addLine () { this.lines.push(this.widths.map((width) => ''.padEnd(width))) } // will wrap to next line within the column setElement (row, column, value, fullWidth = false) { const values = value.toString().split('\n') if (column >= this.widths.length) { throw new Error('Column out of range.') } const width = fullWidth ? value.length : this.widths[column] let index = 0 for (value of values) { while (value.length > 0) { const lineValue = value.substring(0, width).padEnd(width) const remaining = value.substring(width) const line = this.getLine(row + index) line[column] = lineValue value = remaining ++index } } } toString () { this.newRow() let result = '' for (const row of this.rows) { result += row result += '\n' } return result } newRow () { for (const line of this.lines) { this.rows.push(line.join('')) } this.lines = [] } log () { for (const line of this.lines) { console.log(line.join('')) } this.lines = [] } getLine (indexFromZero) { while (this.lines.length < indexFromZero + 1) { this.addLine() } return this.lines[indexFromZero] } } module.exports = Lines