Skip to content

Commit d288fe0

Browse files
committed
Fix take_range with padding to have colored background padding
1 parent 4426d62 commit d288fe0

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

lib/reline/unicode.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ def self.take_range(str, start_col, width, encoding: str.encoding, cover_begin:
223223
total_width += mbchar_width
224224
break if !cover_end && total_width > start_col + width
225225
if cover_begin ? start_col < total_width : start_col <= prev_width
226+
if padding && chunk_start_col.nil? && start_col < prev_width
227+
chunk << ' ' * (prev_width - start_col)
228+
chunk_start_col = start_col
229+
end
226230
chunk << gc
227231
chunk_start_col ||= prev_width
228232
chunk_end_col = total_width
@@ -233,15 +237,9 @@ def self.take_range(str, start_col, width, encoding: str.encoding, cover_begin:
233237
end
234238
chunk_start_col ||= start_col
235239
chunk_end_col ||= start_col
236-
if padding
237-
if start_col < chunk_start_col
238-
chunk = ' ' * (chunk_start_col - start_col) + chunk
239-
chunk_start_col = start_col
240-
end
241-
if chunk_end_col < start_col + width
242-
chunk << ' ' * (start_col + width - chunk_end_col)
243-
chunk_end_col = start_col + width
244-
end
240+
if padding && chunk_end_col < start_col + width
241+
chunk << ' ' * (start_col + width - chunk_end_col)
242+
chunk_end_col = start_col + width
245243
end
246244
[chunk, chunk_start_col, chunk_end_col - chunk_start_col]
247245
end

test/reline/test_unicode.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ def test_take_range
3535
assert_equal [' うえ', 3, 5], Reline::Unicode.take_range('あいうえお', 3, 4, cover_end: true, padding: true)
3636
assert_equal [' うえお ', 3, 10], Reline::Unicode.take_range('あいうえお', 3, 10, padding: true)
3737
assert_equal ["\e[31mc\1ABC\2d\e[0mef", 2, 4], Reline::Unicode.take_range("\e[31mabc\1ABC\2d\e[0mefghi", 2, 4)
38+
assert_equal ["\e[47m い ", 1, 4], Reline::Unicode.take_range("\e[47mあいうえお\e[0m", 1, 4, padding: true)
3839
end
3940
end

0 commit comments

Comments
 (0)