From 682608025f9edd610b63f566600cce7bf1013231 Mon Sep 17 00:00:00 2001 From: Stephen Margheim Date: Sat, 10 Aug 2024 11:21:56 +0200 Subject: [PATCH] Allow composing user-defined WINCH signal traps Resolves https://github.com/ruby/reline/issues/735 Just as the `handle_interrupted` method runs Reline's interrupt hook logic and then runs any user-defined interrupt hook logic, `handle_resized` should run Reline's resize hook logic and then run any user-defined resize logic. This allows user CLI's with additional content in addition to the Reline prompt to ensure that additional content is rendered properly as the terminal is resized. Note: This is my best first attempt at this solution as someone who has only read the source code here today. There may be details/considerations that are important that I missed or didn't consider. I am very open to changes as necessary. cc: @st0012 --- lib/reline/line_editor.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 9c97415050..25c35f12ee 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -179,6 +179,14 @@ def handle_signal @rendered_screen.lines = [] @rendered_screen.cursor_y = 0 render_differential + case @old_winch_trap + when 'DEFAULT', 'SYSTEM_DEFAULT', 'IGNORE' + # Do nothing + when 'EXIT' + exit + else + @old_winch_trap.call if @old_winch_trap.respond_to?(:call) + end end private def handle_interrupted @@ -191,7 +199,7 @@ def handle_signal Reline::IOGate.move_cursor_column 0 @rendered_screen.lines = [] @rendered_screen.cursor_y = 0 - case @old_trap + case @old_int_trap when 'DEFAULT', 'SYSTEM_DEFAULT' raise Interrupt when 'IGNORE' @@ -199,21 +207,21 @@ def handle_signal when 'EXIT' exit else - @old_trap.call if @old_trap.respond_to?(:call) + @old_int_trap.call if @old_int_trap.respond_to?(:call) end end def set_signal_handlers - Reline::IOGate.set_winch_handler do + @old_winch_trap = Reline::IOGate.set_winch_handler do @resized = true end - @old_trap = Signal.trap('INT') do + @old_int_trap = Signal.trap('INT') do @interrupted = true end end def finalize - Signal.trap('INT', @old_trap) + Signal.trap('INT', @old_int_trap) end def eof?