Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support Readline history.

* By default, the response to any #ask call is added to the history.
* Pass `:add_to_history => false` to #ask to avoid adding to history.
* #yes?, #no?, and #file_collision? have `:add_to_history => false` by
  default.
  • Loading branch information...
commit 22c91f7b94c97b6aa562bdba55bc1f1084383758 1 parent 478e5be
George Brocklehurst georgebrock authored georgebrock committed
8 lib/thor/line_editor/readline.rb
View
@@ -8,7 +8,13 @@ def self.available?
end
def readline
- ::Readline.readline(prompt)
+ ::Readline.readline(prompt, add_to_history?)
+ end
+
+ private
+
+ def add_to_history?
+ options.fetch(:add_to_history, true)
end
end
end
9 lib/thor/shell/basic.rb
View
@@ -100,14 +100,14 @@ def say_status(status, message, log_status = true)
# "yes".
#
def yes?(statement, color = nil)
- !!(ask(statement, color) =~ is?(:yes))
+ !!(ask(statement, color, :add_to_history => false) =~ is?(:yes))
end
# Make a question the to user and returns true if the user replies "n" or
# "no".
#
def no?(statement, color = nil)
- !!(ask(statement, color) =~ is?(:no))
+ !!(ask(statement, color, :add_to_history => false) =~ is?(:no))
end
# Prints values in columns
@@ -227,7 +227,10 @@ def file_collision(destination) # rubocop:disable MethodLength
options = block_given? ? '[Ynaqdh]' : '[Ynaqh]'
loop do
- answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
+ answer = ask(
+ %[Overwrite #{destination}? (enter "h" for help) #{options}],
+ :add_to_history => false
+ )
case answer
when is?(:yes), is?(:force), ''
8 spec/line_editor/readline_spec.rb
View
@@ -21,9 +21,15 @@
describe '#readline' do
it 'invokes the readline library' do
- expect(::Readline).to receive(:readline).with('> ').and_return('foo')
+ expect(::Readline).to receive(:readline).with('> ', true).and_return('foo')
editor = Thor::LineEditor::Readline.new('> ', {})
expect(editor.readline).to eq('foo')
end
+
+ it 'supports the add_to_history option' do
+ expect(::Readline).to receive(:readline).with('> ', false).and_return('foo')
+ editor = Thor::LineEditor::Readline.new('> ', :add_to_history => false)
+ expect(editor.readline).to eq('foo')
+ end
end
end
18 spec/shell/basic_spec.rb
View
@@ -67,34 +67,34 @@ def shell
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? ', :add_to_history => false).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? ', :add_to_history => false).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? ', :add_to_history => false).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? ', :add_to_history => false).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? ', :add_to_history => false).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? ', :add_to_history => false).and_return('foobar')
expect(shell.no?('Should I overwrite it?')).to be_false
end
end
@@ -272,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] ', :add_to_history => false).and_return('n')
shell.file_collision('foo')
end
@@ -307,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] ', :add_to_history => false).and_return('a')
expect(shell.file_collision('foo')).to be true
@@ -317,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] ', :add_to_history => false).and_return('s')
shell.file_collision('foo'){ }
end
Please sign in to comment.
Something went wrong with that request. Please try again.