UNPKG

@rawcmd/typewriters

Version:

Collection of typewriters for styling command-line messages for Rawcmd.

49 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const text_1 = require("@rawcmd/text"); const utils_1 = require("@rawcmd/utils"); function rowTypewriter(columns, options) { columns = columns || []; options = Object.assign({ separatorSymbol: ' ', truncationSymbol: '…' }, options); return (data) => { data = (data || []).map((value) => { return utils_1.toString(value) || ''; }); const config = data.map((value, index) => { return Object.assign({ index, width: Infinity, textLength: Infinity, textWrap: true, textAlign: text_1.TextAlign.LEFT }, columns.find((column) => column.index === index)); }); const grid = data.map((text, index) => { const { truncationSymbol } = options; const { textLength } = config[index]; return text_1.trucateText(text, textLength, truncationSymbol) || ''; }).map((text, index) => { const { width, textWrap } = config[index]; return utils_1.toArray(textWrap ? text_1.wrapText(text, width) : text); }).map((rows, index) => { const { truncationSymbol } = options; const { width } = config[index]; return rows.map((row) => { return text_1.trucateText(row, width, truncationSymbol); }); }); const size = [ data.length, Math.max(...grid.map((rows) => rows.length)), ]; const output = grid.map((rows) => { return text_1.repairAnsi(...rows); }).map((rows, index) => { return rows.concat(...Array(size[1] - rows.length).fill(' ')).map((row) => { const { width, textAlign } = config[index]; return text_1.alignText(row, width, textAlign); }); }).reduce((r, a) => { return a.map((v, i) => [...(r[i] || []), v]); }, []).map((rows) => { return rows.join(options.separatorSymbol || ''); }); return output.length ? output.join(text_1.EOL) : ''; }; } exports.rowTypewriter = rowTypewriter; //# sourceMappingURL=row.js.map