Skip to content

Commit

Permalink
fix(float): tail truncate the long title
Browse files Browse the repository at this point in the history
Problem: now we truncate the title from start,This is somewhat inconsistent with the way vim works. Usually we use < to truncate and keep more trailing characters.

Solution: keep more trailing characters.
  • Loading branch information
glepnir committed Apr 22, 2024
1 parent 96d0c70 commit 1ae21ce
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
36 changes: 34 additions & 2 deletions src/nvim/drawscreen.c
Expand Up @@ -92,6 +92,7 @@
#include "nvim/match.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/move.h"
#include "nvim/normal.h"
Expand Down Expand Up @@ -717,18 +718,49 @@ void end_search_hl(void)

static void win_redr_bordertext(win_T *wp, VirtText vt, int col)
{
int used_col = col;
int width = wp->w_config.width;
bool truncate = false;
char *new_text = NULL;

for (size_t i = 0; i < kv_size(vt);) {
int attr = 0;
char *text = next_virt_text_chunk(vt, &i, &attr);
if (text == NULL) {
break;
}
attr = hl_apply_winblend(wp, attr);
col += grid_line_puts(col, text, -1, attr);
int cell = (int)mb_string2cells(text);
int shift = used_col + cell - width;

if (shift > 1) {
truncate = true;
if (used_col > col) {
int move_start = col + shift - 1;
int start_pos = col;
for (int j = move_start; j < used_col; j++) {
grid_line_put_schar(start_pos, linebuf_char[j], linebuf_attr[j]);
start_pos++;
}
used_col -= (shift - 1);
} else {
new_text = xmemdupz(text + shift - 1, strlen(text) - (size_t)shift + 1);
}
}

used_col += grid_line_puts(used_col, new_text ? new_text : text, -1, attr);
if (new_text) {
xfree(new_text);
new_text = NULL;
}
}

if (truncate) {
grid_line_put_schar(col, schar_from_ascii('<'), linebuf_attr[col]);
}
}

int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align)
static int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align)
{
switch (align) {
case kAlignLeft:
Expand Down
58 changes: 45 additions & 13 deletions test/functional/ui/float_spec.lua
Expand Up @@ -1935,15 +1935,26 @@ describe('float window', function()
eq('center', footer_pos)
end)

it('center aligned title longer than window width #25746', function()
it('truncates title longer than window width #23602', function()
local buf = api.nvim_create_buf(false, false)
api.nvim_buf_set_lines(buf, 0, -1, true, {' halloj! ',
' BORDAA '})
local win = api.nvim_open_win(buf, false, {
local config = {
relative='editor', width=9, height=2, row=2, col=5, border="double",
title = "abcdefghijklmnopqrstuvwxyz",title_pos = "center",
})
}

local expected = [[
^ |
{0:~ }|
{0:~ }{5:╔}{11:<stuvwxyz}{5:╗}{0: }|
{0:~ }{5:║}{1: halloj! }{5:║}{0: }|
{0:~ }{5:║}{1: BORDAA }{5:║}{0: }|
{0:~ }{5:╚═════════╝}{0: }|
|
]]

local win = api.nvim_open_win(buf, false, config)
if multigrid then
screen:expect{grid=[[
## grid 1
Expand All @@ -1955,7 +1966,7 @@ describe('float window', function()
## grid 3
|
## grid 4
{5:╔}{11:abcdefghi}{5:╗}|
{5:╔}{11:<stuvwxyz}{5:╗}|
{5:║}{1: halloj! }{5:║}|
{5:║}{1: BORDAA }{5:║}|
{5:╚═════════╝}|
Expand All @@ -1966,17 +1977,38 @@ describe('float window', function()
[4] = {win = 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: }|
|
]]}
screen:expect{grid= expected}
end
api.nvim_win_close(win, false)
assert_alive()

config.title = { {'abcd', 'FloatTitle'}, {'stuvw', 'FloatTitle'}, {'xyz', 'FloatTitle'}}
win = api.nvim_open_win(buf, false, config)

if multigrid then
screen:expect({grid = [[
## grid 1
[2:----------------------------------------]|*6
[3:----------------------------------------]|
## grid 2
^ |
{0:~ }|*5
## grid 3
|
## grid 5
{5:╔}{11:<stuvwxyz}{5:╗}|
{5:║}{1: halloj! }{5:║}|
{5:║}{1: BORDAA }{5:║}|
{5:╚═════════╝}|
]], float_pos={
[5] = {1002, "NW", 1, 2, 5, true, 50};
}, win_viewport={
[2] = {win = 1000, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 1, sum_scroll_delta = 0};
[5] = {win = 1002, topline = 0, botline = 2, curline = 0, curcol = 0, linecount = 2, sum_scroll_delta = 0};
}})
else
screen:expect{grid= expected}
end
api.nvim_win_close(win, false)
assert_alive()
end)
Expand Down

0 comments on commit 1ae21ce

Please sign in to comment.