2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* `input` type prompt
|
|
|
|
*/
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const { map, takeUntil } = require('rxjs/operators');
|
|
|
|
const Base = require('./base');
|
|
|
|
const observe = require('../utils/events');
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
class InputPrompt extends Base {
|
|
|
|
/**
|
|
|
|
* Start the Inquiry session
|
|
|
|
* @param {Function} cb Callback when prompt is done
|
|
|
|
* @return {this}
|
|
|
|
*/
|
|
|
|
|
|
|
|
_run(cb) {
|
|
|
|
this.done = cb;
|
|
|
|
|
|
|
|
// Once user confirm (enter key)
|
2022-06-08 10:36:39 +00:00
|
|
|
const events = observe(this.rl);
|
|
|
|
const submit = events.line.pipe(map(this.filterInput.bind(this)));
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const validation = this.handleSubmitEvents(submit);
|
2022-01-21 08:28:41 +00:00
|
|
|
validation.success.forEach(this.onEnd.bind(this));
|
|
|
|
validation.error.forEach(this.onError.bind(this));
|
|
|
|
|
|
|
|
events.keypress
|
|
|
|
.pipe(takeUntil(validation.success))
|
|
|
|
.forEach(this.onKeypress.bind(this));
|
|
|
|
|
|
|
|
// Init
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the prompt to screen
|
|
|
|
* @return {InputPrompt} self
|
|
|
|
*/
|
|
|
|
|
|
|
|
render(error) {
|
2022-06-08 10:36:39 +00:00
|
|
|
let bottomContent = '';
|
|
|
|
let appendContent = '';
|
|
|
|
let message = this.getQuestion();
|
|
|
|
const { transformer } = this.opt;
|
|
|
|
const isFinal = this.status === 'answered';
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (isFinal) {
|
|
|
|
appendContent = this.answer;
|
|
|
|
} else {
|
|
|
|
appendContent = this.rl.line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transformer) {
|
|
|
|
message += transformer(appendContent, this.answers, { isFinal });
|
|
|
|
} else {
|
|
|
|
message += isFinal ? chalk.cyan(appendContent) : appendContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
bottomContent = chalk.red('>> ') + error;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.render(message, bottomContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press `enter` key
|
|
|
|
*/
|
|
|
|
|
|
|
|
filterInput(input) {
|
|
|
|
if (!input) {
|
|
|
|
return this.opt.default == null ? '' : this.opt.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnd(state) {
|
|
|
|
this.answer = state.value;
|
|
|
|
this.status = 'answered';
|
|
|
|
|
|
|
|
// Re-render prompt
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
this.screen.done();
|
|
|
|
this.done(state.value);
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
onError({ value = '', isValid }) {
|
|
|
|
this.rl.line += value;
|
|
|
|
this.rl.cursor += value.length;
|
|
|
|
this.render(isValid);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When user press a key
|
|
|
|
*/
|
|
|
|
|
|
|
|
onKeypress() {
|
2022-11-30 12:56:09 +00:00
|
|
|
this.status = 'touched';
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InputPrompt;
|