Skip to content

Commit

Permalink
Call user defined sigwinch and sigcont handler (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored Nov 30, 2024
1 parent 7de5a50 commit 7d44770
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/reline/io/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,15 @@ def clear_screen
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.
Expand Down
37 changes: 35 additions & 2 deletions test/reline/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1795,16 +1795,47 @@ def test_thread_safe
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"
Expand All @@ -1814,6 +1845,8 @@ def test_stop_continue
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
Expand Down

0 comments on commit 7d44770

Please sign in to comment.