Skip to content

Commit 2c47bd3

Browse files
authored
Merge pull request #358 from tmtm/simplify_sigwinch_handler
Simplify SIGWINCH handler to avoid aborting when resizing.
2 parents ebbcbd1 + 481add0 commit 2c47bd3

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

lib/reline/ansi.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def self.inner_getc
131131
unless @@buf.empty?
132132
return @@buf.shift
133133
end
134-
until c = @@input.raw(intr: true, &:getbyte)
135-
sleep 0.1
134+
until c = @@input.raw(intr: true) { select([@@input], [], [], 0.1) && @@input.getbyte }
135+
Reline.core.line_editor.resize
136136
end
137137
(c == 0x16 && @@input.raw(min: 0, tim: 0, &:getbyte)) || c
138138
rescue Errno::EIO

lib/reline/line_editor.rb

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,43 +178,49 @@ def reset(prompt = '', encoding:)
178178
rescue ArgumentError
179179
end
180180
Reline::IOGate.set_winch_handler do
181-
@rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
182-
old_screen_size = @screen_size
183-
@screen_size = Reline::IOGate.get_screen_size
184-
@screen_height = @screen_size.first
185-
if old_screen_size.last < @screen_size.last # columns increase
186-
@rerender_all = true
187-
rerender
188-
else
189-
back = 0
190-
new_buffer = whole_lines
191-
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
192-
new_buffer.each_with_index do |line, index|
193-
prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
194-
width = prompt_width + calculate_width(line)
195-
height = calculate_height_by_width(width)
196-
back += height
197-
end
198-
@highest_in_all = back
199-
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
200-
@first_line_started_from =
201-
if @line_index.zero?
202-
0
203-
else
204-
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
205-
end
206-
if @prompt_proc
207-
prompt = prompt_list[@line_index]
208-
prompt_width = calculate_width(prompt, true)
181+
@resized = true
182+
end
183+
@block_elem_width = Reline::Unicode.calculate_width('█')
184+
end
185+
186+
def resize
187+
return unless @resized
188+
@resized = false
189+
@rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
190+
old_screen_size = @screen_size
191+
@screen_size = Reline::IOGate.get_screen_size
192+
@screen_height = @screen_size.first
193+
if old_screen_size.last < @screen_size.last # columns increase
194+
@rerender_all = true
195+
rerender
196+
else
197+
back = 0
198+
new_buffer = whole_lines
199+
prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
200+
new_buffer.each_with_index do |line, index|
201+
prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
202+
width = prompt_width + calculate_width(line)
203+
height = calculate_height_by_width(width)
204+
back += height
205+
end
206+
@highest_in_all = back
207+
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
208+
@first_line_started_from =
209+
if @line_index.zero?
210+
0
211+
else
212+
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
209213
end
210-
calculate_nearest_cursor
211-
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
212-
Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
213-
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
214-
@rerender_all = true
214+
if @prompt_proc
215+
prompt = prompt_list[@line_index]
216+
prompt_width = calculate_width(prompt, true)
215217
end
218+
calculate_nearest_cursor
219+
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
220+
Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
221+
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
222+
@rerender_all = true
216223
end
217-
@block_elem_width = Reline::Unicode.calculate_width('█')
218224
end
219225

220226
def finalize
@@ -264,6 +270,7 @@ def reset_variables(prompt = '', encoding:)
264270
@auto_indent_proc = nil
265271
@dialogs = []
266272
@last_key = nil
273+
@resized = false
267274
reset_line
268275
end
269276

0 commit comments

Comments
 (0)