We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9bc2dbd commit 60ca800Copy full SHA for 60ca800
lib/reline/unicode.rb
@@ -160,24 +160,29 @@ def self.split_by_width(str, max_width, encoding = str.encoding)
160
width = 0
161
rest = str.encode(Encoding::UTF_8)
162
in_zero_width = false
163
+ seq = String.new(encoding: encoding)
164
rest.scan(WIDTH_SCANNER) do |gc|
165
case
166
when gc[NON_PRINTING_START_INDEX]
167
in_zero_width = true
168
+ lines.last << NON_PRINTING_START
169
when gc[NON_PRINTING_END_INDEX]
170
171
+ lines.last << NON_PRINTING_END
172
when gc[CSI_REGEXP_INDEX]
173
lines.last << gc[CSI_REGEXP_INDEX]
174
+ seq << gc[CSI_REGEXP_INDEX]
175
when gc[OSC_REGEXP_INDEX]
176
lines.last << gc[OSC_REGEXP_INDEX]
177
+ seq << gc[OSC_REGEXP_INDEX]
178
when gc[GRAPHEME_CLUSTER_INDEX]
179
gc = gc[GRAPHEME_CLUSTER_INDEX]
180
unless in_zero_width
181
mbchar_width = get_mbchar_width(gc)
182
if (width += mbchar_width) > max_width
183
width = mbchar_width
184
lines << nil
- lines << String.new(encoding: encoding)
185
+ lines << seq.dup
186
height += 1
187
end
188
test/reline/test_unicode.rb
@@ -18,6 +18,15 @@ def test_ambiguous_width
18
assert_equal 2, Reline::Unicode.calculate_width('√', true)
19
20
21
+ def test_split_by_width
22
+ assert_equal [['abc', nil, 'de'], 2], Reline::Unicode.split_by_width('abcde', 3)
23
+ assert_equal [['abc', nil, 'def', nil, ''], 3], Reline::Unicode.split_by_width('abcdef', 3)
24
+ assert_equal [['ab', nil, 'あd', nil, 'ef'], 3], Reline::Unicode.split_by_width('abあdef', 3)
25
+ assert_equal [["ab\1zero\2c", nil, 'def', nil, ''], 3], Reline::Unicode.split_by_width("ab\1zero\2cdef", 3)
26
+ assert_equal [["\e[31mabc", nil, "\e[31md\e[42mef", nil, "\e[31m\e[42mg"], 3], Reline::Unicode.split_by_width("\e[31mabcd\e[42mefg", 3)
27
+ assert_equal [["ab\e]0;1\ac", nil, "\e]0;1\ad"], 2], Reline::Unicode.split_by_width("ab\e]0;1\acd", 3)
28
+ end
29
+
30
def test_take_range
31
assert_equal 'cdef', Reline::Unicode.take_range('abcdefghi', 2, 4)
32
assert_equal 'いう', Reline::Unicode.take_range('あいうえお', 2, 4)
0 commit comments