Skip to content

Commit

Permalink
Split off set_signal_handler method
Browse files Browse the repository at this point in the history
In some tests, the LineEditor#reset method is always called, but doesn't
need to set the signal handlers there, so cuts it out to a separate
method.
  • Loading branch information
aycabta committed Dec 20, 2021
1 parent 7a758e7 commit b143c4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def readline(prompt = '', add_hist = false)

may_req_ambiguous_char_width
line_editor.reset(prompt, encoding: Reline::IOGate.encoding)
line_editor.set_signal_handlers
if multiline
line_editor.multiline_on
if block_given?
Expand Down
57 changes: 30 additions & 27 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,33 +151,6 @@ def reset(prompt = '', encoding:)
@screen_size = Reline::IOGate.get_screen_size
@screen_height = @screen_size.first
reset_variables(prompt, encoding: encoding)
@old_trap = Signal.trap('INT') {
clear_dialog
if @scroll_partial_screen
move_cursor_down(@screen_height - (@line_index - @scroll_partial_screen) - 1)
else
move_cursor_down(@highest_in_all - @line_index - 1)
end
Reline::IOGate.move_cursor_column(0)
scroll_down(1)
case @old_trap
when 'DEFAULT', 'SYSTEM_DEFAULT'
raise Interrupt
when 'IGNORE'
# Do nothing
when 'EXIT'
exit
else
@old_trap.call
end
}
begin
@old_tstp_trap = Signal.trap('TSTP') {
Reline::IOGate.ungetc("\C-z".ord)
@old_tstp_trap.call if @old_tstp_trap.respond_to?(:call)
}
rescue ArgumentError
end
Reline::IOGate.set_winch_handler do
@resized = true
end
Expand Down Expand Up @@ -244,6 +217,36 @@ def resize
end
end

def set_signal_handlers
@old_trap = Signal.trap('INT') {
clear_dialog
if @scroll_partial_screen
move_cursor_down(@screen_height - (@line_index - @scroll_partial_screen) - 1)
else
move_cursor_down(@highest_in_all - @line_index - 1)
end
Reline::IOGate.move_cursor_column(0)
scroll_down(1)
case @old_trap
when 'DEFAULT', 'SYSTEM_DEFAULT'
raise Interrupt
when 'IGNORE'
# Do nothing
when 'EXIT'
exit
else
@old_trap.call
end
}
begin
@old_tstp_trap = Signal.trap('TSTP') {
Reline::IOGate.ungetc("\C-z".ord)
@old_tstp_trap.call if @old_tstp_trap.respond_to?(:call)
}
rescue ArgumentError
end
end

def finalize
Signal.trap('INT', @old_trap)
begin
Expand Down

0 comments on commit b143c4f

Please sign in to comment.