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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. events.keypress
  32. .pipe(takeUntil(validation.success))
  33. .forEach(this.onKeypress.bind(this));
  34. // Init
  35. this.render();
  36. return this;
  37. }
  38. /**
  39. * Render the prompt to screen
  40. * @return {PasswordPrompt} self
  41. */
  42. render(error) {
  43. var message = this.getQuestion();
  44. var bottomContent = '';
  45. if (this.status === 'answered') {
  46. message += this.opt.mask
  47. ? chalk.cyan(mask(this.answer, this.opt.mask))
  48. : chalk.italic.dim('[hidden]');
  49. } else if (this.opt.mask) {
  50. message += mask(this.rl.line || '', this.opt.mask);
  51. } else {
  52. message += chalk.italic.dim('[input is hidden] ');
  53. }
  54. if (error) {
  55. bottomContent = '\n' + chalk.red('>> ') + error;
  56. }
  57. this.screen.render(message, bottomContent);
  58. }
  59. /**
  60. * When user press `enter` key
  61. */
  62. filterInput(input) {
  63. if (!input) {
  64. return this.opt.default == null ? '' : this.opt.default;
  65. }
  66. return input;
  67. }
  68. onEnd(state) {
  69. this.status = 'answered';
  70. this.answer = state.value;
  71. // Re-render prompt
  72. this.render();
  73. this.screen.done();
  74. this.done(state.value);
  75. }
  76. onError(state) {
  77. this.render(state.isValid);
  78. }
  79. onKeypress() {
  80. this.render();
  81. }
  82. }
  83. module.exports = PasswordPrompt;