Skip to content

Commit 0c8d3c8

Browse files
authoredApr 24, 2024
Long line performance (#688)
* Improve C-e (ed_move_to_end) performance for long line * Reline::Unicode.split_by_width optimization for RESET_SGR
1 parent e9d5236 commit 0c8d3c8

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed
 

‎lib/reline/line_editor.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,11 +1542,7 @@ def finish
15421542
alias_method :vi_zero, :ed_move_to_beg
15431543

15441544
private def ed_move_to_end(key)
1545-
@byte_pointer = 0
1546-
while @byte_pointer < current_line.bytesize
1547-
byte_size = Reline::Unicode.get_next_mbchar_size(current_line, @byte_pointer)
1548-
@byte_pointer += byte_size
1549-
end
1545+
@byte_pointer = current_line.bytesize
15501546
end
15511547
alias_method :end_of_line, :ed_move_to_end
15521548

‎lib/reline/unicode.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0)
145145
lines.last << NON_PRINTING_END
146146
when csi
147147
lines.last << csi
148-
seq << csi
148+
unless in_zero_width
149+
if csi == -"\e[m" || csi == -"\e[0m"
150+
seq.clear
151+
else
152+
seq << csi
153+
end
154+
end
149155
when osc
150156
lines.last << osc
151157
seq << osc

‎test/reline/test_unicode.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def test_split_by_width
3838
assert_equal [["ab\e]0;1\ac", nil, "\e]0;1\ad"], 2], Reline::Unicode.split_by_width("ab\e]0;1\acd", 3)
3939
end
4040

41+
def test_split_by_width_csi_reset_sgr_optimization
42+
assert_equal [["\e[1ma\e[mb\e[2mc", nil, "\e[2md\e[0me\e[3mf", nil, "\e[3mg"], 3], Reline::Unicode.split_by_width("\e[1ma\e[mb\e[2mcd\e[0me\e[3mfg", 3)
43+
assert_equal [["\e[1ma\1\e[mzero\e[0m\2\e[2mb", nil, "\e[1m\e[2mc"], 2], Reline::Unicode.split_by_width("\e[1ma\1\e[mzero\e[0m\2\e[2mbc", 2)
44+
end
45+
4146
def test_take_range
4247
assert_equal 'cdef', Reline::Unicode.take_range('abcdefghi', 2, 4)
4348
assert_equal 'あde', Reline::Unicode.take_range('abあdef', 2, 4)

0 commit comments

Comments
 (0)