basant307/AI_Governance_Project
048
1'use strict';2 3const color = require('kleur');4 5const Prompt = require('./prompt');6 7const _require = require('../util'),8 style = _require.style,9 clear = _require.clear,10 figures = _require.figures,11 wrap = _require.wrap,12 entriesToDisplay = _require.entriesToDisplay;13 14const _require2 = require('sisteransi'),15 cursor = _require2.cursor;16/**17 * SelectPrompt Base Element18 * @param {Object} opts Options19 * @param {String} opts.message Message20 * @param {Array} opts.choices Array of choice objects21 * @param {String} [opts.hint] Hint to display22 * @param {Number} [opts.initial] Index of default value23 * @param {Stream} [opts.stdin] The Readable stream to listen to24 * @param {Stream} [opts.stdout] The Writable stream to write readline data to25 * @param {Number} [opts.optionsPerPage=10] Max options to display at once26 */27 28 29class SelectPrompt extends Prompt {30 constructor(opts = {}) {31 super(opts);32 this.msg = opts.message;33 this.hint = opts.hint || '- Use arrow-keys. Return to submit.';34 this.warn = opts.warn || '- This option is disabled';35 this.cursor = opts.initial || 0;36 this.choices = opts.choices.map((ch, idx) => {37 if (typeof ch === 'string') ch = {38 title: ch,39 value: idx40 };41 return {42 title: ch && (ch.title || ch.value || ch),43 value: ch && (ch.value === undefined ? idx : ch.value),44 description: ch && ch.description,45 selected: ch && ch.selected,46 disabled: ch && ch.disabled47 };48 });49 this.optionsPerPage = opts.optionsPerPage || 10;50 this.value = (this.choices[this.cursor] || {}).value;51 this.clear = clear('', this.out.columns);52 this.render();53 }54 55 moveCursor(n) {56 this.cursor = n;57 this.value = this.choices[n].value;58 this.fire();59 }60 61 reset() {62 this.moveCursor(0);63 this.fire();64 this.render();65 }66 67 exit() {68 this.abort();69 }70 71 abort() {72 this.done = this.aborted = true;73 this.fire();74 this.render();75 this.out.write('\n');76 this.close();77 }78 79 submit() {80 if (!this.selection.disabled) {81 this.done = true;82 this.aborted = false;83 this.fire();84 this.render();85 this.out.write('\n');86 this.close();87 } else this.bell();88 }89 90 first() {91 this.moveCursor(0);92 this.render();93 }94 95 last() {96 this.moveCursor(this.choices.length - 1);97 this.render();98 }99 100 up() {101 if (this.cursor === 0) {102 this.moveCursor(this.choices.length - 1);103 } else {104 this.moveCursor(this.cursor - 1);105 }106 107 this.render();108 }109 110 down() {111 if (this.cursor === this.choices.length - 1) {112 this.moveCursor(0);113 } else {114 this.moveCursor(this.cursor + 1);115 }116 117 this.render();118 }119 120 next() {121 this.moveCursor((this.cursor + 1) % this.choices.length);122 this.render();123 }124 125 _(c, key) {126 if (c === ' ') return this.submit();127 }128 129 get selection() {130 return this.choices[this.cursor];131 }132 133 render() {134 if (this.closed) return;135 if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText, this.out.columns));136 super.render();137 138 let _entriesToDisplay = entriesToDisplay(this.cursor, this.choices.length, this.optionsPerPage),139 startIndex = _entriesToDisplay.startIndex,140 endIndex = _entriesToDisplay.endIndex; // Print prompt141 142 143 this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(false), this.done ? this.selection.title : this.selection.disabled ? color.yellow(this.warn) : color.gray(this.hint)].join(' '); // Print choices144 145 if (!this.done) {146 this.outputText += '\n';147 148 for (let i = startIndex; i < endIndex; i++) {149 let title,150 prefix,151 desc = '',152 v = this.choices[i]; // Determine whether to display "more choices" indicators153 154 if (i === startIndex && startIndex > 0) {155 prefix = figures.arrowUp;156 } else if (i === endIndex - 1 && endIndex < this.choices.length) {157 prefix = figures.arrowDown;158 } else {159 prefix = ' ';160 }161 162 if (v.disabled) {163 title = this.cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);164 prefix = (this.cursor === i ? color.bold().gray(figures.pointer) + ' ' : ' ') + prefix;165 } else {166 title = this.cursor === i ? color.cyan().underline(v.title) : v.title;167 prefix = (this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ') + prefix;168 169 if (v.description && this.cursor === i) {170 desc = ` - ${v.description}`;171 172 if (prefix.length + title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {173 desc = '\n' + wrap(v.description, {174 margin: 3,175 width: this.out.columns176 });177 }178 }179 }180 181 this.outputText += `${prefix} ${title}${color.gray(desc)}\n`;182 }183 }184 185 this.out.write(this.outputText);186 }187 188}189 190module.exports = SelectPrompt;