Browse files

Pass #ask options on to LineEditor.readline

This will allow for various editor-level customisations based on the
options, and for the set of #ask options to be expanded to include options
that specifically effect the line editor.
  • Loading branch information...
1 parent ec0bf68 commit 478e5bee21612128357e466f3d08c03c0798ed5c @georgebrock georgebrock committed with georgebrock Aug 11, 2013
View
4 lib/thor/line_editor.rb
@@ -3,8 +3,8 @@
class Thor
module LineEditor
- def self.readline(prompt)
- best_available.new(prompt).readline
+ def self.readline(prompt, options={})
+ best_available.new(prompt, options).readline
end
def self.best_available
View
5 lib/thor/line_editor/basic.rb
@@ -1,14 +1,15 @@
class Thor
module LineEditor
class Basic
- attr_reader :prompt
+ attr_reader :prompt, :options
def self.available?
true
end
- def initialize(prompt)
+ def initialize(prompt, options)
@prompt = prompt
+ @options = options
end
def readline
View
2 lib/thor/shell/basic.rb
@@ -381,7 +381,7 @@ def ask_simply(statement, color, options)
default = options[:default]
message = [statement, ("(#{default})" if default), nil].uniq.join(' ')
message = prepare_message(message, color)
- result = Thor::LineEditor.readline(message)
+ result = Thor::LineEditor.readline(message, options)
return unless result
View
2 spec/actions/create_file_spec.rb
@@ -103,7 +103,7 @@ def silence!
it 'shows conflict status to the user' do
file = File.join(destination_root, 'doc/config.rb')
expect(create_file('doc/config.rb')).not_to be_identical
- expect(Thor::LineEditor).to receive(:readline).with("Overwrite #{file}? (enter \"h\" for help) [Ynaqdh] ").and_return('s')
+ expect(Thor::LineEditor).to receive(:readline).with("Overwrite #{file}? (enter \"h\" for help) [Ynaqdh] ", anything).and_return('s')
content = invoke!
expect(content).to match(/conflict doc\/config\.rb/)
View
2 spec/line_editor/basic_spec.rb
@@ -11,7 +11,7 @@
it 'uses $stdin and $stdout to get input from the user' do
expect($stdout).to receive(:print).with('Enter your name ')
expect($stdin).to receive(:gets).and_return('George')
- editor = Thor::LineEditor::Basic.new('Enter your name ')
+ editor = Thor::LineEditor::Basic.new('Enter your name ', {})
expect(editor.readline).to eq('George')
end
end
View
2 spec/line_editor/readline_spec.rb
@@ -22,7 +22,7 @@
describe '#readline' do
it 'invokes the readline library' do
expect(::Readline).to receive(:readline).with('> ').and_return('foo')
- editor = Thor::LineEditor::Readline.new('> ')
+ editor = Thor::LineEditor::Readline.new('> ', {})
expect(editor.readline).to eq('foo')
end
end
View
8 spec/line_editor_spec.rb
@@ -13,9 +13,9 @@
describe '.readline' do
it 'uses the Readline line editor' do
editor = double('Readline')
- expect(Thor::LineEditor::Readline).to receive(:new).with('Enter your name ').and_return(editor)
+ expect(Thor::LineEditor::Readline).to receive(:new).with('Enter your name ', :default => 'Brian').and_return(editor)
expect(editor).to receive(:readline).and_return('George')
- expect(Thor::LineEditor.readline('Enter your name ')).to eq('George')
+ expect(Thor::LineEditor.readline('Enter your name ', :default => 'Brian')).to eq('George')
end
end
end
@@ -35,9 +35,9 @@
describe '.readline' do
it 'uses the Basic line editor' do
editor = double('Basic')
- expect(Thor::LineEditor::Basic).to receive(:new).with('Enter your name ').and_return(editor)
+ expect(Thor::LineEditor::Basic).to receive(:new).with('Enter your name ', :default => 'Brian').and_return(editor)
expect(editor).to receive(:readline).and_return('George')
- expect(Thor::LineEditor.readline('Enter your name ')).to eq('George')
+ expect(Thor::LineEditor.readline('Enter your name ', :default => 'Brian')).to eq('George')
end
end
end
View
43 spec/shell/basic_spec.rb
@@ -18,18 +18,18 @@ def shell
describe '#ask' do
it 'prints a message to the user and gets the response' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('Sure')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('Sure')
expect(shell.ask('Should I overwrite it?')).to eq('Sure')
end
it 'prints a message to the user prefixed with the current padding' do
- expect(Thor::LineEditor).to receive(:readline).with(' Enter your name: ').and_return('George')
+ expect(Thor::LineEditor).to receive(:readline).with(' Enter your name: ', {}).and_return('George')
shell.padding = 2
shell.ask('Enter your name:')
end
- it 'prints a message and returns nil if EOF is sent to stdin' do
- expect(Thor::LineEditor).to receive(:readline).with(' ').and_return(nil)
+ it 'prints a message and returns nil if EOF is given as input' do
+ expect(Thor::LineEditor).to receive(:readline).with(' ', {}).and_return(nil)
expect(shell.ask('')).to eq(nil)
end
@@ -40,58 +40,61 @@ def shell
end
it 'prints a message to the user with the available options and determines the correctness of the answer' do
- expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ').and_return('chocolate')
- expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => %w[strawberry chocolate vanilla])).to eq('chocolate')
+ flavors = %w[strawberry chocolate vanilla]
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return('chocolate')
+ expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq('chocolate')
end
it 'prints a message to the user with the available options and reasks the question after an incorrect repsonse' do
+ flavors = %w[strawberry chocolate vanilla]
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
- expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ').and_return('moose tracks', 'chocolate')
- expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => %w[strawberry chocolate vanilla])).to eq('chocolate')
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] ', :limited_to => flavors).and_return('moose tracks', 'chocolate')
+ expect(shell.ask('What\'s your favorite Neopolitan flavor?', :limited_to => flavors)).to eq('chocolate')
end
it 'prints a message to the user containing a default and sets the default if only enter is pressed' do
- expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ').and_return('')
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? (vanilla) ', :default => 'vanilla').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
+ flavors = %w[strawberry chocolate vanilla]
expect($stdout).to receive(:print).with("Your response must be one of: [strawberry, chocolate, vanilla]. Please try again.\n")
- expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] (vanilla) ').and_return('moose tracks', '')
- expect(shell.ask("What's your favorite Neopolitan flavor?", :default => 'vanilla', :limited_to => %w[strawberry chocolate vanilla])).to eq('vanilla')
+ expect(Thor::LineEditor).to receive(:readline).with('What\'s your favorite Neopolitan flavor? [strawberry, chocolate, vanilla] (vanilla) ', :default => 'vanilla', :limited_to => flavors).and_return('moose tracks', '')
+ expect(shell.ask("What's your favorite Neopolitan flavor?", :default => 'vanilla', :limited_to => flavors)).to eq('vanilla')
end
end
describe '#yes?' do
it 'asks the user and returns true if the user replies yes' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('y')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('y')
expect(shell.yes?('Should I overwrite it?')).to be_true
end
it 'asks the user and returns false if the user replies no' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('n')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('n')
expect(shell.yes?('Should I overwrite it?')).not_to be_true
end
it 'asks the user and returns false if the user replies with an answer other than yes or no' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('foobar')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('foobar')
expect(shell.yes?('Should I overwrite it?')).to be_false
end
end
describe '#no?' do
it 'asks the user and returns true if the user replies no' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('n')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('n')
expect(shell.no?('Should I overwrite it?')).to be_true
end
it 'asks the user and returns false if the user replies yes' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('Yes')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('Yes')
expect(shell.no?('Should I overwrite it?')).to be_false
end
it 'asks the user and returns false if the user replies with an answer other than yes or no' do
- expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ').and_return('foobar')
+ expect(Thor::LineEditor).to receive(:readline).with('Should I overwrite it? ', {}).and_return('foobar')
expect(shell.no?('Should I overwrite it?')).to be_false
end
end
@@ -269,7 +272,7 @@ def #456 Lanç...
describe '#file_collision' do
it 'shows a menu with options' do
- expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ').and_return('n')
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ', {}).and_return('n')
shell.file_collision('foo')
end
@@ -304,7 +307,7 @@ def #456 Lanç...
end
it 'always returns true if the user chooses always' do
- expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ').and_return('a')
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqh] ', {}).and_return('a')
expect(shell.file_collision('foo')).to be true
@@ -314,7 +317,7 @@ def #456 Lanç...
describe 'when a block is given' do
it 'displays diff options to the user' do
- expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ').and_return('s')
+ expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ', {}).and_return('s')
shell.file_collision('foo'){ }
end
View
4 spec/shell/color_spec.rb
@@ -12,10 +12,10 @@ def shell
describe '#ask' do
it 'sets the color if specified and tty?' do
- expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m").and_return('yes')
+ expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? \e[0m", anything).and_return('yes')
shell.ask 'Is this green?', :green
- expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m").and_return('Yes')
+ expect(Thor::LineEditor).to receive(:readline).with("\e[32mIs this green? [Yes, No, Maybe] \e[0m", anything).and_return('Yes')
shell.ask 'Is this green?', :green, :limited_to => ['Yes', 'No', 'Maybe']
end
end

0 comments on commit 478e5be

Please sign in to comment.