piotrmurach / tty-prompt

A beautiful and powerful interactive command line prompt
https://ttytoolkit.org
MIT License
1.47k stars 136 forks source link

Default's behavior should be pre-fill in #ask #45

Closed kke closed 7 years ago

kke commented 7 years ago

You can not enter an empty value to a prompt that has a default value.

So instead of treating empty response to mean "use default", maybe the default value should be prefilled to the input prompt.

piotrmurach commented 7 years ago

Interesting issue! I feel as though based on my experience that it is pretty much expectation in command line tools on how default values behave. It is impossible to detect whether user wishes to enter empty string or by entering empty string wants to use default value.

If I were to introduce your suggestion of 'pre-filling' default values that may get annoying pretty quickly for some users. I can easily imagine situation where you wish to prompt for few inputs where the defaults simply don't apply to the user. She would have to first delete the default input and then type in what she wanted originally. Having said that, I can also imagine that you could have the input prefilled with default value with a gray colour and when enter is pressed the value is submitted as is and when user starts typing the value is cleared and actual input accepted. Though this still leaves the question of what to do when someone wants to submit empty input. Should they press delete and hence remove default input?

I'm not sure whether the default behaviour should be changed or to allow ask prompt to accept :prefill option that changes how defaults are gathered as described above? I want the solution to work for everybody and feel intuitive.

kke commented 7 years ago

Good point. Harder nut to crack than I anticipated.

Perhaps jump out of the field with tab and get something like select?

Maybe there could then be default: 'foo' and suggestions: { ... }.

Enter foo: (foo) _
   * Use empty
   * /etc/foo

Of course this could be flipped around and just use:

foo = prompt.select('Foo?') do |menu|
  menu.choice 'foo'
  menu.choice '<custom>', :custom
end

foo = prompt.ask('Custom foo:") if foo == :custom
piotrmurach commented 7 years ago

The more I think about this request, the more I'm reluctant to change the current behaviour. I'm really struggling to see when and why it would be a good thing to allow for empty input with default option? In your example, does 'Custom foo' being empty make much sense in your tool?

If you really want empty input, then I would probably go with #yes? prompt to decide whether a user wants to provide custom value or pick default and then having actual boolean flag decide whether #ask should be shown at all. Would that work?

As guiding principle, I want to keep the prompts as simple as possible and allow them to be combined in powerful ways.

kke commented 7 years ago

Agreed