2020-03-31 20:42:07 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const color = require('kleur');
|
|
|
|
const Prompt = require('./prompt');
|
|
|
|
const { erase, cursor } = require('sisteransi');
|
2020-03-31 20:57:48 +08:00
|
|
|
const { style, clear, wrap } = require('../util');
|
2020-03-31 20:42:07 +08:00
|
|
|
|
|
|
|
const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
|
|
|
|
const getTitle = (arr, i) => arr[i] && (arr[i].title || arr[i].value || arr[i]);
|
|
|
|
const getIndex = (arr, valOrTitle) => {
|
|
|
|
const index = arr.findIndex(el => el.value === valOrTitle || el.title === valOrTitle);
|
|
|
|
return index > -1 ? index : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TextPrompt Base Element
|
|
|
|
* @param {Object} opts Options
|
|
|
|
* @param {String} opts.message Message
|
|
|
|
* @param {Array} opts.choices Array of auto-complete choices objects
|
|
|
|
* @param {Function} [opts.suggest] Filter function. Defaults to sort by title
|
|
|
|
* @param {Number} [opts.limit=10] Max number of results to show
|
|
|
|
* @param {Number} [opts.cursor=0] Cursor start position
|
|
|
|
* @param {String} [opts.style='default'] Render style
|
|
|
|
* @param {String} [opts.fallback] Fallback message - initial to default value
|
|
|
|
* @param {String} [opts.initial] Index of the default value
|
|
|
|
* @param {Stream} [opts.stdin] The Readable stream to listen to
|
|
|
|
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
|
|
|
|
* @param {String} [opts.noMatches] The no matches found label
|
|
|
|
*/
|
|
|
|
class AutocompletePrompt extends Prompt {
|
|
|
|
constructor(opts={}) {
|
|
|
|
super(opts);
|
|
|
|
this.msg = opts.message;
|
|
|
|
this.suggest = opts.suggest;
|
|
|
|
this.choices = opts.choices;
|
|
|
|
this.initial = typeof opts.initial === 'number'
|
|
|
|
? opts.initial
|
|
|
|
: getIndex(opts.choices, opts.initial);
|
|
|
|
this.select = this.initial || opts.cursor || 0;
|
|
|
|
this.i18n = { noMatches: opts.noMatches || 'no matches found' };
|
|
|
|
this.fallback = opts.fallback || this.initial;
|
2020-03-31 20:57:48 +08:00
|
|
|
this.suggestions = [[]];
|
|
|
|
this.page = 0;
|
2020-03-31 20:42:07 +08:00
|
|
|
this.input = '';
|
|
|
|
this.limit = opts.limit || 10;
|
|
|
|
this.cursor = 0;
|
|
|
|
this.transform = style.render(opts.style);
|
|
|
|
this.scale = this.transform.scale;
|
|
|
|
this.render = this.render.bind(this);
|
|
|
|
this.complete = this.complete.bind(this);
|
|
|
|
this.clear = clear('');
|
|
|
|
this.complete(this.render);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
set fallback(fb) {
|
|
|
|
this._fb = Number.isSafeInteger(parseInt(fb)) ? parseInt(fb) : fb;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fallback() {
|
|
|
|
let choice;
|
|
|
|
if (typeof this._fb === 'number')
|
|
|
|
choice = this.choices[this._fb];
|
|
|
|
else if (typeof this._fb === 'string')
|
|
|
|
choice = { title: this._fb };
|
|
|
|
return choice || this._fb || { title: this.i18n.noMatches };
|
|
|
|
}
|
|
|
|
|
|
|
|
moveSelect(i) {
|
|
|
|
this.select = i;
|
2020-03-31 20:57:48 +08:00
|
|
|
if (this.suggestions[this.page].length > 0)
|
|
|
|
this.value = getVal(this.suggestions[this.page], i);
|
2020-03-31 20:42:07 +08:00
|
|
|
else this.value = this.fallback.value;
|
|
|
|
this.fire();
|
|
|
|
}
|
|
|
|
|
|
|
|
async complete(cb) {
|
|
|
|
const p = (this.completing = this.suggest(this.input, this.choices));
|
|
|
|
const suggestions = await p;
|
|
|
|
|
|
|
|
if (this.completing !== p) return;
|
|
|
|
this.suggestions = suggestions
|
2020-03-31 20:57:48 +08:00
|
|
|
.map((s, i, arr) => ({ title: getTitle(arr, i), value: getVal(arr, i), description: s.description }))
|
|
|
|
.reduce((arr, sug) => {
|
|
|
|
if (arr[arr.length - 1].length < this.limit)
|
|
|
|
arr[arr.length - 1].push(sug);
|
|
|
|
else arr.push([sug]);
|
|
|
|
return arr;
|
|
|
|
}, [[]]);
|
2020-03-31 20:42:07 +08:00
|
|
|
this.completing = false;
|
2020-03-31 20:57:48 +08:00
|
|
|
if (!this.suggestions[this.page])
|
|
|
|
this.page = 0;
|
|
|
|
|
|
|
|
// if (!this.suggestions.length && this.fallback) {
|
|
|
|
// const index = getIndex(this.choices, this.fallback);
|
|
|
|
// this.suggestions = [[]];
|
|
|
|
// if (index !== undefined)
|
|
|
|
// this.suggestions[0].push({ title: getTitle(this.choices, index), value: getVal(this.choices, index) });
|
|
|
|
// this.isFallback = true;
|
|
|
|
// }
|
|
|
|
|
2020-03-31 20:42:07 +08:00
|
|
|
const l = Math.max(suggestions.length - 1, 0);
|
|
|
|
this.moveSelect(Math.min(l, this.select));
|
|
|
|
|
|
|
|
cb && cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.input = '';
|
|
|
|
this.complete(() => {
|
|
|
|
this.moveSelect(this.initial !== void 0 ? this.initial : 0);
|
|
|
|
this.render();
|
|
|
|
});
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this.done = this.aborted = true;
|
|
|
|
this.fire();
|
|
|
|
this.render();
|
|
|
|
this.out.write('\n');
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
submit() {
|
|
|
|
this.done = true;
|
|
|
|
this.aborted = false;
|
|
|
|
this.fire();
|
|
|
|
this.render();
|
|
|
|
this.out.write('\n');
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
_(c, key) { // TODO on ctrl+# go to page #
|
2020-03-31 20:42:07 +08:00
|
|
|
let s1 = this.input.slice(0, this.cursor);
|
|
|
|
let s2 = this.input.slice(this.cursor);
|
|
|
|
this.input = `${s1}${c}${s2}`;
|
|
|
|
this.cursor = s1.length+1;
|
|
|
|
this.complete(this.render);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete() {
|
|
|
|
if (this.cursor === 0) return this.bell();
|
|
|
|
let s1 = this.input.slice(0, this.cursor-1);
|
|
|
|
let s2 = this.input.slice(this.cursor);
|
|
|
|
this.input = `${s1}${s2}`;
|
|
|
|
this.complete(this.render);
|
|
|
|
this.cursor = this.cursor-1;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteForward() {
|
2020-03-31 20:57:48 +08:00
|
|
|
if(this.cursor*this.scale >= this.rendered.length) return this.bell();
|
|
|
|
let s1 = this.input.slice(0, this.cursor);
|
|
|
|
let s2 = this.input.slice(this.cursor+1);
|
|
|
|
this.input = `${s1}${s2}`;
|
|
|
|
this.complete(this.render);
|
|
|
|
this.render();
|
2020-03-31 20:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
first() {
|
|
|
|
this.moveSelect(0);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
last() {
|
2020-03-31 20:57:48 +08:00
|
|
|
this.moveSelect(this.suggestions[this.page].length - 1);
|
2020-03-31 20:42:07 +08:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
up() {
|
|
|
|
if (this.select <= 0) return this.bell();
|
|
|
|
this.moveSelect(this.select - 1);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
down() {
|
2020-03-31 20:57:48 +08:00
|
|
|
if (this.select >= this.suggestions[this.page].length - 1) return this.bell();
|
2020-03-31 20:42:07 +08:00
|
|
|
this.moveSelect(this.select + 1);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
next() {
|
2020-03-31 20:57:48 +08:00
|
|
|
if (this.select === this.suggestions[this.page].length - 1) {
|
|
|
|
this.page = (this.page + 1) % this.suggestions.length;
|
2020-03-31 20:42:07 +08:00
|
|
|
this.moveSelect(0);
|
|
|
|
} else this.moveSelect(this.select + 1);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage() {
|
2020-03-31 20:57:48 +08:00
|
|
|
if (this.page >= this.suggestions.length - 1)
|
|
|
|
return this.bell();
|
|
|
|
this.page++;
|
|
|
|
this.moveSelect(0);
|
2020-03-31 20:42:07 +08:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage() {
|
2020-03-31 20:57:48 +08:00
|
|
|
if (this.page <= 0)
|
|
|
|
return this.bell();
|
|
|
|
this.page--;
|
|
|
|
this.moveSelect(0);
|
2020-03-31 20:42:07 +08:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
left() {
|
|
|
|
if (this.cursor <= 0) return this.bell();
|
|
|
|
this.cursor = this.cursor-1;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
right() {
|
|
|
|
if (this.cursor*this.scale >= this.rendered.length) return this.bell();
|
|
|
|
this.cursor = this.cursor+1;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
renderOption(v, hovered) {
|
|
|
|
let desc, title = v.title;
|
|
|
|
if (hovered) {
|
|
|
|
title = color.cyan(v.title);
|
|
|
|
if (v.description) {
|
|
|
|
desc = ` - ${v.description}`;
|
|
|
|
if (title.length + desc.length >= this.out.columns
|
|
|
|
|| v.description.split(/\r?\n/).length > 1) {
|
|
|
|
desc = '\n' + wrap(v.description, { width: this.out.columns })
|
|
|
|
}
|
2020-03-31 20:42:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 20:57:48 +08:00
|
|
|
return title + color.gray(desc || '');
|
2020-03-31 20:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.closed) return;
|
2020-03-31 20:57:48 +08:00
|
|
|
if (!this.firstRender) this.out.write(clear(this.outputText));
|
2020-03-31 20:42:07 +08:00
|
|
|
super.render();
|
|
|
|
|
|
|
|
this.outputText = [
|
2020-03-31 20:57:48 +08:00
|
|
|
color.bold(style.symbol(this.done, this.aborted)),
|
2020-03-31 20:42:07 +08:00
|
|
|
color.bold(this.msg),
|
|
|
|
style.delimiter(this.completing),
|
2020-03-31 20:57:48 +08:00
|
|
|
this.done && this.suggestions[this.page][this.select]
|
|
|
|
? this.suggestions[this.page][this.select].title
|
|
|
|
: this.rendered = this.transform.render(this.input)
|
2020-03-31 20:42:07 +08:00
|
|
|
].join(' ');
|
|
|
|
|
|
|
|
if (!this.done) {
|
2020-03-31 20:57:48 +08:00
|
|
|
const suggestions = this.suggestions[this.page]
|
|
|
|
.map((item, i) => this.renderOption(item, this.select === i))
|
2020-03-31 20:42:07 +08:00
|
|
|
.join('\n');
|
|
|
|
this.outputText += `\n` + (suggestions || color.gray(this.fallback.title));
|
2020-03-31 20:57:48 +08:00
|
|
|
|
|
|
|
if (this.suggestions[this.page].length > 1) {
|
|
|
|
this.outputText += color.blue(`\nPage ${this.page+1}/${this.suggestions.length}`);
|
|
|
|
}
|
2020-03-31 20:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.out.write(erase.line + cursor.to(0) + this.outputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AutocompletePrompt;
|