UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * `editor` type prompt
  3. */
  4. var util = require('util');
  5. var chalk = require('chalk');
  6. var ExternalEditor = require('external-editor');
  7. var Base = require('./base');
  8. var observe = require('../utils/events');
  9. var rx = require('rx-lite-aggregates');
  10. /**
  11. * Module exports
  12. */
  13. module.exports = Prompt;
  14. /**
  15. * Constructor
  16. */
  17. function Prompt() {
  18. return Base.apply(this, arguments);
  19. }
  20. util.inherits(Prompt, Base);
  21. /**
  22. * Start the Inquiry session
  23. * @param {Function} cb Callback when prompt is done
  24. * @return {this}
  25. */
  26. Prompt.prototype._run = function (cb) {
  27. this.done = cb;
  28. this.editorResult = new rx.Subject();
  29. // Open Editor on "line" (Enter Key)
  30. var events = observe(this.rl);
  31. this.lineSubscription = events.line.forEach(this.startExternalEditor.bind(this));
  32. // Trigger Validation when editor closes
  33. var validation = this.handleSubmitEvents(this.editorResult);
  34. validation.success.forEach(this.onEnd.bind(this));
  35. validation.error.forEach(this.onError.bind(this));
  36. // Prevents default from being printed on screen (can look weird with multiple lines)
  37. this.currentText = this.opt.default;
  38. this.opt.default = null;
  39. // Init
  40. this.render();
  41. return this;
  42. };
  43. /**
  44. * Render the prompt to screen
  45. * @return {Prompt} self
  46. */
  47. Prompt.prototype.render = function (error) {
  48. var bottomContent = '';
  49. var message = this.getQuestion();
  50. if (this.status === 'answered') {
  51. message += chalk.dim('Received');
  52. } else {
  53. message += chalk.dim('Press <enter> to launch your preferred editor.');
  54. }
  55. if (error) {
  56. bottomContent = chalk.red('>> ') + error;
  57. }
  58. this.screen.render(message, bottomContent);
  59. };
  60. /**
  61. * Launch $EDITOR on user press enter
  62. */
  63. Prompt.prototype.startExternalEditor = function () {
  64. // Pause Readline to prevent stdin and stdout from being modified while the editor is showing
  65. this.rl.pause();
  66. ExternalEditor.editAsync(this.currentText, this.endExternalEditor.bind(this));
  67. };
  68. Prompt.prototype.endExternalEditor = function (error, result) {
  69. this.rl.resume();
  70. if (error) {
  71. this.editorResult.onError(error);
  72. } else {
  73. this.editorResult.onNext(result);
  74. }
  75. };
  76. Prompt.prototype.onEnd = function (state) {
  77. this.editorResult.dispose();
  78. this.lineSubscription.dispose();
  79. this.answer = state.value;
  80. this.status = 'answered';
  81. // Re-render prompt
  82. this.render();
  83. this.screen.done();
  84. this.done(this.answer);
  85. };
  86. Prompt.prototype.onError = function (state) {
  87. this.render(state.isValid);
  88. };