| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const color = require('kleur');
|
|---|
| 4 |
|
|---|
| 5 | const _require = require('sisteransi'),
|
|---|
| 6 | cursor = _require.cursor;
|
|---|
| 7 |
|
|---|
| 8 | const MultiselectPrompt = require('./multiselect');
|
|---|
| 9 |
|
|---|
| 10 | const _require2 = require('../util'),
|
|---|
| 11 | clear = _require2.clear,
|
|---|
| 12 | style = _require2.style,
|
|---|
| 13 | figures = _require2.figures;
|
|---|
| 14 | /**
|
|---|
| 15 | * MultiselectPrompt Base Element
|
|---|
| 16 | * @param {Object} opts Options
|
|---|
| 17 | * @param {String} opts.message Message
|
|---|
| 18 | * @param {Array} opts.choices Array of choice objects
|
|---|
| 19 | * @param {String} [opts.hint] Hint to display
|
|---|
| 20 | * @param {String} [opts.warn] Hint shown for disabled choices
|
|---|
| 21 | * @param {Number} [opts.max] Max choices
|
|---|
| 22 | * @param {Number} [opts.cursor=0] Cursor start position
|
|---|
| 23 | * @param {Stream} [opts.stdin] The Readable stream to listen to
|
|---|
| 24 | * @param {Stream} [opts.stdout] The Writable stream to write readline data to
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | class AutocompleteMultiselectPrompt extends MultiselectPrompt {
|
|---|
| 29 | constructor(opts = {}) {
|
|---|
| 30 | opts.overrideRender = true;
|
|---|
| 31 | super(opts);
|
|---|
| 32 | this.inputValue = '';
|
|---|
| 33 | this.clear = clear('', this.out.columns);
|
|---|
| 34 | this.filteredOptions = this.value;
|
|---|
| 35 | this.render();
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | last() {
|
|---|
| 39 | this.cursor = this.filteredOptions.length - 1;
|
|---|
| 40 | this.render();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | next() {
|
|---|
| 44 | this.cursor = (this.cursor + 1) % this.filteredOptions.length;
|
|---|
| 45 | this.render();
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | up() {
|
|---|
| 49 | if (this.cursor === 0) {
|
|---|
| 50 | this.cursor = this.filteredOptions.length - 1;
|
|---|
| 51 | } else {
|
|---|
| 52 | this.cursor--;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | this.render();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | down() {
|
|---|
| 59 | if (this.cursor === this.filteredOptions.length - 1) {
|
|---|
| 60 | this.cursor = 0;
|
|---|
| 61 | } else {
|
|---|
| 62 | this.cursor++;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | this.render();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | left() {
|
|---|
| 69 | this.filteredOptions[this.cursor].selected = false;
|
|---|
| 70 | this.render();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | right() {
|
|---|
| 74 | if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
|
|---|
| 75 | this.filteredOptions[this.cursor].selected = true;
|
|---|
| 76 | this.render();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | delete() {
|
|---|
| 80 | if (this.inputValue.length) {
|
|---|
| 81 | this.inputValue = this.inputValue.substr(0, this.inputValue.length - 1);
|
|---|
| 82 | this.updateFilteredOptions();
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | updateFilteredOptions() {
|
|---|
| 87 | const currentHighlight = this.filteredOptions[this.cursor];
|
|---|
| 88 | this.filteredOptions = this.value.filter(v => {
|
|---|
| 89 | if (this.inputValue) {
|
|---|
| 90 | if (typeof v.title === 'string') {
|
|---|
| 91 | if (v.title.toLowerCase().includes(this.inputValue.toLowerCase())) {
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (typeof v.value === 'string') {
|
|---|
| 97 | if (v.value.toLowerCase().includes(this.inputValue.toLowerCase())) {
|
|---|
| 98 | return true;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | return false;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return true;
|
|---|
| 106 | });
|
|---|
| 107 | const newHighlightIndex = this.filteredOptions.findIndex(v => v === currentHighlight);
|
|---|
| 108 | this.cursor = newHighlightIndex < 0 ? 0 : newHighlightIndex;
|
|---|
| 109 | this.render();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | handleSpaceToggle() {
|
|---|
| 113 | const v = this.filteredOptions[this.cursor];
|
|---|
| 114 |
|
|---|
| 115 | if (v.selected) {
|
|---|
| 116 | v.selected = false;
|
|---|
| 117 | this.render();
|
|---|
| 118 | } else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
|
|---|
| 119 | return this.bell();
|
|---|
| 120 | } else {
|
|---|
| 121 | v.selected = true;
|
|---|
| 122 | this.render();
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | handleInputChange(c) {
|
|---|
| 127 | this.inputValue = this.inputValue + c;
|
|---|
| 128 | this.updateFilteredOptions();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | _(c, key) {
|
|---|
| 132 | if (c === ' ') {
|
|---|
| 133 | this.handleSpaceToggle();
|
|---|
| 134 | } else {
|
|---|
| 135 | this.handleInputChange(c);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | renderInstructions() {
|
|---|
| 140 | if (this.instructions === undefined || this.instructions) {
|
|---|
| 141 | if (typeof this.instructions === 'string') {
|
|---|
| 142 | return this.instructions;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | return `
|
|---|
| 146 | Instructions:
|
|---|
| 147 | ${figures.arrowUp}/${figures.arrowDown}: Highlight option
|
|---|
| 148 | ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
|
|---|
| 149 | [a,b,c]/delete: Filter choices
|
|---|
| 150 | enter/return: Complete answer
|
|---|
| 151 | `;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | return '';
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | renderCurrentInput() {
|
|---|
| 158 | return `
|
|---|
| 159 | Filtered results for: ${this.inputValue ? this.inputValue : color.gray('Enter something to filter')}\n`;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | renderOption(cursor, v, i) {
|
|---|
| 163 | let title;
|
|---|
| 164 | if (v.disabled) title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);else title = cursor === i ? color.cyan().underline(v.title) : v.title;
|
|---|
| 165 | return (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ' + title;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | renderDoneOrInstructions() {
|
|---|
| 169 | if (this.done) {
|
|---|
| 170 | return this.value.filter(e => e.selected).map(v => v.title).join(', ');
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | const output = [color.gray(this.hint), this.renderInstructions(), this.renderCurrentInput()];
|
|---|
| 174 |
|
|---|
| 175 | if (this.filteredOptions.length && this.filteredOptions[this.cursor].disabled) {
|
|---|
| 176 | output.push(color.yellow(this.warn));
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | return output.join(' ');
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | render() {
|
|---|
| 183 | if (this.closed) return;
|
|---|
| 184 | if (this.firstRender) this.out.write(cursor.hide);
|
|---|
| 185 | super.render(); // print prompt
|
|---|
| 186 |
|
|---|
| 187 | let prompt = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(false), this.renderDoneOrInstructions()].join(' ');
|
|---|
| 188 |
|
|---|
| 189 | if (this.showMinError) {
|
|---|
| 190 | prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);
|
|---|
| 191 | this.showMinError = false;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | prompt += this.renderOptions(this.filteredOptions);
|
|---|
| 195 | this.out.write(this.clear + prompt);
|
|---|
| 196 | this.clear = clear(prompt, this.out.columns);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | module.exports = AutocompleteMultiselectPrompt; |
|---|