fix(float): win_get_bordertext_col returning negative column number (#25752)

Problem:
  `win_get_bordertext_col` returns column < 1 for right or center
  aligned text, if its length is more than window width.

Solution:
  Return max(resulting_column, 1)
This commit is contained in:
nwounkn 2023-10-26 08:44:28 +05:00 committed by GitHub
parent f2fc44550f
commit 9de157bce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -735,9 +735,9 @@ int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align)
case kAlignLeft:
return 1;
case kAlignCenter:
return (total_col - text_width) / 2 + 1;
return MAX((total_col - text_width) / 2 + 1, 1);
case kAlignRight:
return total_col - text_width + 1;
return MAX(total_col - text_width + 1, 1);
}
UNREACHABLE;
}

View File

@ -2034,6 +2034,61 @@ describe('float window', function()
eq('center', footer_pos)
end)
it('center aligned title longer than window width #25746', function()
local buf = meths.create_buf(false, false)
meths.buf_set_lines(buf, 0, -1, true, {' halloj! ',
' BORDAA '})
local win = meths.open_win(buf, false, {
relative='editor', width=9, height=2, row=2, col=5, border="double",
title = "abcdefghijklmnopqrstuvwxyz",title_pos = "center",
})
if multigrid then
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4
{5:}{11:abcdefghi}{5:}|
{5:}{1: halloj! }{5:}|
{5:}{1: BORDAA }{5:}|
{5:}|
]], float_pos={
[4] = { { id = 1001 }, "NW", 1, 2, 5, true }
}, win_viewport={
[2] = {win = {id = 1000}, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 1, sum_scroll_delta = 0};
[4] = {win = {id = 1001}, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 2, sum_scroll_delta = 0};
}}
else
screen:expect{grid=[[
^ |
{0:~ }|
{0:~ }{5:}{11:abcdefghi}{5:}{0: }|
{0:~ }{5:}{1: halloj! }{5:}{0: }|
{0:~ }{5:}{1: BORDAA }{5:}{0: }|
{0:~ }{5:}{0: }|
|
]]}
end
meths.win_close(win, false)
assert_alive()
end)
it('border with title', function()
local buf = meths.create_buf(false, false)
meths.buf_set_lines(buf, 0, -1, true, {' halloj! ',