zcatalyst-cli
Version:
Command Line Tool for CATALYST
75 lines (74 loc) • 2.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ansi_colors_1 = require("ansi-colors");
const list_js_1 = __importDefault(require("inquirer/lib/prompts/list.js"));
class InquirerPromptWithDescription extends list_js_1.default {
constructor(question, rl, answers) {
super(question, rl, answers);
this.question = question;
}
render() {
let message = this.getQuestion();
if (this.firstRender) {
message += (0, ansi_colors_1.dim)('(Use arrow keys)');
}
if (this.status === 'answered') {
message += (0, ansi_colors_1.cyan)(this.opt.choices.getChoice(this.selected).short + '');
}
else {
if (this.question.description) {
message += '\n' + (0, ansi_colors_1.dim)(this.question.description);
}
const choicesStr = this.listRender(this.opt.choices, this.selected);
const indexPosition = this.opt.choices.indexOf(this.opt.choices.getChoice(this.selected));
const realIndexPosition = this.opt.choices.reduce((acc, value, i) => {
if (i > indexPosition) {
return acc;
}
if (value.type === 'separator') {
return acc + 1;
}
const l = value.name;
if (typeof l !== 'string') {
return acc + 1;
}
const ll = l.split('\n');
return acc + ll.length;
}, 0) - 1;
message +=
'\n' +
this.paginator.paginate(choicesStr || '', realIndexPosition, this.opt.pageSize);
}
this.firstRender = false;
this.screen.render(message, '');
}
listRender(choices, pointer) {
let output = '';
let separatorOffset = 0;
choices.forEach((choice, i) => {
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
if (choice.disabled) {
separatorOffset++;
output += ' - ' + choice.name;
output += ` (${typeof choice.disabled === 'string' ? choice.disabled : 'Disabled'})`;
output += '\n';
return;
}
const isSelected = i - separatorOffset === pointer;
let line = (isSelected ? '❯' + ' ' : ' ') + choice.name;
if (isSelected) {
line = (0, ansi_colors_1.cyan)(line);
}
output += line + ' \n';
});
return output.replace(/\n$/, '');
}
}
exports.default = InquirerPromptWithDescription;