summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2023-01-01 14:43:55 -0300
committerGitHub <noreply@github.com>2023-01-01 14:43:55 -0300
commitee18153d63db64242fdbf467a89774c9627d54d5 (patch)
treefe2c5cf23b79265a976cdb08371b8c1e53a7d352
parent49eed912b0113b3b9c0949f0ef056f390ed359ba (diff)
parentb4067e75b620573238115a6f7252ffeaaf9672cc (diff)
downloadhighline-ee18153d63db64242fdbf467a89774c9627d54d5.tar.gz
Merge pull request #257 from abinoam/issue_233
Issue #233 - Show Question#default value hint for non String objects
-rw-r--r--Changelog.md4
-rw-r--r--README.md9
-rw-r--r--lib/highline/question.rb22
-rwxr-xr-xtest/test_highline.rb32
4 files changed, 60 insertions, 7 deletions
diff --git a/Changelog.md b/Changelog.md
index fb665c2..9a86f8a 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,10 @@
Below is a complete listing of changes for each revision of HighLine.
+### Unreleased 2.2.0.develop.1
+* I #233 Show Question#default hint for non String values (@abinoam)
+ * Add Question#default_hint_show to allow disabling it.
+
### 2.1.0 / 2022-12-31
* PR #255 - Change minimum Ruby version requirement to 2.3 (@abinoam)
* PR #254 - Improve Github Actions file (@abinoam)
diff --git a/README.md b/README.md
index 740b446..c1baf49 100644
--- a/README.md
+++ b/README.md
@@ -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 e623305..13ff406 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -817,17 +817,47 @@ class TestHighLine < Minitest::Test
end
def test_default_with_non_String_objects
- # With a non-string object, it should not show
+ # With a non-string object, it should try to coerce it to String.
+ # If the coercion is not possible, do not show
# any 'default' at prompt line. And should
# return the "default" object, without conversion.
@input << "\n"
@input.rewind
+ default_integer_object = 42
+
+ answer = @terminal.ask("Question: ") do |q|
+ q.default = default_integer_object
+ end
+
+ assert_equal default_integer_object, answer
+ assert_equal "Question: |42| ", @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
+ 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