Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #236 - Handle Ctrl-C when Question#echo = false (raw_no_echo_mode) #259

Merged
merged 4 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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