Skip to content

Commit 29714df

Browse files
authored
Completely support full-width characters in differential rendering (#654)
* Add a cut variation of Reline::Unicode.take_range method take_mbchar_range * Consider fullwidth take_range in differential rendering
1 parent bed5fb3 commit 29714df

4 files changed

Lines changed: 113 additions & 17 deletions

File tree

β€Žlib/reline/line_editor.rbβ€Ž

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,13 @@ def render_line_differential(old_items, new_items)
414414
@output.write "#{Reline::IOGate::RESET_COLOR}#{' ' * width}"
415415
else
416416
x, w, content = new_items[level]
417-
content = Reline::Unicode.take_range(content, base_x - x, width) unless x == base_x && w == width
418-
Reline::IOGate.move_cursor_column base_x
417+
cover_begin = base_x != 0 && new_levels[base_x - 1] == level
418+
cover_end = new_levels[base_x + width] == level
419+
pos = 0
420+
unless x == base_x && w == width
421+
content, pos = Reline::Unicode.take_mbchar_range(content, base_x - x, width, cover_begin: cover_begin, cover_end: cover_end, padding: true)
422+
end
423+
Reline::IOGate.move_cursor_column x + pos
419424
@output.write "#{Reline::IOGate::RESET_COLOR}#{content}#{Reline::IOGate::RESET_COLOR}"
420425
end
421426
base_x += width
@@ -699,13 +704,6 @@ def add_dialog_proc(name, p, context = nil)
699704

700705
DIALOG_DEFAULT_HEIGHT = 20
701706

702-
private def padding_space_with_escape_sequences(str, width)
703-
padding_width = width - calculate_width(str, true)
704-
# padding_width should be only positive value. But macOS and Alacritty returns negative value.
705-
padding_width = 0 if padding_width < 0
706-
str + (' ' * padding_width)
707-
end
708-
709707
private def dialog_range(dialog, dialog_y)
710708
x_range = dialog.column...dialog.column + dialog.width
711709
y_range = dialog_y + dialog.vertical_offset...dialog_y + dialog.vertical_offset + dialog.contents.size
@@ -778,7 +776,7 @@ def add_dialog_proc(name, p, context = nil)
778776
dialog.contents = contents.map.with_index do |item, i|
779777
line_sgr = i == pointer ? enhanced_sgr : default_sgr
780778
str_width = dialog.width - (scrollbar_pos.nil? ? 0 : @block_elem_width)
781-
str = padding_space_with_escape_sequences(Reline::Unicode.take_range(item, 0, str_width), str_width)
779+
str, = Reline::Unicode.take_mbchar_range(item, 0, str_width, padding: true)
782780
colored_content = "#{line_sgr}#{str}"
783781
if scrollbar_pos
784782
if scrollbar_pos <= (i * 2) and (i * 2 + 1) < (scrollbar_pos + bar_height)

β€Žlib/reline/unicode.rbβ€Ž

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,32 +179,78 @@ def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0)
179179

180180
# Take a chunk of a String cut by width with escape sequences.
181181
def self.take_range(str, start_col, max_width)
182+
take_mbchar_range(str, start_col, max_width).first
183+
end
184+
185+
def self.take_mbchar_range(str, start_col, width, cover_begin: false, cover_end: false, padding: false)
182186
chunk = String.new(encoding: str.encoding)
187+
188+
end_col = start_col + width
183189
total_width = 0
184190
rest = str.encode(Encoding::UTF_8)
185191
in_zero_width = false
192+
chunk_start_col = nil
193+
chunk_end_col = nil
194+
has_csi = false
186195
rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc|
187196
case
188197
when non_printing_start
189198
in_zero_width = true
199+
chunk << NON_PRINTING_START
190200
when non_printing_end
191201
in_zero_width = false
202+
chunk << NON_PRINTING_END
192203
when csi
204+
has_csi = true
193205
chunk << csi
194206
when osc
195207
chunk << osc
196208
when gc
197209
if in_zero_width
198210
chunk << gc
211+
next
212+
end
213+
214+
mbchar_width = get_mbchar_width(gc)
215+
prev_width = total_width
216+
total_width += mbchar_width
217+
218+
if (cover_begin || padding ? total_width <= start_col : prev_width < start_col)
219+
# Current character haven't reached start_col yet
220+
next
221+
elsif padding && !cover_begin && prev_width < start_col && start_col < total_width
222+
# Add preceding padding. This padding might have background color.
223+
chunk << ' '
224+
chunk_start_col ||= start_col
225+
chunk_end_col = total_width
226+
next
227+
elsif (cover_end ? prev_width < end_col : total_width <= end_col)
228+
# Current character is in the range
229+
chunk << gc
230+
chunk_start_col ||= prev_width
231+
chunk_end_col = total_width
232+
break if total_width >= end_col
199233
else
200-
mbchar_width = get_mbchar_width(gc)
201-
total_width += mbchar_width
202-
break if (start_col + max_width) < total_width
203-
chunk << gc if start_col < total_width
234+
# Current character exceeds end_col
235+
if padding && end_col < total_width
236+
# Add succeeding padding. This padding might have background color.
237+
chunk << ' '
238+
chunk_start_col ||= prev_width
239+
chunk_end_col = end_col
240+
end
241+
break
204242
end
205243
end
206244
end
207-
chunk
245+
chunk_start_col ||= start_col
246+
chunk_end_col ||= start_col
247+
if padding && chunk_end_col < end_col
248+
# Append padding. This padding should not include background color.
249+
chunk << "\e[0m" if has_csi
250+
chunk << ' ' * (end_col - chunk_end_col)
251+
chunk_end_col = end_col
252+
end
253+
[chunk, chunk_start_col, chunk_end_col - chunk_start_col]
208254
end
209255

210256
def self.get_next_mbchar_size(line, byte_pointer)

β€Žtest/reline/test_line_editor.rbβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ def test_dialog_move
112112
end
113113
end
114114

115+
def test_multibyte
116+
base = [0, 12, 'δΈ€δΊŒδΈ‰δΈ€δΊŒδΈ‰']
117+
left = [0, 3, 'LLL']
118+
right = [9, 3, 'RRR']
119+
front = [3, 6, 'FFFFFF']
120+
# δΈ€ FFFFFF δΈ‰
121+
# δΈ€δΊŒδΈ‰δΈ€δΊŒδΈ‰
122+
assert_output '[COL_2]δΊŒδΈ‰δΈ€δΊŒ' do
123+
@line_editor.render_line_differential([base, front], [base, nil])
124+
end
125+
126+
# LLLFFFFFF δΈ‰
127+
# LLL δΈ‰δΈ€δΊŒδΈ‰
128+
assert_output '[COL_3] δΈ‰δΈ€δΊŒ' do
129+
@line_editor.render_line_differential([base, left, front], [base, left, nil])
130+
end
131+
132+
# δΈ€ FFFFFFRRR
133+
# δΈ€δΊŒδΈ‰δΈ€ RRR
134+
assert_output '[COL_2]δΊŒδΈ‰δΈ€ ' do
135+
@line_editor.render_line_differential([base, right, front], [base, right, nil])
136+
end
137+
138+
# LLLFFFFFFRRR
139+
# LLL δΈ‰δΈ€ RRR
140+
assert_output '[COL_3] δΈ‰δΈ€ ' do
141+
@line_editor.render_line_differential([base, left, right, front], [base, left, right, nil])
142+
end
143+
end
144+
115145
def test_complicated
116146
state_a = [nil, [19, 7, 'bbbbbbb'], [15, 8, 'cccccccc'], [10, 5, 'ddddd'], [18, 4, 'eeee'], [1, 3, 'fff'], [17, 2, 'gg'], [7, 1, 'h']]
117147
state_b = [[5, 9, 'aaaaaaaaa'], nil, [15, 8, 'cccccccc'], nil, [18, 4, 'EEEE'], [25, 4, 'ffff'], [17, 2, 'gg'], [2, 2, 'hh']]

β€Žtest/reline/test_unicode.rbβ€Ž

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def test_split_by_width_csi_reset_sgr_optimization
4646
def test_take_range
4747
assert_equal 'cdef', Reline::Unicode.take_range('abcdefghi', 2, 4)
4848
assert_equal 'あde', Reline::Unicode.take_range('abあdef', 2, 4)
49-
assert_equal 'zerocdef', Reline::Unicode.take_range("ab\1zero\2cdef", 2, 4)
50-
assert_equal 'bzerocde', Reline::Unicode.take_range("ab\1zero\2cdef", 1, 4)
49+
assert_equal "\1zero\2cdef", Reline::Unicode.take_range("ab\1zero\2cdef", 2, 4)
50+
assert_equal "b\1zero\2cde", Reline::Unicode.take_range("ab\1zero\2cdef", 1, 4)
5151
assert_equal "\e[31mcd\e[42mef", Reline::Unicode.take_range("\e[31mabcd\e[42mefg", 2, 4)
5252
assert_equal "\e]0;1\acd", Reline::Unicode.take_range("ab\e]0;1\acd", 2, 3)
5353
assert_equal 'いう', Reline::Unicode.take_range('γ‚γ„γ†γˆγŠ', 2, 4)
@@ -67,4 +67,26 @@ def test_calculate_width
6767
assert_equal 10, Reline::Unicode.calculate_width('γ‚γ„γ†γˆγŠ')
6868
assert_equal 10, Reline::Unicode.calculate_width('γ‚γ„γ†γˆγŠ', true)
6969
end
70+
71+
def test_take_mbchar_range
72+
assert_equal ['cdef', 2, 4], Reline::Unicode.take_mbchar_range('abcdefghi', 2, 4)
73+
assert_equal ['cdef', 2, 4], Reline::Unicode.take_mbchar_range('abcdefghi', 2, 4, padding: true)
74+
assert_equal ['cdef', 2, 4], Reline::Unicode.take_mbchar_range('abcdefghi', 2, 4, cover_begin: true)
75+
assert_equal ['cdef', 2, 4], Reline::Unicode.take_mbchar_range('abcdefghi', 2, 4, cover_end: true)
76+
assert_equal ['いう', 2, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 2, 4)
77+
assert_equal ['いう', 2, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 2, 4, padding: true)
78+
assert_equal ['いう', 2, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 2, 4, cover_begin: true)
79+
assert_equal ['いう', 2, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 2, 4, cover_end: true)
80+
assert_equal ['う', 4, 2], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4)
81+
assert_equal [' う ', 3, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4, padding: true)
82+
assert_equal ['いう', 2, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4, cover_begin: true)
83+
assert_equal ['γ†γˆ', 4, 4], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4, cover_end: true)
84+
assert_equal ['いう ', 2, 5], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4, cover_begin: true, padding: true)
85+
assert_equal [' γ†γˆ', 3, 5], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 4, cover_end: true, padding: true)
86+
assert_equal [' γ†γˆγŠ ', 3, 10], Reline::Unicode.take_mbchar_range('γ‚γ„γ†γˆγŠ', 3, 10, padding: true)
87+
assert_equal [" \e[41mγ†γˆγŠ\e[0m ", 3, 10], Reline::Unicode.take_mbchar_range("あい\e[41mγ†γˆγŠ", 3, 10, padding: true)
88+
assert_equal ["\e[41m \e[42mい\e[43m ", 1, 4], Reline::Unicode.take_mbchar_range("\e[41mあ\e[42mい\e[43mう", 1, 4, padding: true)
89+
assert_equal ["\e[31mc\1ABC\2d\e[0mef", 2, 4], Reline::Unicode.take_mbchar_range("\e[31mabc\1ABC\2d\e[0mefghi", 2, 4)
90+
assert_equal ["\e[41m \e[42mい\e[43m ", 1, 4], Reline::Unicode.take_mbchar_range("\e[41mあ\e[42mい\e[43mう", 1, 4, padding: true)
91+
end
7092
end

0 commit comments

Comments
Β (0)