[ruby/reline] Call user defined sigwinch and sigcont handler

(https://github.com/ruby/reline/pull/788)

7d44770c84
This commit is contained in:
tomoya ishida 2024-12-01 02:21:17 +09:00 committed by git
parent 569f27b425
commit 0fc70022e6
2 changed files with 41 additions and 4 deletions

View file

@ -284,11 +284,15 @@ class Reline::ANSI < Reline::IO
end
def set_winch_handler(&handler)
@old_winch_handler = Signal.trap('WINCH', &handler)
@old_cont_handler = Signal.trap('CONT') do
@old_winch_handler = Signal.trap('WINCH') do |arg|
handler.call
@old_winch_handler.call(arg) if @old_winch_handler.respond_to?(:call)
end
@old_cont_handler = Signal.trap('CONT') do |arg|
@input.raw!(intr: true) if @input.tty?
# Rerender the screen. Note that screen size might be changed while suspended.
handler.call
@old_cont_handler.call(arg) if @old_cont_handler.respond_to?(:call)
end
rescue ArgumentError
# Signal.trap may raise an ArgumentError if the platform doesn't support the signal.

View file

@ -1795,16 +1795,47 @@ begin
close
end
def test_user_defined_winch
omit if Reline.core.io_gate.win?
pidfile = Tempfile.create('pidfile')
rubyfile = Tempfile.create('rubyfile')
rubyfile.write <<~RUBY
File.write(#{pidfile.path.inspect}, Process.pid)
winch_called = false
Signal.trap(:WINCH, ->(_arg){ winch_called = true })
p Reline.readline('>')
puts "winch: \#{winch_called}"
RUBY
rubyfile.close
start_terminal(10, 50, %W{ruby -I#{@pwd}/lib -rreline #{rubyfile.path}})
assert_screen(/^>/)
write 'a'
assert_screen(/^>a/)
pid = pidfile.tap(&:rewind).read.to_i
Process.kill(:WINCH, pid) unless pid.zero?
write "b\n"
assert_screen(/"ab"\nwinch: true/)
close
ensure
File.delete(rubyfile.path) if rubyfile
pidfile.close if pidfile
File.delete(pidfile.path) if pidfile
end
def test_stop_continue
omit if Reline.core.io_gate.win?
pidfile = Tempfile.create('pidfile')
rubyfile = Tempfile.create('rubyfile')
rubyfile.write <<~RUBY
File.write(#{pidfile.path.inspect}, Process.pid)
p Reline.readmultiline('>'){false}
cont_called = false
Signal.trap(:CONT, ->(_arg){ cont_called = true })
Reline.readmultiline('>'){|input| input.match?(/ghi/) }
puts "cont: \#{cont_called}"
RUBY
rubyfile.close
start_terminal(40, 50, ['bash'])
start_terminal(10, 50, ['bash'])
write "ruby -I#{@pwd}/lib -rreline #{rubyfile.path}\n"
assert_screen(/^>/)
write "abc\ndef\nhi"
@ -1814,6 +1845,8 @@ begin
assert_screen(/fg\n.*>/m)
write "\ebg"
assert_screen(/>abc\n>def\n>ghi\n/)
write "\n"
assert_screen(/cont: true/)
close
ensure
File.delete(rubyfile.path) if rubyfile