Skip to content

Commit

Permalink
Fix take_range with padding to have colored background padding
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jan 2, 2023
1 parent 4426d62 commit d288fe0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/reline/unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ def self.take_range(str, start_col, width, encoding: str.encoding, cover_begin:
total_width += mbchar_width
break if !cover_end && total_width > start_col + width
if cover_begin ? start_col < total_width : start_col <= prev_width
if padding && chunk_start_col.nil? && start_col < prev_width
chunk << ' ' * (prev_width - start_col)
chunk_start_col = start_col
end
chunk << gc
chunk_start_col ||= prev_width
chunk_end_col = total_width
Expand All @@ -233,15 +237,9 @@ def self.take_range(str, start_col, width, encoding: str.encoding, cover_begin:
end
chunk_start_col ||= start_col
chunk_end_col ||= start_col
if padding
if start_col < chunk_start_col
chunk = ' ' * (chunk_start_col - start_col) + chunk
chunk_start_col = start_col
end
if chunk_end_col < start_col + width
chunk << ' ' * (start_col + width - chunk_end_col)
chunk_end_col = start_col + width
end
if padding && chunk_end_col < start_col + width
chunk << ' ' * (start_col + width - chunk_end_col)
chunk_end_col = start_col + width
end
[chunk, chunk_start_col, chunk_end_col - chunk_start_col]
end
Expand Down
1 change: 1 addition & 0 deletions test/reline/test_unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ def test_take_range
assert_equal [' うえ', 3, 5], Reline::Unicode.take_range('あいうえお', 3, 4, cover_end: true, padding: true)
assert_equal [' うえお ', 3, 10], Reline::Unicode.take_range('あいうえお', 3, 10, padding: true)
assert_equal ["\e[31mc\1ABC\2d\e[0mef", 2, 4], Reline::Unicode.take_range("\e[31mabc\1ABC\2d\e[0mefghi", 2, 4)
assert_equal ["\e[47m い ", 1, 4], Reline::Unicode.take_range("\e[47mあいうえお\e[0m", 1, 4, padding: true)
end
end

0 comments on commit d288fe0

Please sign in to comment.