@prostojs/tree
Version:
Print tree
96 lines (92 loc) • 4.53 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
const dim = '[2m';
const reset = '[0m';
class ProstoTree {
constructor(options) {
var _a, _b, _c, _d, _e;
const label = (options === null || options === void 0 ? void 0 : options.label) || 'label';
const children = (options === null || options === void 0 ? void 0 : options.children) || 'children';
const branchWidth = typeof (options === null || options === void 0 ? void 0 : options.branchWidth) === 'number' ? options === null || options === void 0 ? void 0 : options.branchWidth : 2;
const hLine = (((_a = options === null || options === void 0 ? void 0 : options.chars) === null || _a === void 0 ? void 0 : _a.hLine) || '─').repeat(branchWidth);
const chars = {
end: ((_b = options === null || options === void 0 ? void 0 : options.chars) === null || _b === void 0 ? void 0 : _b.end) || dim + '└',
middle: ((_c = options === null || options === void 0 ? void 0 : options.chars) === null || _c === void 0 ? void 0 : _c.middle) || dim + '├',
vLine: ((_d = options === null || options === void 0 ? void 0 : options.chars) === null || _d === void 0 ? void 0 : _d.vLine) || dim + '│',
hLine,
node: ((_e = options === null || options === void 0 ? void 0 : options.chars) === null || _e === void 0 ? void 0 : _e.node) || '•',
};
this.options = {
label: label,
children,
renderLabel: (options === null || options === void 0 ? void 0 : options.renderLabel) ||
((n) => typeof n === 'string'
? n
: n[label]),
chars,
branchWidth,
};
}
_render(root, opts) {
const { children, renderLabel, branchWidth: w, chars: c } = this.options;
function renderNode(node, before = '', level = 0, openLevels = []) {
let s = '';
let pref = '';
for (let i = 0; i < level - 1; i++) {
const isOpen = openLevels[i];
pref += `${isOpen ? c.vLine : ' '}${' '.repeat(w)}`;
}
const labels = [renderLabel(node, level)].flat();
const items = (node[children] || []);
openLevels[level] = !!items.length;
let pref2 = '';
for (let i = 0; i <= level; i++) {
const isOpen = openLevels[i];
pref2 += `${isOpen ? c.vLine : ' '}${' '.repeat(w)}`;
}
pref2 = pref2.slice(0, -w) + ' ';
for (let i = 0; i < labels.length; i++) {
const l = labels[i];
if (i === 0) {
s += `${pref}${before}${c.node} ${l}\n`;
}
else {
s += `${pref2}${l}\n`;
}
}
if (items.length && (opts === null || opts === void 0 ? void 0 : opts.level) && level >= opts.level - 1) {
s += pref2 + renderCollapsedChildren(items.length) + '\n';
openLevels[level] = false;
}
else {
for (let i = 0; i < items.length; i++) {
const last = i === items.length - 1;
if ((opts === null || opts === void 0 ? void 0 : opts.childrenLimit) && i === opts.childrenLimit && (!opts.showLast || !last)) {
s += pref2 + renderCollapsedChildren(items.length - i) + '\n';
openLevels[level] = false;
if (opts.showLast) {
s += `${renderNode(items[items.length - 1], `${c.end}${c.hLine}`, level + 1, openLevels)}`;
}
break;
}
openLevels[level] = !last;
s += `${renderNode(items[i], `${last ? c.end : c.middle}${c.hLine}`, level + 1, openLevels)}`;
}
}
return s;
}
return renderNode(root);
}
render(root, opts) {
return this._render(root, opts);
}
print(root, opts) {
const s = this.render(root, opts);
console.log(s);
return s;
}
}
function renderCollapsedChildren(count) {
return dim + '+ ' + '[3m' + count.toString() + ` item${count === 1 ? '' : 's'}` + reset;
}
exports.ProstoTree = ProstoTree;