File tree Expand file tree Collapse file tree 4 files changed +41
-4
lines changed Expand file tree Collapse file tree 4 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,21 @@ def initialize(prompt, options)
14
14
15
15
def readline
16
16
$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 )
18
32
end
19
33
end
20
34
end
Original file line number Diff line number Diff line change @@ -8,9 +8,13 @@ def self.available?
8
8
end
9
9
10
10
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
14
18
end
15
19
16
20
private
Original file line number Diff line number Diff line change 11
11
it 'uses $stdin and $stdout to get input from the user' do
12
12
expect ( $stdout) . to receive ( :print ) . with ( 'Enter your name ' )
13
13
expect ( $stdin) . to receive ( :gets ) . and_return ( 'George' )
14
+ expect ( $stdin) . not_to receive ( :noecho )
14
15
editor = Thor ::LineEditor ::Basic . new ( 'Enter your name ' , { } )
15
16
expect ( editor . readline ) . to eq ( 'George' )
16
17
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
17
27
end
18
28
end
Original file line number Diff line number Diff line change 56
56
editor = Thor ::LineEditor ::Readline . new ( 'Path to file: ' , :path => true )
57
57
Dir . chdir ( File . dirname ( __FILE__ ) ) { editor . readline }
58
58
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
59
68
end
60
69
end
You can’t perform that action at this time.
0 commit comments