input.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * `input` type prompt
  3. */
  4. import chalk from 'chalk';
  5. import { map, takeUntil } from 'rxjs';
  6. import Base from './base.js';
  7. import observe from '../utils/events.js';
  8. export default class InputPrompt extends Base {
  9. /**
  10. * Start the Inquiry session
  11. * @param {Function} cb Callback when prompt is done
  12. * @return {this}
  13. */
  14. _run(cb) {
  15. this.done = cb;
  16. // Once user confirm (enter key)
  17. const events = observe(this.rl);
  18. const submit = events.line.pipe(map(this.filterInput.bind(this)));
  19. const validation = this.handleSubmitEvents(submit);
  20. validation.success.forEach(this.onEnd.bind(this));
  21. validation.error.forEach(this.onError.bind(this));
  22. events.keypress
  23. .pipe(takeUntil(validation.success))
  24. .forEach(this.onKeypress.bind(this));
  25. // Init
  26. this.render();
  27. return this;
  28. }
  29. /**
  30. * Render the prompt to screen
  31. * @return {InputPrompt} self
  32. */
  33. render(error) {
  34. let bottomContent = '';
  35. let appendContent = '';
  36. let message = this.getQuestion();
  37. const { transformer } = this.opt;
  38. const isFinal = this.status === 'answered';
  39. if (isFinal) {
  40. appendContent = this.answer;
  41. } else {
  42. appendContent = this.rl.line;
  43. }
  44. if (transformer) {
  45. message += transformer(appendContent, this.answers, { isFinal });
  46. } else {
  47. message += isFinal ? chalk.cyan(appendContent) : appendContent;
  48. }
  49. if (error) {
  50. bottomContent = chalk.red('>> ') + error;
  51. }
  52. this.screen.render(message, bottomContent);
  53. }
  54. /**
  55. * When user press `enter` key
  56. */
  57. filterInput(input) {
  58. if (!input) {
  59. return this.opt.default == null ? '' : this.opt.default;
  60. }
  61. return input;
  62. }
  63. onEnd(state) {
  64. this.answer = state.value;
  65. this.status = 'answered';
  66. // Re-render prompt
  67. this.render();
  68. this.screen.done();
  69. this.done(state.value);
  70. }
  71. onError({ value = '', isValid }) {
  72. this.rl.line += value;
  73. this.rl.cursor += value.length;
  74. this.render(isValid);
  75. }
  76. /**
  77. * When user press a key
  78. */
  79. onKeypress() {
  80. this.status = 'touched';
  81. this.render();
  82. }
  83. }