Skip to content

Commit

Permalink
Merge 37a2e33 into dc96d00
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed Mar 28, 2013
2 parents dc96d00 + 37a2e33 commit cfc6d0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/thor/shell/basic.rb
Expand Up @@ -48,7 +48,7 @@ def padding=(value)
def ask(statement, *args)
options = args.last.is_a?(Hash) ? args.pop : {}

options[:limited_to] ? ask_filtered(statement, options[:limited_to], *args) : ask_simply(statement, *args)
options[:limited_to] ? ask_filtered(statement, options, *args) : ask_simply(statement, options, *args)
end

# Say (print) something to the user. If the sentence ends with a whitespace
Expand Down Expand Up @@ -372,15 +372,24 @@ def as_unicode
end
end

def ask_simply(statement, color=nil)
say("#{statement} ", color)
stdin.gets.tap{|text| text.strip! if text}
def ask_simply(statement, options = {}, color=nil)
default = options[:default]
message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
say(message, color)
result = stdin.gets.tap{|text| text.strip! if text}

if default && result == ""
default
else
result
end
end

def ask_filtered(statement, answer_set, *args)
def ask_filtered(statement, options = {}, *args)
answer_set = options[:limited_to]
correct_answer = nil
until correct_answer
answer = ask_simply("#{statement} #{answer_set.inspect}", *args)
answer = ask_simply("#{statement} #{answer_set.inspect}", options, *args)
correct_answer = answer_set.include?(answer) ? answer : nil
answers = answer_set.map(&:inspect).join(", ")
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
Expand Down
13 changes: 13 additions & 0 deletions spec/shell/basic_spec.rb
Expand Up @@ -42,6 +42,19 @@ def shell
$stdin.should_receive(:gets).and_return('moose tracks', 'chocolate')
expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
end

it "prints a message to the user containing a default and sets the default if only enter is pressed" do
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ("vanilla") ')
$stdin.should_receive(:gets).and_return('')
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla")).to eq("vanilla")
end

it "prints a message to the user with the available options and reasks the question after an incorrect repsonse and then returns the default" do
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ("vanilla") ').twice
$stdout.should_receive(:puts).with('Your response must be one of: ["strawberry", "chocolate", "vanilla"]. Please try again.')
$stdin.should_receive(:gets).and_return('moose tracks', '')
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("vanilla")
end
end

describe "#yes?" do
Expand Down

0 comments on commit cfc6d0f

Please sign in to comment.