Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

confirm.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. /**
  3. * `confirm` type prompt
  4. */
  5. var _ = require('lodash');
  6. var chalk = require('chalk');
  7. var { take, takeUntil } = require('rxjs/operators');
  8. var Base = require('./base');
  9. var observe = require('../utils/events');
  10. class ConfirmPrompt extends Base {
  11. constructor(questions, rl, answers) {
  12. super(questions, rl, answers);
  13. var rawDefault = true;
  14. _.extend(this.opt, {
  15. filter: function(input) {
  16. var value = rawDefault;
  17. if (input != null && input !== '') {
  18. value = /^y(es)?/i.test(input);
  19. }
  20. return value;
  21. }
  22. });
  23. if (_.isBoolean(this.opt.default)) {
  24. rawDefault = this.opt.default;
  25. }
  26. this.opt.default = rawDefault ? 'Y/n' : 'y/N';
  27. return this;
  28. }
  29. /**
  30. * Start the Inquiry session
  31. * @param {Function} cb Callback when prompt is done
  32. * @return {this}
  33. */
  34. _run(cb) {
  35. this.done = cb;
  36. // Once user confirm (enter key)
  37. var events = observe(this.rl);
  38. events.keypress.pipe(takeUntil(events.line)).forEach(this.onKeypress.bind(this));
  39. events.line.pipe(take(1)).forEach(this.onEnd.bind(this));
  40. // Init
  41. this.render();
  42. return this;
  43. }
  44. /**
  45. * Render the prompt to screen
  46. * @return {ConfirmPrompt} self
  47. */
  48. render(answer) {
  49. var message = this.getQuestion();
  50. if (typeof answer === 'boolean') {
  51. message += chalk.cyan(answer ? 'Yes' : 'No');
  52. } else {
  53. message += this.rl.line;
  54. }
  55. this.screen.render(message);
  56. return this;
  57. }
  58. /**
  59. * When user press `enter` key
  60. */
  61. onEnd(input) {
  62. this.status = 'answered';
  63. var output = this.opt.filter(input);
  64. this.render(output);
  65. this.screen.done();
  66. this.done(output);
  67. }
  68. /**
  69. * When user press a key
  70. */
  71. onKeypress() {
  72. this.render();
  73. }
  74. }
  75. module.exports = ConfirmPrompt;