UNPKG

@mail-core/cli

Version:

Инструментарий для написания cli-скриптов

41 lines 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.list = exports.getLengthOffset = void 0; const stringLength = require("string-length"); function getLengthOffset(val) { const re = /[^\w]+(\[\d+m)/ug; let len = 0; let match = null; while (match = re.exec(val)) { len += match[1].length + 1; } return len; } exports.getLengthOffset = getLengthOffset; function list(console, items, options = {}) { const { bullet = '→', indent = 1, glue = ' ', fixedCol = true, } = options; const colPad = {}; const calcColPad = (val, idx) => { colPad[idx] = Math.max(val == null ? 0 : stringLength(val), colPad[idx] || 0); }; if (fixedCol) { items.forEach((item) => { if (Array.isArray(item)) { if (fixedCol === true) { item.forEach(calcColPad); } else { calcColPad(item[fixedCol], fixedCol); } } }); } items.forEach((item) => { const args = [bullet.padStart(indent)].concat(item).map((val, idx) => { return val.padEnd(colPad[idx - 1] ? colPad[idx - 1] + getLengthOffset(val) : 0); }); console.log(args.join(glue)); }); } exports.list = list; //# sourceMappingURL=list.js.map