Skip to content

Commit 7d44770

Browse files
authored
Call user defined sigwinch and sigcont handler (#788)
1 parent 7de5a50 commit 7d44770

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/reline/io/ansi.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,15 @@ def clear_screen
284284
end
285285

286286
def set_winch_handler(&handler)
287-
@old_winch_handler = Signal.trap('WINCH', &handler)
288-
@old_cont_handler = Signal.trap('CONT') do
287+
@old_winch_handler = Signal.trap('WINCH') do |arg|
288+
handler.call
289+
@old_winch_handler.call(arg) if @old_winch_handler.respond_to?(:call)
290+
end
291+
@old_cont_handler = Signal.trap('CONT') do |arg|
289292
@input.raw!(intr: true) if @input.tty?
290293
# Rerender the screen. Note that screen size might be changed while suspended.
291294
handler.call
295+
@old_cont_handler.call(arg) if @old_cont_handler.respond_to?(:call)
292296
end
293297
rescue ArgumentError
294298
# Signal.trap may raise an ArgumentError if the platform doesn't support the signal.

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,16 +1795,47 @@ def test_thread_safe
17951795
close
17961796
end
17971797

1798+
def test_user_defined_winch
1799+
omit if Reline.core.io_gate.win?
1800+
pidfile = Tempfile.create('pidfile')
1801+
rubyfile = Tempfile.create('rubyfile')
1802+
rubyfile.write <<~RUBY
1803+
File.write(#{pidfile.path.inspect}, Process.pid)
1804+
winch_called = false
1805+
Signal.trap(:WINCH, ->(_arg){ winch_called = true })
1806+
p Reline.readline('>')
1807+
puts "winch: \#{winch_called}"
1808+
RUBY
1809+
rubyfile.close
1810+
1811+
start_terminal(10, 50, %W{ruby -I#{@pwd}/lib -rreline #{rubyfile.path}})
1812+
assert_screen(/^>/)
1813+
write 'a'
1814+
assert_screen(/^>a/)
1815+
pid = pidfile.tap(&:rewind).read.to_i
1816+
Process.kill(:WINCH, pid) unless pid.zero?
1817+
write "b\n"
1818+
assert_screen(/"ab"\nwinch: true/)
1819+
close
1820+
ensure
1821+
File.delete(rubyfile.path) if rubyfile
1822+
pidfile.close if pidfile
1823+
File.delete(pidfile.path) if pidfile
1824+
end
1825+
17981826
def test_stop_continue
17991827
omit if Reline.core.io_gate.win?
18001828
pidfile = Tempfile.create('pidfile')
18011829
rubyfile = Tempfile.create('rubyfile')
18021830
rubyfile.write <<~RUBY
18031831
File.write(#{pidfile.path.inspect}, Process.pid)
1804-
p Reline.readmultiline('>'){false}
1832+
cont_called = false
1833+
Signal.trap(:CONT, ->(_arg){ cont_called = true })
1834+
Reline.readmultiline('>'){|input| input.match?(/ghi/) }
1835+
puts "cont: \#{cont_called}"
18051836
RUBY
18061837
rubyfile.close
1807-
start_terminal(40, 50, ['bash'])
1838+
start_terminal(10, 50, ['bash'])
18081839
write "ruby -I#{@pwd}/lib -rreline #{rubyfile.path}\n"
18091840
assert_screen(/^>/)
18101841
write "abc\ndef\nhi"
@@ -1814,6 +1845,8 @@ def test_stop_continue
18141845
assert_screen(/fg\n.*>/m)
18151846
write "\ebg"
18161847
assert_screen(/>abc\n>def\n>ghi\n/)
1848+
write "\n"
1849+
assert_screen(/cont: true/)
18171850
close
18181851
ensure
18191852
File.delete(rubyfile.path) if rubyfile

0 commit comments

Comments
 (0)