From 37a2e338294dde6d2fe09dcd0718b301f2644419 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Thu, 28 Mar 2013 08:36:41 -0400 Subject: [PATCH] Add support for a default to #ask --- lib/thor/shell/basic.rb | 21 +++++++++++++++------ spec/shell/basic_spec.rb | 13 +++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/thor/shell/basic.rb b/lib/thor/shell/basic.rb index 1e1e9be14..b21a631c3 100644 --- a/lib/thor/shell/basic.rb +++ b/lib/thor/shell/basic.rb @@ -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 @@ -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 diff --git a/spec/shell/basic_spec.rb b/spec/shell/basic_spec.rb index c1a417350..d551279e1 100644 --- a/spec/shell/basic_spec.rb +++ b/spec/shell/basic_spec.rb @@ -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