Skip to content

Commit ac6f652

Browse files
committed
Use DialogRenderInfo struct
1 parent 860be91 commit ac6f652

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

lib/reline.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ConfigEncodingConversionError < StandardError; end
1818

1919
Key = Struct.new('Key', :char, :combined_char, :with_meta)
2020
CursorPos = Struct.new(:x, :y)
21+
DialogRenderInfo = Struct.new(:pos, :contents, :pointer, :bg_color, keyword_init: true)
2122

2223
class Core
2324
ATTR_READER_NAMES = %i(
@@ -221,7 +222,7 @@ def get_screen_size
221222
context.clear
222223
context.push(cursor_pos_to_render, result, pointer)
223224
end
224-
[cursor_pos_to_render, result, pointer, nil]
225+
DialogRenderInfo.new(pos: cursor_pos_to_render, contents: result, pointer: pointer)
225226
}
226227
Reline::DEFAULT_DIALOG_CONTEXT = Array.new
227228

lib/reline/line_editor.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,12 @@ def add_dialog_proc(name, p, context = nil)
566566
return
567567
end
568568
dialog.set_cursor_pos(cursor_column, @first_line_started_from + @started_from)
569-
pos, result, pointer, bg = dialog.call
569+
dialog_render_info = dialog.call
570570
old_dialog_contents = dialog.contents
571571
old_dialog_column = dialog.column
572572
old_dialog_vertical_offset = dialog.vertical_offset
573-
if result and not result.empty?
574-
dialog.contents = result
573+
if dialog_render_info and dialog_render_info.contents and not dialog_render_info.contents.empty?
574+
dialog.contents = dialog_render_info.contents
575575
dialog.contents = dialog.contents[0...DIALOG_HEIGHT] if dialog.contents.size > DIALOG_HEIGHT
576576
else
577577
dialog.lines_backup = {
@@ -587,32 +587,32 @@ def add_dialog_proc(name, p, context = nil)
587587
end
588588
upper_space = @first_line_started_from - @started_from
589589
lower_space = @highest_in_all - @first_line_started_from - @started_from - 1
590-
dialog.column = pos.x
590+
dialog.column = dialog_render_info.pos.x
591591
diff = (dialog.column + DIALOG_WIDTH) - (@screen_size.last - 1)
592592
if diff > 0
593593
dialog.column -= diff
594594
end
595-
if (lower_space + @rest_height - pos.y) >= DIALOG_HEIGHT
596-
dialog.vertical_offset = pos.y + 1
595+
if (lower_space + @rest_height - dialog_render_info.pos.y) >= DIALOG_HEIGHT
596+
dialog.vertical_offset = dialog_render_info.pos.y + 1
597597
elsif upper_space >= DIALOG_HEIGHT
598-
dialog.vertical_offset = pos.y + -(DIALOG_HEIGHT + 1)
598+
dialog.vertical_offset = dialog_render_info.pos.y + -(DIALOG_HEIGHT + 1)
599599
else
600-
if (lower_space + @rest_height - pos.y) < DIALOG_HEIGHT
601-
scroll_down(DIALOG_HEIGHT + pos.y)
602-
move_cursor_up(DIALOG_HEIGHT + pos.y)
600+
if (lower_space + @rest_height - dialog_render_info.pos.y) < DIALOG_HEIGHT
601+
scroll_down(DIALOG_HEIGHT + dialog_render_info.pos.y)
602+
move_cursor_up(DIALOG_HEIGHT + dialog_render_info.pos.y)
603603
end
604-
dialog.vertical_offset = pos.y + 1
604+
dialog.vertical_offset = dialog_render_info.pos.y + 1
605605
end
606606
Reline::IOGate.hide_cursor
607607
reset_dialog(dialog, old_dialog_contents, old_dialog_column, old_dialog_vertical_offset)
608608
move_cursor_down(dialog.vertical_offset)
609609
Reline::IOGate.move_cursor_column(dialog.column)
610610
dialog.contents.each_with_index do |item, i|
611-
if i == pointer
611+
if i == dialog_render_info.pointer
612612
bg_color = '45'
613613
else
614-
if bg
615-
bg_color = bg
614+
if dialog_render_info.bg_color
615+
bg_color = dialog_render_info.bg_color
616616
else
617617
bg_color = '46'
618618
end

0 commit comments

Comments
 (0)