6 var chalk = require('chalk');
7 var editAsync = require('external-editor').editAsync;
8 var Base = require('./base');
9 var observe = require('../utils/events');
10 var { Subject } = require('rxjs');
12 class EditorPrompt extends Base {
14 * Start the Inquiry session
15 * @param {Function} cb Callback when prompt is done
22 this.editorResult = new Subject();
24 // Open Editor on "line" (Enter Key)
25 var events = observe(this.rl);
26 this.lineSubscription = events.line.subscribe(this.startExternalEditor.bind(this));
28 // Trigger Validation when editor closes
29 var validation = this.handleSubmitEvents(this.editorResult);
30 validation.success.forEach(this.onEnd.bind(this));
31 validation.error.forEach(this.onError.bind(this));
33 // Prevents default from being printed on screen (can look weird with multiple lines)
34 this.currentText = this.opt.default;
35 this.opt.default = null;
44 * Render the prompt to screen
45 * @return {EditorPrompt} self
49 var bottomContent = '';
50 var message = this.getQuestion();
52 if (this.status === 'answered') {
53 message += chalk.dim('Received');
55 message += chalk.dim('Press <enter> to launch your preferred editor.');
59 bottomContent = chalk.red('>> ') + error;
62 this.screen.render(message, bottomContent);
66 * Launch $EDITOR on user press enter
69 startExternalEditor() {
70 // Pause Readline to prevent stdin and stdout from being modified while the editor is showing
72 editAsync(this.currentText, this.endExternalEditor.bind(this));
75 endExternalEditor(error, result) {
78 this.editorResult.error(error);
80 this.editorResult.next(result);
85 this.editorResult.unsubscribe();
86 this.lineSubscription.unsubscribe();
87 this.answer = state.value;
88 this.status = 'answered';
92 this.done(this.answer);
96 this.render(state.isValid);
100 module.exports = EditorPrompt;