Skip to content

Commit

Permalink
Merge pull request #259 from abinoam/issue_236
Browse files Browse the repository at this point in the history
Handle Ctrl-C when Question#echo = false (raw_no_echo_mode)
  • Loading branch information
abinoam committed Jan 6, 2023
2 parents 7755f1f + 75010bc commit 8e5f773
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ def get_line_raw_no_echo_mode(question)
terminal.raw_no_echo_mode_exec do
loop do
character = terminal.get_character
raise Interrupt if character == "\u0003"
break unless character
break if ["\n", "\r"].include? character

Expand Down
2 changes: 1 addition & 1 deletion lib/highline/io_console_compatible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

module IOConsoleCompatible
def getch
def getch(min:nil, time:nil, intr: nil)
getc
end

Expand Down
2 changes: 1 addition & 1 deletion lib/highline/terminal/io_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def restore_mode

# (see Terminal#get_character)
def get_character
input.getch # from ruby io/console
input.getch(intr: true) # from ruby io/console
rescue Errno::ENOTTY
input.getc
end
Expand Down
14 changes: 14 additions & 0 deletions test/test_highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,20 @@ def test_echoing_with_utf8_when_echo_is_star
assert_equal("maçã", answer)
end

def test_echo_false_with_ctrl_c_interrupts
@input << "String with a ctrl-c at the end \u0003 \n"
@input.rewind
@answer = nil

assert_raises(Interrupt) do
@answer = @terminal.ask("Type: ") do |q|
q.echo = false
end
end

assert_nil @answer
end

def test_range_requirements
@input << "112\n-541\n28\n"
@input.rewind
Expand Down

0 comments on commit 8e5f773

Please sign in to comment.