UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

109 lines 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toPrettyTable = void 0; const table = require("table"); const logger_1 = require("../util/logger"); function toPrettyTable(obj, options) { var _a, _b, _c, _d; const orig = obj; if (!options.color) { options._hints = (_a = options._hints) !== null && _a !== void 0 ? _a : {}; options._hints.border = 'ramac'; } // handle API object let kind = obj.kind; if (kind == 'list') { kind = obj.itemKind; obj = obj.items; } else if (kind == 'queryresult') { kind = (_b = obj.query) === null || _b === void 0 ? void 0 : _b.kind; obj = obj.items; } if ((_c = options._hints) === null || _c === void 0 ? void 0 : _c.model) { logger_1.logger.debug(`explicit model found in the hints`); let explicitModel = options._hints.model; if (typeof explicitModel == 'function') { // create model explicitModel = explicitModel.call(null, options); } // explicit model found in hints return render(obj, explicitModel, options); } // handle misc if ((_d = options._hints) === null || _d === void 0 ? void 0 : _d.kind) { logger_1.logger.debug(`explicit kind found in the hints`); const model = makeModel(options._hints.kind, options); if (!model) { logger_1.logger.debug(`no custom renderer for ${options._hints.kind} registered`); } else { return render(obj, model, options); } } if (kind) { const model = makeModel(kind, options); if (!model) { logger_1.logger.debug(`no api renderer for <${kind}> registered`); } else { return render(obj, model, options); } } logger_1.logger.debug(`Forced to render text as json`); if (options.color) { return require('json-colorizer')(orig, { pretty: true }) + '\n'; } else { return JSON.stringify(orig, undefined, 2) + '\n'; } } exports.toPrettyTable = toPrettyTable; function render(itemOrArray, model, options) { var _a, _b; if (!Array.isArray(itemOrArray)) { // wrap in an array for simplicity itemOrArray = [itemOrArray]; } const config = { columns: model.columns, border: table.getBorderCharacters((_b = (_a = options._hints) === null || _a === void 0 ? void 0 : _a.border) !== null && _b !== void 0 ? _b : 'norc'), columnDefault: { paddingLeft: 0, }, drawHorizontalLine: (index, size) => { return index == 0 || index == 1 || index == itemOrArray.length + 1; }, }; const data = []; // put the header in const header = []; data.push(header); for (let col of model.columns) { header.push(col.name); } // list of api objects for (let item of itemOrArray) { const row = model.makeRow(item); for (let i in row) { if (typeof row[i] == 'function') { row[i] = row[i].call(null, item, options); } } data.push(row); } return table.table(data, config); } function makeModel(kind, options) { const obj = require('./models')[kind]; if (obj === undefined) { return undefined; } if (typeof obj == 'function') { // create model return obj.call(null, options); } // just use existing model return obj; } //# sourceMappingURL=table.js.map