-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Allow empty input by removing default if user enters backspace on empty input #700
Allow empty input by removing default if user enters backspace on empty input #700
Conversation
Codecov Report
@@ Coverage Diff @@
## master #700 +/- ##
==========================================
- Coverage 92.73% 92.69% -0.05%
==========================================
Files 26 26
Lines 1033 1040 +7
Branches 19 19
==========================================
+ Hits 958 964 +6
- Misses 75 76 +1
Continue to review full report at Codecov.
|
I think stripping the quotes might have side effet for other users who might need them. I guess you could allow spaces and strip them in the filter function. Or maybe use |
Are you suggesting backspace would clear the default and rerender the prompt? That sounds great! |
Yeah, I think this would be more intuitive and more clearly communicate it with the user. Want to send a PR? |
I've looked into using backspace, and I don't actually see how we could do it correctly. It's possible to intercept the backspace here and remove the default value, but it's not possible to distinguish whether the user has hit backspace on an already empty line (for which we want to clear the default) or instead has hit backspace to remove the last previously-typed character to leave the line blank (which I don't think should trigger clearing the default. Maybe an example will clarify. If we change that onKeypress(event) {
const { key } = event;
// Empty the default when a user clears the input
if (key.name === 'backspace' && !this.rl.line) {
console.log('');
console.log('key', key);
console.log('this.rl.line', JSON.stringify(this.rl.line));
this.opt.default = undefined;
}
this.render();
} $ node packages/inquirer/examples/input.js
? What's your first name # hit enter
? What's your last name (Doe) # hit backspace
key { sequence: '�',
name: 'backspace',
ctrl: false,
meta: false,
shift: false }
this.rl.line ""
? What's your last name # ^D
$ node packages/inquirer/examples/input.js
? What's your first name # hit enter
? What's your last name (Doe) a # hit a followed by backspace
key { sequence: '�',
name: 'backspace',
ctrl: false,
meta: false,
shift: false }
this.rl.line ""
? What's your last name # ^D |
I see the problem. I think on keypress, we can check the value of this.line. And if the line is empty then we set a flag like |
When the input is empty and backspace key is entered, the default is cleared. This allows the user to enter an empty value.
c44f566
to
ef148f5
Compare
@SBoudrias Ok, I updated this to use the backspace key instead of stripping surrounding quotes. I realized that we want to get this behavior even if the user has made no keypress before the backspace, so I implemented that little state tracking as |
Thanks! The latest version looks great :) |
Thanks for your thoughtful feedback, Simon. |
This version includes SBoudrias/Inquirer.js#700, which enables a user to enter an empty input value for an input with a default value. To enter an empty value, a user hits the backspace key when the input is empty. This clears the default value.
This works for a text field, but doesn't work for password. |
When the input is empty and backspace key is entered, the default is cleared. This allows the user to enter an empty value.
Strip surrounding quotes from input value; enable empty valuesAs mentioned a loooong time ago in #108, there's currently no way to input an empty value for an input with a default value.
But what if we allowed users to put quotes around their input values the way you would when setting a bash variable? And then the value would be set to the unquoted value.In other words, when a user enters"foo"
the value would be set tofoo
(without quotes).This feels like expected behavior to me, and it would enable an empty value for an input with a default value, because""
would become an empty string.Is this too magical or unexpected?Would it be better to simply treat a value of""
or''
as a special case that should be transformed to an empty string?I think some solution would be preferable to the current recommendation (adding an extra Y/N question).
UPDATED in line with comment to use
backspace
instead of stripping quotes.