1
0
mirror of https://github.com/actions/labeler synced 2026-05-11 00:51:02 +02:00
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

@@ -9,13 +9,15 @@ const color = require('kleur');
const Prompt = require('./prompt');
const _require = require('sisteransi'),
erase = _require.erase,
cursor = _require.cursor;
const _require2 = require('../util'),
style = _require2.style,
clear = _require2.clear,
figures = _require2.figures,
strip = _require2.strip;
wrap = _require2.wrap,
entriesToDisplay = _require2.entriesToDisplay;
const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
@@ -50,9 +52,11 @@ class AutocompletePrompt extends Prompt {
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.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;
@@ -65,15 +69,23 @@ 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();
}
@@ -87,25 +99,10 @@ 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;
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));
@@ -141,7 +138,6 @@ class AutocompletePrompt extends Prompt {
}
_(c, key) {
// TODO on ctrl+# go to page #
let s1 = this.input.slice(0, this.cursor);
let s2 = this.input.slice(this.cursor);
this.input = `${s1}${c}${s2}`;
@@ -175,7 +171,7 @@ class AutocompletePrompt extends Prompt {
}
last() {
this.moveSelect(this.suggestions[this.page].length - 1);
this.moveSelect(this.suggestions.length - 1);
this.render();
}
@@ -186,14 +182,13 @@ 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);
@@ -201,16 +196,12 @@ class AutocompletePrompt extends Prompt {
}
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();
}
@@ -226,49 +217,43 @@ 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;
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 (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 (v.description) {
desc = ` - ${v.description}`;
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++;
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
});
}
}
this.out.write(this.clear + prompt);
this.clear = clear(prompt);
return prefix + ' ' + title + color.gray(desc || '');
}
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);
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText));
super.render();
let _entriesToDisplay = entriesToDisplay(this.select, this.choices.length, this.limit),
startIndex = _entriesToDisplay.startIndex,
endIndex = _entriesToDisplay.endIndex;
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);
}
}