Skip to content

Commit c42a6c0

Browse files
committed
Move :echo => false support into LineEditors
:echo => false was added to the ask method in dbfdd30, this maintains that behaviour with Readline support. Tip o' the hat to @cwninja, we paired on this.
1 parent 8e54c8f commit c42a6c0

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

lib/thor/line_editor/basic.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@ def initialize(prompt, options)
1414

1515
def readline
1616
$stdout.print(prompt)
17-
$stdin.gets
17+
get_input
18+
end
19+
20+
private
21+
22+
def get_input
23+
if echo?
24+
$stdin.gets
25+
else
26+
$stdin.noecho(&:gets)
27+
end
28+
end
29+
30+
def echo?
31+
options.fetch(:echo, true)
1832
end
1933
end
2034
end

lib/thor/line_editor/readline.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ def self.available?
88
end
99

1010
def readline
11-
::Readline.completion_append_character = nil
12-
::Readline.completion_proc = completion_proc
13-
::Readline.readline(prompt, add_to_history?)
11+
if echo?
12+
::Readline.completion_append_character = nil
13+
::Readline.completion_proc = completion_proc
14+
::Readline.readline(prompt, add_to_history?)
15+
else
16+
super
17+
end
1418
end
1519

1620
private

spec/line_editor/basic_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@
1111
it 'uses $stdin and $stdout to get input from the user' do
1212
expect($stdout).to receive(:print).with('Enter your name ')
1313
expect($stdin).to receive(:gets).and_return('George')
14+
expect($stdin).not_to receive(:noecho)
1415
editor = Thor::LineEditor::Basic.new('Enter your name ', {})
1516
expect(editor.readline).to eq('George')
1617
end
18+
19+
it 'disables echo when asked to' do
20+
expect($stdout).to receive(:print).with('Password: ')
21+
noecho_stdin = double('noecho_stdin')
22+
expect(noecho_stdin).to receive(:gets).and_return('secret')
23+
expect($stdin).to receive(:noecho).and_yield(noecho_stdin)
24+
editor = Thor::LineEditor::Basic.new('Password: ', :echo => false)
25+
expect(editor.readline).to eq('secret')
26+
end
1727
end
1828
end

spec/line_editor/readline_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,14 @@
5656
editor = Thor::LineEditor::Readline.new('Path to file: ', :path => true)
5757
Dir.chdir(File.dirname(__FILE__)) { editor.readline }
5858
end
59+
60+
it 'uses STDIN when asked not to echo input' do
61+
expect($stdout).to receive(:print).with('Password: ')
62+
noecho_stdin = double('noecho_stdin')
63+
expect(noecho_stdin).to receive(:gets).and_return('secret')
64+
expect($stdin).to receive(:noecho).and_yield(noecho_stdin)
65+
editor = Thor::LineEditor::Readline.new('Password: ', :echo => false)
66+
expect(editor.readline).to eq('secret')
67+
end
5968
end
6069
end

0 commit comments

Comments
 (0)