Skip to content

Commit

Permalink
Merge a5b8f17 into a26e3e7
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbro committed Oct 3, 2019
2 parents a26e3e7 + a5b8f17 commit 157700c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/tty/prompt/confirm_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,30 @@ def render_question

protected

# Decide how to handle input from user
#
# @api private
def process_input(question)
@input = read_input(question)
if Utils.blank?(@input)
@input = default ? positive : negative
end
@evaluator.call(@input)
end

# @api private
def setup_defaults
@convert = conversion
return if suffix? && positive?

if suffix? && (!positive? || !negative?)
parts = @suffix.split('/')
@positive = parts[0]
@negative = parts[1]
@convert = conversion
elsif !suffix? && positive?
@suffix = create_suffix
@convert = conversion
else
create_default_labels
@convert = :bool
end
end

Expand All @@ -108,6 +117,8 @@ def create_default_labels
@suffix = default ? 'Y/n' : 'y/N'
@positive = default ? 'Yes' : 'yes'
@negative = default ? 'no' : 'No'
@validation = /^(y(es)?|no?)$/i
@messages[:valid?] = "Invalid input."
end

# @api private
Expand Down
34 changes: 34 additions & 0 deletions spec/unit/yes_no_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@
].join)
end

it 'warns about invalid entry when using defaults' do
prompt.input << "test"
prompt.input.rewind
prompt.yes?("Are you a human?")
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m t",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m te",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m tes",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m test",
"\e[31m>>\e[0m Invalid input.\e[1A",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end

it 'assumes default true' do
prompt.input << "\r"
prompt.input.rewind
Expand Down Expand Up @@ -181,6 +198,23 @@
].join)
end

it 'warns about invalid entry when using defaults' do
prompt.input << "test"
prompt.input.rewind
prompt.no?("Are you a human?")
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m t",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m te",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m tes",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m test",
"\e[31m>>\e[0m Invalid input.\e[1A",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end

it 'assumes default false' do
prompt.input << "\r"
prompt.input.rewind
Expand Down

0 comments on commit 157700c

Please sign in to comment.