Skip to content

Commit

Permalink
Fix Reline::Unicode.take_range as it was not fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Aug 31, 2021
1 parent f0e54f2 commit 5da6a8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConfigEncodingConversionError < StandardError; end

Key = Struct.new('Key', :char, :combined_char, :with_meta)
CursorPos = Struct.new(:x, :y)
DialogRenderInfo = Struct.new(:pos, :contents, :pointer, :bg_color, :height, keyword_init: true)
DialogRenderInfo = Struct.new(:pos, :contents, :pointer, :bg_color, :width, :height, keyword_init: true)

class Core
ATTR_READER_NAMES = %i(
Expand Down
42 changes: 25 additions & 17 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,22 +538,29 @@ def call
end

class Dialog
attr_reader :name, :contents, :contents_width
attr_reader :name, :contents, :width
attr_accessor :scroll_top, :column, :vertical_offset, :lines_backup

def initialize(name, proc_scope)
@name = name
@proc_scope = proc_scope
@width = nil
@scroll_top = 0
end

def set_cursor_pos(col, row)
@proc_scope.set_cursor_pos(col, row)
end

def width=(v)
@width = v
end

def contents=(contents)
@contents = contents
@contents_width = contents.map{ |line| Reline::Unicode.calculate_width(line, true) }.max if contents
if contents and @width.nil?
@width = contents.map{ |line| Reline::Unicode.calculate_width(line, true) }.max
end
end

def call
Expand Down Expand Up @@ -582,6 +589,7 @@ def add_dialog_proc(name, p, context = nil)
end
dialog.set_cursor_pos(cursor_column, @first_line_started_from + @started_from)
dialog_render_info = dialog.call
dialog.width = dialog_render_info.width if dialog_render_info.width
old_dialog = dialog.clone
if dialog_render_info and dialog_render_info.contents and not dialog_render_info.contents.empty?
height = dialog_render_info.height || DIALOG_HEIGHT
Expand Down Expand Up @@ -615,7 +623,7 @@ def add_dialog_proc(name, p, context = nil)
upper_space = @first_line_started_from - @started_from
lower_space = @highest_in_all - @first_line_started_from - @started_from - 1
dialog.column = dialog_render_info.pos.x
diff = (dialog.column + dialog.contents_width) - (@screen_size.last - 1)
diff = (dialog.column + dialog.width) - (@screen_size.last - 1)
if diff > 0
dialog.column -= diff
end
Expand Down Expand Up @@ -644,7 +652,7 @@ def add_dialog_proc(name, p, context = nil)
bg_color = '46'
end
end
@output.write "\e[#{bg_color}m%-#{dialog.contents_width}s\e[49m" % item.slice(0, dialog.contents_width)
@output.write "\e[#{bg_color}m%-#{dialog.width}s\e[49m" % item.slice(0, dialog.width)
Reline::IOGate.move_cursor_column(dialog.column)
move_cursor_down(1) if i < (dialog.contents.size - 1)
end
Expand Down Expand Up @@ -685,11 +693,11 @@ def add_dialog_proc(name, p, context = nil)
line_num.times do |i|
Reline::IOGate.move_cursor_column(old_dialog.column)
if visual_lines[start + i].nil?
s = ' ' * dialog.contents_width
s = ' ' * dialog.width
else
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column, dialog.contents_width)
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column, dialog.width)
end
@output.write "\e[39m\e[49m%-#{dialog.contents_width}s\e[39m\e[49m" % s
@output.write "\e[39m\e[49m%-#{dialog.width}s\e[39m\e[49m" % s
move_cursor_down(1) if i < (line_num - 1)
end
move_cursor_up(old_dialog.vertical_offset + line_num - 1 - y_diff)
Expand All @@ -702,11 +710,11 @@ def add_dialog_proc(name, p, context = nil)
line_num.times do |i|
Reline::IOGate.move_cursor_column(old_dialog.column)
if visual_lines[start + i].nil?
s = ' ' * dialog.contents_width
s = ' ' * dialog.width
else
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column, dialog.contents_width)
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column, dialog.width)
end
@output.write "\e[39m\e[49m%-#{dialog.contents_width}s\e[39m\e[49m" % s
@output.write "\e[39m\e[49m%-#{dialog.width}s\e[39m\e[49m" % s
move_cursor_down(1) if i < (line_num - 1)
end
move_cursor_up(dialog.vertical_offset + dialog.contents.size + line_num - 1 - y_diff)
Expand All @@ -729,20 +737,20 @@ def add_dialog_proc(name, p, context = nil)
end
move_cursor_up(old_dialog.vertical_offset + line_num - 1 - y_diff)
end
if (old_dialog.column + old_dialog.contents_width) > (dialog.column + dialog.contents_width)
if (old_dialog.column + old_dialog.width) > (dialog.column + dialog.width)
# rerender right
move_cursor_down(old_dialog.vertical_offset + y_diff)
width = (old_dialog.column + old_dialog.contents_width) - (dialog.column + dialog.contents_width)
width = (old_dialog.column + old_dialog.width) - (dialog.column + dialog.width)
start = visual_start + old_dialog.vertical_offset
line_num = old_dialog.contents.size
line_num.times do |i|
Reline::IOGate.move_cursor_column(old_dialog.column + dialog.contents_width)
Reline::IOGate.move_cursor_column(old_dialog.column + dialog.width)
if visual_lines[start + i].nil?
s = ' ' * width
else
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column + dialog.contents_width, width)
s = Reline::Unicode.take_range(visual_lines[start + i], old_dialog.column + dialog.width, width)
end
Reline::IOGate.move_cursor_column(dialog.column + dialog.contents_width)
Reline::IOGate.move_cursor_column(dialog.column + dialog.width)
@output.write "\e[39m\e[49m%-#{width}s\e[39m\e[49m" % s
move_cursor_down(1) if i < (line_num - 1)
end
Expand Down Expand Up @@ -780,10 +788,10 @@ def add_dialog_proc(name, p, context = nil)
dialog_vertical_size.times do |i|
if i < visual_lines_under_dialog.size
Reline::IOGate.move_cursor_column(0)
@output.write "\e[39m\e[49m%-#{dialog.contents_width}s\e[39m\e[49m" % visual_lines_under_dialog[i]
@output.write "\e[39m\e[49m%-#{dialog.width}s\e[39m\e[49m" % visual_lines_under_dialog[i]
else
Reline::IOGate.move_cursor_column(dialog.column)
@output.write "\e[39m\e[49m#{' ' * dialog.contents_width}\e[39m\e[49m"
@output.write "\e[39m\e[49m#{' ' * dialog.width}\e[39m\e[49m"
end
Reline::IOGate.erase_after_cursor
move_cursor_down(1) if i < (dialog_vertical_size - 1)
Expand Down
11 changes: 6 additions & 5 deletions lib/reline/unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ def self.split_by_width(str, max_width, encoding = str.encoding)
end

# Take a chunk of a String with escape sequences.
def self.take_range(str, col, length, encoding = str.encoding)
def self.take_range(str, start_col, max_width, encoding = str.encoding)
chunk = String.new(encoding: encoding)
width = 0
total_width = 0
rest = str.encode(Encoding::UTF_8)
in_zero_width = false
rest.scan(WIDTH_SCANNER) do |gc|
Expand All @@ -206,9 +206,10 @@ def self.take_range(str, col, length, encoding = str.encoding)
if in_zero_width
chunk << gc
else
width = get_mbchar_width(gc)
break if (width + length) <= col
chunk << gc if col <= width
mbchar_width = get_mbchar_width(gc)
total_width += mbchar_width
break if (start_col + max_width) < total_width
chunk << gc if start_col < total_width
end
end
end
Expand Down

0 comments on commit 5da6a8d

Please sign in to comment.