@zombienet/utils
Version:
Useful utilities for ZombieNet Framework
110 lines (109 loc) • 4.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateLogTable = exports.setLogType = exports.getLogType = void 0;
const cli_table3_1 = __importDefault(require("cli-table3"));
const colors_1 = require("./colors");
const chars = {
top: "═",
"top-mid": "╤",
"top-left": "╔",
"top-right": "╗",
bottom: "═",
"bottom-mid": "╧",
"bottom-left": "╚",
"bottom-right": "╝",
left: "║",
"left-mid": "╟",
mid: "─",
"mid-mid": "┼",
right: "║",
"right-mid": "╢",
middle: "│",
};
// Module level config.
let logType = "table";
const logTypeValues = ["text", "table", "silent"];
const getLogType = (logType) => {
if (logTypeValues.includes(logType)) {
return logType;
}
else {
logType &&
console.error(`${colors_1.decorators.red(`
Argument 'logType' provided ('${logType}') is not one of the accepted params; Falling back to 'table'.
Possible values: ${logTypeValues.join(", ")} - Defaults to 'table'.\n\n`)}`);
return "table";
}
};
exports.getLogType = getLogType;
const setLogType = (value) => {
logType = value;
};
exports.setLogType = setLogType;
class CreateLogTable {
constructor({ head, colWidths, doubleBorder, wordWrap }) {
this.pushTo = (inputs) => {
Array.isArray(inputs) &&
inputs.forEach((input) => {
var _a, _b;
Array.isArray(input) &&
input.forEach((inp, index) => {
const split = this.colWidths[index] - 10;
const times = parseInt((inp.length / split).toString());
if (times > 1) {
const some = inp;
for (let i = 0; i <= times; i++) {
if (i === 0) {
inp = some.substring(0, split);
}
else {
inp += "\n" + some.substring(split * i, split * (i + 1));
}
}
input[index] = inp;
}
});
if (logType === "text") {
if (input[0] === "\x1B[36mCommand\x1B[0m") {
input[1] = input[1].replace(/\n/g, " ");
}
// if input has a JSON - that means a merged cell
if ((_a = input[0]) === null || _a === void 0 ? void 0 : _a.content) {
input[0] = (_b = input[0]) === null || _b === void 0 ? void 0 : _b.content;
}
console.log(input.join(" : "));
}
else if (logType === "silent") {
return;
}
else if (logType === "table") {
this.table.push(input);
}
});
};
this.print = () => {
if (logType === "table")
console.log(this.table.toString());
};
// This function makes the process of creating a table, pushing data and printing it faster
// It is meant to exist in order to reduce the log lines in the code
this.pushToPrint = (inputs) => {
this.pushTo(inputs);
this.print();
};
this.wordWrap = wordWrap || false;
this.colWidths = colWidths;
const params = { colWidths, wordWrap };
if (head === null || head === void 0 ? void 0 : head.length)
params.head = head;
if (doubleBorder) {
params.chars = chars;
}
this.text = [];
this.table = new cli_table3_1.default(params);
}
}
exports.CreateLogTable = CreateLogTable;