cli-block
Version:
Create nice looking CLI Blocks
69 lines • 3.43 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.blockTable = exports.createBlockTable = void 0;
const util_1 = require("../util");
const border_1 = require("../border");
const settings_1 = require("../settings");
const util_2 = require("../util");
const blocks_line_1 = require("./blocks.line");
const config_1 = require("../settings/config");
const createBlockTable = (table, settings = {}, config = null) => __awaiter(void 0, void 0, void 0, function* () {
settings = (0, settings_1.useSettings)(settings);
config = (0, config_1.useConfig)(config);
const getTableWidth = (table) => {
let height = 1;
table.forEach((item) => {
if (typeof item == "object" && height < item.length)
height = item.length;
});
return height;
};
const width = Math.floor((0, settings_1.getContentWidth)(settings) / getTableWidth(table)) - 2;
// Check if all tables
table = table.map((item) => typeof item == "string" ? (item = [item]) : (item = item));
settings.tableSpace && (table = [[], ...table, []]);
settings.tableHeader && table.splice(2, 0, []);
// for (let r = 0; r < table.length; r++) {
table = table.map((row) => {
return (row = [
...row,
...new Array(getTableWidth(table) - row.length),
]).map((i) => i == undefined
? (i = (0, util_2.spacedText)(width, ""))
: (i = (0, util_2.spacedText)(width, (0, util_2.stylizeValue)(i))));
});
settings.tableHeader &&
(table[1] = table[1].map((item) => (item = `${(0, util_1.bold)(item)}`)));
let lines = [];
config.margin &&
config.marginTop &&
lines.push((0, blocks_line_1.createBlockLine)(null, settings)[0]);
config.header && lines.push((0, blocks_line_1.createBlockLine)(config.header, settings)[0]);
table.forEach((row) => {
lines.push((0, blocks_line_1.createBlockLine)(row.join(` ${(0, border_1.border)(border_1.BorderElement.side, settings)} `), settings));
});
config.footer && lines.push((0, blocks_line_1.createBlockLine)(config.footer, settings)[0]);
config.margin &&
config.marginBottom &&
lines.push((0, blocks_line_1.createBlockLine)(null, settings)[0]);
return lines;
});
exports.createBlockTable = createBlockTable;
const blockTable = (table, settings = {}, config = null) => __awaiter(void 0, void 0, void 0, function* () {
const cfg = (0, settings_1.useSettings)(settings);
const lines = yield (0, exports.createBlockTable)(table, cfg, config);
lines.forEach((line) => {
(0, util_2.logger)(line, cfg);
});
});
exports.blockTable = blockTable;
//# sourceMappingURL=blocks.table.js.map
;