Skip to content

Commit af08aec

Browse files
authored
Support grapheme cluster that has width >= 3 (#834)
1 parent 5a0d2c0 commit af08aec

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/reline/unicode.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def self.take_mbchar_range(str, start_col, width, cover_begin: false, cover_end:
220220
next
221221
elsif padding && !cover_begin && prev_width < start_col && start_col < total_width
222222
# Add preceding padding. This padding might have background color.
223-
chunk << ' '
223+
chunk << ' ' * (total_width - start_col)
224224
chunk_start_col ||= start_col
225225
chunk_end_col = total_width
226226
next
@@ -234,7 +234,7 @@ def self.take_mbchar_range(str, start_col, width, cover_begin: false, cover_end:
234234
# Current character exceeds end_col
235235
if padding && end_col < total_width
236236
# Add succeeding padding. This padding might have background color.
237-
chunk << ' '
237+
chunk << ' ' * (end_col - prev_width)
238238
chunk_start_col ||= prev_width
239239
chunk_end_col = end_col
240240
end

test/reline/test_unicode.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ def test_take_mbchar_range
107107
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)
108108
end
109109

110+
def test_three_width_characters_take_mbchar_range
111+
halfwidth_dakuten = 0xFF9E.chr('utf-8')
112+
a = 'あ' + halfwidth_dakuten
113+
b = 'い' + halfwidth_dakuten
114+
c = 'う' + halfwidth_dakuten
115+
line = 'x' + a + b + c + 'x'
116+
assert_equal [' ' + b + ' ', 2, 6], Reline::Unicode.take_mbchar_range(line, 2, 6, padding: true)
117+
assert_equal [' ' + b + ' ', 3, 6], Reline::Unicode.take_mbchar_range(line, 3, 6, padding: true)
118+
assert_equal [b + c, 4, 6], Reline::Unicode.take_mbchar_range(line, 4, 6, padding: true)
119+
assert_equal [a + b, 1, 6], Reline::Unicode.take_mbchar_range(line, 2, 6, cover_begin: true)
120+
assert_equal [a + b, 1, 6], Reline::Unicode.take_mbchar_range(line, 3, 6, cover_begin: true)
121+
assert_equal [b + c, 4, 6], Reline::Unicode.take_mbchar_range(line, 2, 6, cover_end: true)
122+
assert_equal [b + c, 4, 6], Reline::Unicode.take_mbchar_range(line, 3, 6, cover_end: true)
123+
end
124+
110125
def test_common_prefix
111126
assert_equal('', Reline::Unicode.common_prefix([]))
112127
assert_equal('abc', Reline::Unicode.common_prefix(['abc']))

0 commit comments

Comments
 (0)