This commit is contained in:
David Kale
2020-09-08 13:25:36 -04:00
parent e4246d2b5b
commit 91fcbb0108
4227 changed files with 416837 additions and 457884 deletions

View File

@@ -2,8 +2,8 @@
const color = require('kleur');
const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style, clear, figures, strip } = require('../util');
const { erase, cursor } = require('sisteransi');
const { style, clear, figures, wrap, entriesToDisplay } = require('../util');
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]);
@@ -37,13 +37,9 @@ class AutocompletePrompt extends Prompt {
? opts.initial
: getIndex(opts.choices, opts.initial);
this.select = this.initial || opts.cursor || 0;
this.fallback = opts.fallback || (
opts.initial !== undefined ?
`${figures.pointerSmall} ${getTitle(this.choices, this.initial)}` :
`${figures.pointerSmall} ${opts.noMatches || 'no matches found'}`
);
this.suggestions = [[]];
this.page = 0;
this.i18n = { noMatches: opts.noMatches || 'no matches found' };
this.fallback = opts.fallback || this.initial;
this.suggestions = [];
this.input = '';
this.limit = opts.limit || 10;
this.cursor = 0;
@@ -56,15 +52,24 @@ class AutocompletePrompt extends Prompt {
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;
if (this.suggestions[this.page].length > 0) {
this.value = getVal(this.suggestions[this.page], i);
} else {
this.value = this.initial !== undefined
? getVal(this.choices, this.initial)
: null;
}
if (this.suggestions.length > 0)
this.value = getVal(this.suggestions, i);
else this.value = this.fallback.value;
this.fire();
}
@@ -74,26 +79,8 @@ class AutocompletePrompt extends Prompt {
if (this.completing !== p) return;
this.suggestions = suggestions
.map((s, i, arr) => ({title: getTitle(arr, i), value: getVal(arr, i)}))
.reduce((arr, sug) => {
if (arr[arr.length - 1].length < this.limit)
arr[arr.length - 1].push(sug);
else arr.push([sug]);
return arr;
}, [[]]);
this.isFallback = false;
.map((s, i, arr) => ({ title: getTitle(arr, i), value: getVal(arr, i), description: s.description }));
this.completing = false;
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;
}
const l = Math.max(suggestions.length - 1, 0);
this.moveSelect(Math.min(l, this.select));
@@ -126,7 +113,7 @@ class AutocompletePrompt extends Prompt {
this.close();
}
_(c, key) { // TODO on ctrl+# go to page #
_(c, key) {
let s1 = this.input.slice(0, this.cursor);
let s2 = this.input.slice(this.cursor);
this.input = `${s1}${c}${s2}`;
@@ -146,12 +133,12 @@ class AutocompletePrompt extends Prompt {
}
deleteForward() {
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();
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();
}
first() {
@@ -160,7 +147,7 @@ class AutocompletePrompt extends Prompt {
}
last() {
this.moveSelect(this.suggestions[this.page].length - 1);
this.moveSelect(this.suggestions.length - 1);
this.render();
}
@@ -171,32 +158,25 @@ class AutocompletePrompt extends Prompt {
}
down() {
if (this.select >= this.suggestions[this.page].length - 1) return this.bell();
if (this.select >= this.suggestions.length - 1) return this.bell();
this.moveSelect(this.select + 1);
this.render();
}
next() {
if (this.select === this.suggestions[this.page].length - 1) {
this.page = (this.page + 1) % this.suggestions.length;
if (this.select === this.suggestions.length - 1) {
this.moveSelect(0);
} else this.moveSelect(this.select + 1);
this.render();
}
nextPage() {
if (this.page >= this.suggestions.length - 1)
return this.bell();
this.page++;
this.moveSelect(0);
this.moveSelect(Math.min(this.select + this.limit, this.suggestions.length - 1));
this.render();
}
prevPage() {
if (this.page <= 0)
return this.bell();
this.page--;
this.moveSelect(0);
this.moveSelect(Math.max(this.select - this.limit, 0));
this.render();
}
@@ -212,52 +192,50 @@ class AutocompletePrompt extends Prompt {
this.render();
}
render() {
if (this.closed) return;
super.render();
if (this.lineCount) this.out.write(cursor.down(this.lineCount));
let prompt = color.bold(`${style.symbol(this.done, this.aborted)} ${this.msg} `)
+ `${style.delimiter(this.completing)} `;
let length = strip(prompt).length;
if (this.done && this.suggestions[this.page][this.select]) {
prompt += `${this.suggestions[this.page][this.select].title}`;
} else {
this.rendered = `${this.transform.render(this.input)}`;
length += this.rendered.length;
prompt += this.rendered;
}
if (!this.done) {
this.lineCount = this.suggestions[this.page].length;
let suggestions = this.suggestions[this.page].reduce((acc, item, i) =>
acc + `\n${i === this.select ? color.cyan(item.title) : item.title}`, '');
if (suggestions && !this.isFallback) {
prompt += suggestions;
if (this.suggestions.length > 1) {
this.lineCount++;
prompt += color.blue(`\nPage ${this.page+1}/${this.suggestions.length}`);
}
} else {
const fallbackIndex = getIndex(this.choices, this.fallback);
const fallbackTitle = fallbackIndex !== undefined
? getTitle(this.choices, fallbackIndex)
: this.fallback;
prompt += `\n${color.gray(fallbackTitle)}`;
this.lineCount++;
renderOption(v, hovered, isStart, isEnd) {
let desc;
let prefix = isStart ? figures.arrowUp : isEnd ? figures.arrowDown : ' ';
let title = hovered ? color.cyan().underline(v.title) : v.title;
prefix = (hovered ? color.cyan(figures.pointer) + ' ' : ' ') + prefix;
if (v.description) {
desc = ` - ${v.description}`;
if (prefix.length + title.length + desc.length >= this.out.columns
|| v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, { margin: 3, width: this.out.columns })
}
}
return prefix + ' ' + title + color.gray(desc || '');
}
this.out.write(this.clear + prompt);
this.clear = clear(prompt);
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
super.render();
if (this.lineCount && !this.done) {
let pos = cursor.up(this.lineCount);
pos += cursor.left+cursor.to(length);
pos += cursor.move(-this.rendered.length+this.cursor*this.scale);
this.out.write(pos);
let { startIndex, endIndex } = entriesToDisplay(this.select, this.choices.length, this.limit);
this.outputText = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.completing),
this.done && this.suggestions[this.select]
? this.suggestions[this.select].title
: this.rendered = this.transform.render(this.input)
].join(' ');
if (!this.done) {
const suggestions = this.suggestions
.slice(startIndex, endIndex)
.map((item, i) => this.renderOption(item,
this.select === i + startIndex,
i === 0 && startIndex > 0,
i + startIndex === endIndex - 1 && endIndex < this.choices.length))
.join('\n');
this.outputText += `\n` + (suggestions || color.gray(this.fallback.title));
}
this.out.write(erase.line + cursor.to(0) + this.outputText);
}
}