Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): add flag waitUserInput #1150

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
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
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
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
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'));
});
});