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.

password.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. /**
  3. * `password` type prompt
  4. */
  5. var chalk = require('chalk');
  6. var { map, takeUntil } = require('rxjs/operators');
  7. var Base = require('./base');
  8. var observe = require('../utils/events');
  9. function mask(input, maskChar) {
  10. input = String(input);
  11. maskChar = typeof maskChar === 'string' ? maskChar : '*';
  12. if (input.length === 0) {
  13. return '';
  14. }
  15. return new Array(input.length + 1).join(maskChar);
  16. }
  17. class PasswordPrompt extends Base {
  18. /**
  19. * Start the Inquiry session
  20. * @param {Function} cb Callback when prompt is done
  21. * @return {this}
  22. */
  23. _run(cb) {
  24. this.done = cb;
  25. var events = observe(this.rl);
  26. // Once user confirm (enter key)
  27. var submit = events.line.pipe(map(this.filterInput.bind(this)));
  28. var validation = this.handleSubmitEvents(submit);
  29. validation.success.forEach(this.onEnd.bind(this));
  30. validation.error.forEach(this.onError.bind(this));
  31. if (this.opt.mask) {
  32. events.keypress
  33. .pipe(takeUntil(validation.success))
  34. .forEach(this.onKeypress.bind(this));
  35. }
  36. // Init
  37. this.render();
  38. return this;
  39. }
  40. /**
  41. * Render the prompt to screen
  42. * @return {PasswordPrompt} self
  43. */
  44. render(error) {
  45. var message = this.getQuestion();
  46. var bottomContent = '';
  47. if (this.status === 'answered') {
  48. message += this.opt.mask
  49. ? chalk.cyan(mask(this.answer, this.opt.mask))
  50. : chalk.italic.dim('[hidden]');
  51. } else if (this.opt.mask) {
  52. message += mask(this.rl.line || '', this.opt.mask);
  53. } else {
  54. message += chalk.italic.dim('[input is hidden] ');
  55. }
  56. if (error) {
  57. bottomContent = '\n' + chalk.red('>> ') + error;
  58. }
  59. this.screen.render(message, bottomContent);
  60. }
  61. /**
  62. * When user press `enter` key
  63. */
  64. filterInput(input) {
  65. if (!input) {
  66. return this.opt.default == null ? '' : this.opt.default;
  67. }
  68. return input;
  69. }
  70. onEnd(state) {
  71. this.status = 'answered';
  72. this.answer = state.value;
  73. // Re-render prompt
  74. this.render();
  75. this.screen.done();
  76. this.done(state.value);
  77. }
  78. onError(state) {
  79. this.render(state.isValid);
  80. }
  81. onKeypress() {
  82. this.render();
  83. }
  84. }
  85. module.exports = PasswordPrompt;