UNPKG

snake-cli-ts

Version:
176 lines (175 loc) 7.62 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var chalk_1 = __importDefault(require("chalk")); var InputController_1 = __importDefault(require("../helpers/InputController")); var Console_1 = __importDefault(require("../helpers/Console")); var events_1 = require("events"); var MenuOptions_1 = __importDefault(require("./MenuOptions")); var CLIMenu = (function (_super) { __extends(CLIMenu, _super); function CLIMenu(settings) { if (settings === void 0) { settings = {}; } var _this = _super.call(this) || this; _this.maxTextWidth = 0; _this.inited = false; _this.title = settings.title || ''; _this.parent = settings.parent; _this.options = (settings.options || []).map(function (opt) { return new MenuOptions_1.default(opt, _this); }); _this.selectedOption = _this.options.filter(function (option) { return !option.disabled; })[0]; _this.onResize(); process.stdout.on('resize', function () { return _this.onResize(); }); return _this; } Object.defineProperty(CLIMenu.prototype, "width", { get: function () { return process.stdout.columns; }, enumerable: false, configurable: true }); Object.defineProperty(CLIMenu.prototype, "height", { get: function () { return process.stdout.rows; }, enumerable: false, configurable: true }); CLIMenu.prototype.onResize = function () { var _this = this; this.maxTextWidth = Math.max.apply(Math, __spread((this.options || []) .map(function (option) { return option.name; }) .concat(this.title) .map(function (str) { return _this.calculateTextSize(str).width; }))); if (this.inited) { this.print(); } }; CLIMenu.prototype.init = function () { var self = this; self.inited = true; function onKeyPress(name) { var _a, _b; if (name === 'down' || name === 'up') { var arr = name === 'down' ? __spread(self.options) : __spread(self.options).reverse(); var index = arr.indexOf(self.selectedOption); self.selectedOption = __spread(arr.slice(index + 1), arr.slice(0, index + 1)).filter(function (option) { return !option.disabled; })[0]; self.print(); } else if (name === 'return' || name === 'space') { (_b = (_a = self.selectedOption) === null || _a === void 0 ? void 0 : _a.callback) === null || _b === void 0 ? void 0 : _b.call(self.selectedOption); } } InputController_1.default.on('keypress', onKeyPress); this.on('destroy', function () { InputController_1.default.off('keypress', onKeyPress); }); this.options.forEach(function (option) { var _a; (_a = option.onInit) === null || _a === void 0 ? void 0 : _a.call(option); }); this.print(); }; CLIMenu.prototype.destroy = function () { this.inited = false; Console_1.default.clear(); this.emit('destroy'); }; CLIMenu.prototype.calculateTextSize = function (text) { var rows = text.split('\n'); return { width: Math.max.apply(Math, __spread(rows.map(function (s) { return s.length; }))), height: rows.length, }; }; CLIMenu.prototype.center = function (text, modifyText, modifyMargin) { if (modifyText === void 0) { modifyText = function (val) { return val; }; } if (modifyMargin === void 0) { modifyMargin = 0; } var maxWidth = this.calculateTextSize(text).width; var padding = ' '.repeat(Math.floor((this.width - maxWidth) / 2)); return text .split('\n') .map(function (s) { var marginedStartPadding = padding.slice(0, padding.length - modifyMargin); var str = ' '.repeat(modifyMargin) + s + ' '.repeat(modifyMargin); return "" + marginedStartPadding + modifyText(str); }) .join('\n'); }; CLIMenu.prototype.print = function () { var e_1, _a; var height = this.title.split('\n').length + this.options.reduce(function (prev, now) { return prev + now.name.split('\n').length; }, 0); Console_1.default.clear(); Console_1.default.log('\n'.repeat(Math.floor(this.height - height) / 4)); Console_1.default.log(this.center(this.title, chalk_1.default.green)); try { for (var _b = __values(this.options), _c = _b.next(); !_c.done; _c = _b.next()) { var option = _c.value; var title = void 0; var margin = Math.floor((this.maxTextWidth - option.name.length) / 2); if (option === this.selectedOption) { title = this.center(option.name, function (txt) { return chalk_1.default.bgGreen(chalk_1.default.black(txt)); }, margin); } else { title = this.center(option.name, function (txt) { return chalk_1.default.green(txt); }, margin); } Console_1.default.log(title); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; return CLIMenu; }(events_1.EventEmitter)); exports.default = CLIMenu;