JEG2 / highline

A higher level command-line oriented interface.
Other
1.29k stars 137 forks source link

More information about default value for user #182

Closed tukanos closed 8 years ago

tukanos commented 8 years ago

Indicate default value for the user on the output. The user is presented by only |value|, which is in, my opinion, not enough. The user should now that there is a default value waiting for him. User should be prompted by something like: |Default value: value|

abinoam commented 8 years ago

Hi @tukanos,

First, sorry for the late answer. I wouldn't like to introduce such a change because I think it is not appropriate for everyone in every situation. But If you really need such behaviour, I would be glad to accept a PR that make it easy to the user to customise the behaviour.

You can check the method Question#append_default on lib/highline/question.rb

   #
    # Adds the default choice to the end of question between <tt>|...|</tt>.
    # Trailing whitespace is preserved so the function of HighLine.say() is
    # not affected.
    #
    def append_default
      if template =~ /([\t ]+)\Z/
        template << "|#{default}|#{$1}"
      elsif template == ""
        template << "|#{default}|  "
      elsif template[-1, 1] == "\n"
        template[-2, 0] =  "  |#{default}|"
      else
        template << "  |#{default}|"
      end
    end

One idea is to change the attribute accessor default to a full featured method that conditionally adds the desired string based on something the user has defined.

I'm closing this PR, but feel free to discuss it a little more. We could reopen it if necessary.

Thanks

cc: @JEG2

TODO: Looking at this code made me think that we should change this not_so_meaningful attribute accessor's name default to something more clear like _defaultanswer

JEG2 commented 8 years ago

default_answer does sound better.

I agree.

tukanos commented 8 years ago

When I read the code I had to test it to find where is the default is produced. I have to agree with _defaultanswer as being much better.

Thank you for your answer. I have to agree that my patch is a 'crude' fix for my issue. In my mind I wanted to have a discussion about this topic.

For example: The console application produces this interaction with the user:

Do want to TIME your scrape (y/n)? y How many MINUTES should the application run? Default: |0|

As there was no way to add any information to the default value produced I had to come up with this solution. I don't like the result, but it is, at least, understandable.

UPDATE:

One idea is to change the attribute accessor default to a full featured method that conditionally adds the desired string based on something the user has defined.

Missed this comment. Yes, that would be perfect solution. END UPDATE:

The best solution would be if the programmer could tell the application to add some additional information to the default_answer. (Maybe even the default_answer separator could be configured if that is not an overkill).

The snippets:

default_answer = default_answer_text + default_answer_separator + ' ' + default_answer unless default_answer_text.nil? or default_answer_text.empty? # that would depend on the initialization

or more verbose

unless default_answer_text.nil? or default_answer_text.empty? # that would depend on the initialization
  default_answer << default_answer_text
  default_answer << default_answer_separator
  default_answer << ' '
  default_answer << 'default_answer'
end

In the end you could have one more method something like which would return formatted default_answer with the text specified. If this functionality would not be used then programmer can just ignore it and it will produce the same result.