Skip to content

Commit ca61719

Browse files
committed
Merge pull request #317 from justincampbell/shell-defaults
Add support for a default to Shell::Basic#ask
2 parents 0ca96ec + 6412895 commit ca61719

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/thor/shell/basic.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ def padding=(value)
4747
#
4848
def ask(statement, *args)
4949
options = args.last.is_a?(Hash) ? args.pop : {}
50+
color = args.first
5051

51-
options[:limited_to] ? ask_filtered(statement, options[:limited_to], *args) : ask_simply(statement, *args)
52+
if options[:limited_to]
53+
ask_filtered(statement, color, options)
54+
else
55+
ask_simply(statement, color, options)
56+
end
5257
end
5358

5459
# Say (print) something to the user. If the sentence ends with a whitespace
@@ -372,15 +377,28 @@ def as_unicode
372377
end
373378
end
374379

375-
def ask_simply(statement, color=nil)
376-
say("#{statement} ", color)
377-
stdin.gets.tap{|text| text.strip! if text}
380+
def ask_simply(statement, color, options)
381+
default = options[:default]
382+
message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
383+
say(message, color)
384+
result = stdin.gets
385+
386+
return unless result
387+
388+
result.strip!
389+
390+
if default && result == ""
391+
default
392+
else
393+
result
394+
end
378395
end
379396

380-
def ask_filtered(statement, answer_set, *args)
397+
def ask_filtered(statement, color, options)
398+
answer_set = options[:limited_to]
381399
correct_answer = nil
382400
until correct_answer
383-
answer = ask_simply("#{statement} #{answer_set.inspect}", *args)
401+
answer = ask_simply("#{statement} #{answer_set.inspect}", color, options)
384402
correct_answer = answer_set.include?(answer) ? answer : nil
385403
answers = answer_set.map(&:inspect).join(", ")
386404
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer

spec/shell/basic_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ def shell
4242
$stdin.should_receive(:gets).and_return('moose tracks', 'chocolate')
4343
expect(shell.ask("What's your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("chocolate")
4444
end
45+
46+
it "prints a message to the user containing a default and sets the default if only enter is pressed" do
47+
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ("vanilla") ')
48+
$stdin.should_receive(:gets).and_return('')
49+
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla")).to eq("vanilla")
50+
end
51+
52+
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
53+
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ("vanilla") ').twice
54+
$stdout.should_receive(:puts).with('Your response must be one of: ["strawberry", "chocolate", "vanilla"]. Please try again.')
55+
$stdin.should_receive(:gets).and_return('moose tracks', '')
56+
expect(shell.ask("What's your favorite Neopolitan flavor?", :default => "vanilla", :limited_to => ["strawberry", "chocolate", "vanilla"])).to eq("vanilla")
57+
end
4558
end
4659

4760
describe "#yes?" do

0 commit comments

Comments
 (0)