diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2023-01-01 14:00:16 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2023-01-01 14:00:16 -0300 |
commit | bf7ad58dceb5eba1b54461c402375cb711468b2c (patch) | |
tree | 30d6d1a7b36bd23ec62ce74a8cd2f4f5ed178cdd | |
parent | 078490a4b250008b0f762bceae67018f5f1864f9 (diff) | |
download | highline-bf7ad58dceb5eba1b54461c402375cb711468b2c.tar.gz |
Document the new Question#default_hint_show
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | lib/highline/question.rb | 22 | ||||
-rwxr-xr-x | test/test_highline.rb | 15 |
3 files changed, 40 insertions, 6 deletions
@@ -43,6 +43,15 @@ puts "You have answered: #{answer}" cli.ask("Company? ") { |q| q.default = "none" } +## Disable default value hint showing + +my_special_default_object = Object.new + +cli.ask("Question? ") do |q| + q.default = my_special_default_object + q.default_hint_show = false +end + # Validation diff --git a/lib/highline/question.rb b/lib/highline/question.rb index 50478ed..ce2245d 100644 --- a/lib/highline/question.rb +++ b/lib/highline/question.rb @@ -56,6 +56,7 @@ class HighLine @completion = @answer_type @echo = true + @default_hint_show = true @whitespace = :strip @case = nil @in = nil @@ -136,6 +137,10 @@ class HighLine attr_accessor :case # Used to provide a default answer to this question. attr_accessor :default + # Set it to a truthy or falsy value to enable or disable showing the default + # value hint between vertical bars (pipes) when asking the question. + # Defaults to +true+ + attr_accessor :default_hint_show # # If set to a Regexp, the answer must match (before type conversion). # Can also be set to a Proc which will be called with the provided @@ -252,7 +257,7 @@ class HighLine # Same as {#answer_type}. def build_responses(message_source = answer_type) - append_default if [::String, Symbol].include? default.class + append_default_to_template if default_hint_show new_hash = build_responses_new_hash(message_source) # Update our internal responses with the new hash @@ -607,15 +612,20 @@ class HighLine # Trailing whitespace is preserved so the function of HighLine.say() is # not affected. # - def append_default + def append_default_to_template + return unless default.respond_to? :to_s + + default_str = default.to_s + return if default_str.empty? + if template =~ /([\t ]+)\Z/ - template << "|#{default}|#{Regexp.last_match(1)}" + template << "|#{default_str}|#{Regexp.last_match(1)}" elsif template == "" - template << "|#{default}| " + template << "|#{default_str}| " elsif template[-1, 1] == "\n" - template[-2, 0] = " |#{default}|" + template[-2, 0] = " |#{default_str}|" else - template << " |#{default}|" + template << " |#{default_str}|" end end diff --git a/test/test_highline.rb b/test/test_highline.rb index aeb41ca..13ff406 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -846,6 +846,21 @@ class TestHighLine < Minitest::Test end assert_equal default_non_string_object, answer + assert_equal "Question: |#{default_non_string_object}| ", @output.string + + @input.truncate(@input.rewind) + @input << "\n" + @input.rewind + @output.truncate(@output.rewind) + + default_non_string_object = Object.new + + answer = @terminal.ask("Question: ") do |q| + q.default = default_non_string_object + q.default_hint_show = false + end + + assert_equal default_non_string_object, answer assert_equal "Question: ", @output.string end |