Skip to content

Commit

Permalink
[ruby/reline] Long line performance
Browse files Browse the repository at this point in the history
(ruby/reline#688)

* Improve C-e (ed_move_to_end) performance for long line

* Reline::Unicode.split_by_width optimization for RESET_SGR

ruby/reline@0c8d3c827a
  • Loading branch information
tompng authored and matzbot committed Apr 24, 2024
1 parent 5c32a15 commit cf24a04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 1 addition & 5 deletions lib/reline/line_editor.rb
Expand Up @@ -1542,11 +1542,7 @@ def finish
alias_method :vi_zero, :ed_move_to_beg

private def ed_move_to_end(key)
@byte_pointer = 0
while @byte_pointer < current_line.bytesize
byte_size = Reline::Unicode.get_next_mbchar_size(current_line, @byte_pointer)
@byte_pointer += byte_size
end
@byte_pointer = current_line.bytesize
end
alias_method :end_of_line, :ed_move_to_end

Expand Down
8 changes: 7 additions & 1 deletion lib/reline/unicode.rb
Expand Up @@ -145,7 +145,13 @@ def self.split_by_width(str, max_width, encoding = str.encoding, offset: 0)
lines.last << NON_PRINTING_END
when csi
lines.last << csi
seq << csi
unless in_zero_width
if csi == -"\e[m" || csi == -"\e[0m"
seq.clear
else
seq << csi
end
end
when osc
lines.last << osc
seq << osc
Expand Down
5 changes: 5 additions & 0 deletions test/reline/test_unicode.rb
Expand Up @@ -38,6 +38,11 @@ def test_split_by_width
assert_equal [["ab\e]0;1\ac", nil, "\e]0;1\ad"], 2], Reline::Unicode.split_by_width("ab\e]0;1\acd", 3)
end

def test_split_by_width_csi_reset_sgr_optimization
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)
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)
end

def test_take_range
assert_equal 'cdef', Reline::Unicode.take_range('abcdefghi', 2, 4)
assert_equal 'あde', Reline::Unicode.take_range('abあdef', 2, 4)
Expand Down

0 comments on commit cf24a04

Please sign in to comment.