wishthis/node_modules/inquirer/lib/prompts/list.js

209 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
/**
* `list` type prompt
*/
2022-04-07 07:06:43 +00:00
const chalk = require('chalk');
const figures = require('figures');
const cliCursor = require('cli-cursor');
const runAsync = require('run-async');
const { flatMap, map, take, takeUntil } = require('rxjs/operators');
const Base = require('./base');
const observe = require('../utils/events');
const Paginator = require('../utils/paginator');
const incrementListIndex = require('../utils/incrementListIndex');
2022-01-21 08:28:41 +00:00
class ListPrompt extends Base {
constructor(questions, rl, answers) {
super(questions, rl, answers);
if (!this.opt.choices) {
this.throwParamError('choices');
}
this.firstRender = true;
this.selected = 0;
2022-04-07 07:06:43 +00:00
const def = this.opt.default;
2022-01-21 08:28:41 +00:00
// If def is a Number, then use as index. Otherwise, check for value.
2022-04-07 07:06:43 +00:00
if (typeof def === 'number' && def >= 0 && def < this.opt.choices.realLength) {
2022-01-21 08:28:41 +00:00
this.selected = def;
2022-04-07 07:06:43 +00:00
} else if (typeof def !== 'number' && def != null) {
const index = this.opt.choices.realChoices.findIndex(({ value }) => value === def);
2022-01-21 08:28:41 +00:00
this.selected = Math.max(index, 0);
}
// Make sure no default is set (so it won't be printed)
this.opt.default = null;
2022-04-07 07:06:43 +00:00
const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
2022-01-21 08:28:41 +00:00
}
/**
* Start the Inquiry session
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
_run(cb) {
this.done = cb;
2022-04-07 07:06:43 +00:00
const self = this;
2022-01-21 08:28:41 +00:00
2022-04-07 07:06:43 +00:00
const events = observe(this.rl);
2022-01-21 08:28:41 +00:00
events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
events.normalizedDownKey
.pipe(takeUntil(events.line))
.forEach(this.onDownKey.bind(this));
events.numberKey.pipe(takeUntil(events.line)).forEach(this.onNumberKey.bind(this));
events.line
.pipe(
take(1),
map(this.getCurrentValue.bind(this)),
2022-04-07 07:06:43 +00:00
flatMap((value) =>
runAsync(self.opt.filter)(value, self.answers).catch((err) => err)
)
2022-01-21 08:28:41 +00:00
)
.forEach(this.onSubmit.bind(this));
// Init the prompt
cliCursor.hide();
this.render();
return this;
}
/**
* Render the prompt to screen
* @return {ListPrompt} self
*/
render() {
// Render question
2022-04-07 07:06:43 +00:00
let message = this.getQuestion();
2022-01-21 08:28:41 +00:00
if (this.firstRender) {
message += chalk.dim('(Use arrow keys)');
}
// Render choices or answer depending on the state
if (this.status === 'answered') {
message += chalk.cyan(this.opt.choices.getChoice(this.selected).short);
} else {
2022-04-07 07:06:43 +00:00
const choicesStr = listRender(this.opt.choices, this.selected);
const indexPosition = this.opt.choices.indexOf(
2022-01-21 08:28:41 +00:00
this.opt.choices.getChoice(this.selected)
);
2022-04-07 07:06:43 +00:00
const realIndexPosition =
this.opt.choices.reduce((acc, value, i) => {
// Dont count lines past the choice we are looking at
if (i > indexPosition) {
return acc;
}
// Add line if it's a separator
if (value.type === 'separator') {
return acc + 1;
}
let l = value.name;
// Non-strings take up one line
if (typeof l !== 'string') {
return acc + 1;
}
// Calculate lines taken up by string
l = l.split('\n');
return acc + l.length;
}, 0) - 1;
2022-01-21 08:28:41 +00:00
message +=
2022-04-07 07:06:43 +00:00
'\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
2022-01-21 08:28:41 +00:00
}
this.firstRender = false;
this.screen.render(message);
}
/**
* When user press `enter` key
*/
onSubmit(value) {
this.status = 'answered';
// Rerender prompt
this.render();
this.screen.done();
cliCursor.show();
this.done(value);
}
getCurrentValue() {
return this.opt.choices.getChoice(this.selected).value;
}
/**
* When user press a key
*/
onUpKey() {
2022-04-07 07:06:43 +00:00
this.selected = incrementListIndex(this.selected, 'up', this.opt);
2022-01-21 08:28:41 +00:00
this.render();
}
onDownKey() {
2022-04-07 07:06:43 +00:00
this.selected = incrementListIndex(this.selected, 'down', this.opt);
2022-01-21 08:28:41 +00:00
this.render();
}
onNumberKey(input) {
if (input <= this.opt.choices.realLength) {
this.selected = input - 1;
}
this.render();
}
}
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender(choices, pointer) {
2022-04-07 07:06:43 +00:00
let output = '';
let separatorOffset = 0;
2022-01-21 08:28:41 +00:00
choices.forEach((choice, i) => {
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
if (choice.disabled) {
separatorOffset++;
output += ' - ' + choice.name;
2022-04-07 07:06:43 +00:00
output += ` (${
typeof choice.disabled === 'string' ? choice.disabled : 'Disabled'
})`;
2022-01-21 08:28:41 +00:00
output += '\n';
return;
}
2022-04-07 07:06:43 +00:00
const isSelected = i - separatorOffset === pointer;
let line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
2022-01-21 08:28:41 +00:00
if (isSelected) {
line = chalk.cyan(line);
}
output += line + ' \n';
});
return output.replace(/\n$/, '');
}
module.exports = ListPrompt;