Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

editor.js 2.3KB

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