Skip to content

Commit

Permalink
feat(editor): add flag waitUserInput (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
dellamina authored Aug 4, 2022
1 parent 6f497f1 commit 8328d65
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ A question object is a `hash` containing question related values:
- **suffix**: (String) Change the default _suffix_ message.
- **askAnswered**: (Boolean) Force to prompt the question if the answer already exists.
- **loop**: (Boolean) Enable list looping. Defaults: `true`
- **waitUserInput**: (Boolean) Flag to enable/disable wait for user input before opening system editor - Defaults: `true`

`default`, `choices`(if defined as functions), `validate`, `filter` and `when` functions can be called asynchronously. Either return a promise or use `this.async()` to get a callback you'll call with the final value.

Expand Down Expand Up @@ -300,7 +301,7 @@ Note that `mask` is required to hide the actual user input.

#### Editor - `{type: 'editor'}`

Take `type`, `name`, `message`[, `default`, `filter`, `validate`, `postfix`] properties
Take `type`, `name`, `message`[, `default`, `filter`, `validate`, `postfix`, `waitUserInput`] properties

Launches an instance of the users preferred editor on a temporary file. Once the user exits their editor, the contents of the temporary file are read in as the result. The editor to use is determined by reading the $VISUAL or $EDITOR environment variables. If neither of those are present, notepad (on Windows) or vim (Linux or Mac) is used.

Expand Down
1 change: 1 addition & 0 deletions packages/inquirer/examples/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const questions = [

return true;
},
waitUserInput: true,
},
];

Expand Down
6 changes: 6 additions & 0 deletions packages/inquirer/lib/prompts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export default class EditorPrompt extends Base {
// Open Editor on "line" (Enter Key)
const events = observe(this.rl);
this.lineSubscription = events.line.subscribe(this.startExternalEditor.bind(this));
const waitUserInput =
this.opt.waitUserInput === undefined ? true : this.opt.waitUserInput;

if (!waitUserInput) {
this.startExternalEditor();
}

// Trigger Validation when editor closes
const validation = this.handleSubmitEvents(this.editorResult);
Expand Down
9 changes: 9 additions & 0 deletions packages/inquirer/test/specs/prompts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ describe('`editor` prompt', () => {

return promise.then((answer) => expect(answer).to.equal('testing'));
});

it('should open editor without waiting for the user to press enter', function () {
this.fixture.waitUserInput = false;
const prompt = new Editor(this.fixture, this.rl);

const promise = prompt.run();

return promise.then((answer) => expect(answer).to.equal('testing'));
});
});

0 comments on commit 8328d65

Please sign in to comment.