Skip to content

Commit de094dc

Browse files
mikoto2000chrisbra
authored andcommitted
patch 9.1.0863: getcellpixels() can be further improved
Problem: getcellpixels() can be further improved Solution: improve it further, add more tests (mikoto2000) closes: #16047 Signed-off-by: mikoto2000 <mikoto2000@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 0acd3ab commit de094dc

6 files changed

Lines changed: 87 additions & 50 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 11
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 14
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3790,10 +3790,10 @@ getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
37903790
getcellpixels() *getcellpixels()*
37913791
Returns a |List| of terminal cell pixel size.
37923792
List format is [xpixels, ypixels].
3793-
Only works on Unix. For gVim and on other systems,
3794-
returns [-1, -1].
3793+
Only works on (terminal) Unix. For gVim, on other systems and
3794+
on failure returns [].
37953795

3796-
Return type: list<Number>
3796+
Return type: list<any>
37973797

37983798

37993799
getcellwidths() *getcellwidths()*

src/os_unix.c

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,55 +4358,56 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
43584358
if (rettv_list_alloc(rettv) == FAIL)
43594359
return;
43604360

4361+
// failed get pixel size.
4362+
if (cs.cs_xpixel == -1)
4363+
return;
4364+
4365+
#if defined(FEAT_GUI)
4366+
// gui return [].
4367+
if (gui.in_use)
4368+
return;
4369+
#endif
4370+
4371+
// success pixel size and no gui.
43614372
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
43624373
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
43634374
}
43644375
#endif
43654376

43664377
/*
43674378
* Try to get the current terminal cell size.
4368-
* If faile get cell size, fallback 5x10 pixel.
4379+
* On failure, returns -1x-1
43694380
*/
43704381
void
43714382
mch_calc_cell_size(struct cellsize *cs_out)
43724383
{
4373-
#if defined(FEAT_GUI)
4374-
if (!gui.in_use)
4375-
{
4384+
// get current tty size.
4385+
struct winsize ws;
4386+
int fd = 1;
4387+
int retval = -1;
4388+
retval = ioctl(fd, TIOCGWINSZ, &ws);
4389+
4390+
#ifdef FEAT_EVAL
4391+
ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
43764392
#endif
4377-
// get current tty size.
4378-
struct winsize ws;
4379-
int fd = 1;
4380-
int retval = -1;
4381-
retval = ioctl(fd, TIOCGWINSZ, &ws);
4382-
# ifdef FEAT_EVAL
4383-
ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
4384-
# endif
4385-
if (retval == -1)
4386-
{
4387-
cs_out->cs_xpixel = -1;
4388-
cs_out->cs_ypixel = -1;
4389-
return;
4390-
}
43914393

4392-
// calculate parent tty's pixel per cell.
4393-
int x_cell_size = ws.ws_xpixel / ws.ws_col;
4394-
int y_cell_size = ws.ws_ypixel / ws.ws_row;
4394+
if (retval == -1)
4395+
{
4396+
cs_out->cs_xpixel = -1;
4397+
cs_out->cs_ypixel = -1;
4398+
return;
4399+
}
43954400

4396-
// calculate current tty's pixel
4397-
cs_out->cs_xpixel = x_cell_size;
4398-
cs_out->cs_ypixel = y_cell_size;
4401+
// calculate parent tty's pixel per cell.
4402+
int x_cell_size = ws.ws_xpixel / ws.ws_col;
4403+
int y_cell_size = ws.ws_ypixel / ws.ws_row;
43994404

4400-
# ifdef FEAT_EVAL
4401-
ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
4402-
# endif
4403-
#if defined(FEAT_GUI)
4404-
}
4405-
else
4406-
{
4407-
cs_out->cs_xpixel = -1;
4408-
cs_out->cs_ypixel = -1;
4409-
}
4405+
// calculate current tty's pixel
4406+
cs_out->cs_xpixel = x_cell_size;
4407+
cs_out->cs_ypixel = y_cell_size;
4408+
4409+
#ifdef FEAT_EVAL
4410+
ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
44104411
#endif
44114412
}
44124413

@@ -4433,8 +4434,18 @@ mch_report_winsize(int fd, int rows, int cols)
44334434
// calcurate and set tty pixel size
44344435
struct cellsize cs;
44354436
mch_calc_cell_size(&cs);
4436-
ws.ws_xpixel = cols * cs.cs_xpixel;
4437-
ws.ws_ypixel = rows * cs.cs_ypixel;
4437+
4438+
if (cs.cs_xpixel == -1)
4439+
{
4440+
// failed get pixel size.
4441+
ws.ws_xpixel = 0;
4442+
ws.ws_ypixel = 0;
4443+
}
4444+
else
4445+
{
4446+
ws.ws_xpixel = cols * cs.cs_xpixel;
4447+
ws.ws_ypixel = rows * cs.cs_ypixel;
4448+
}
44384449

44394450
retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
44404451
ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");

src/os_unix.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,9 @@ int mch_rename(const char *src, const char *dest);
489489
// We have three kinds of ACL support.
490490
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
491491

492+
// Defined as signed, to return -1 on error
492493
struct cellsize {
493-
unsigned int cs_xpixel;
494-
unsigned int cs_ypixel;
494+
int cs_xpixel;
495+
int cs_ypixel;
495496
};
496497

src/testdir/test_functions.vim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,4 +4159,35 @@ func Test_slice()
41594159
call assert_equal(0, slice(v:true, 1))
41604160
endfunc
41614161

4162+
4163+
" Test for getcellpixels()
4164+
" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
4165+
func Test_getcellpixels()
4166+
" Not yet Windows-compatible
4167+
CheckNotMSWindows
4168+
CheckRunVimInTerminal
4169+
4170+
let buf = RunVimInTerminal('', #{rows: 6})
4171+
4172+
" write getcellpixels() result to current buffer.
4173+
call term_sendkeys(buf, ":redi @\"\<CR>")
4174+
call term_sendkeys(buf, ":echo getcellpixels()\<CR>")
4175+
call term_sendkeys(buf, ":redi END\<CR>")
4176+
call term_sendkeys(buf, "P")
4177+
4178+
call WaitForAssert({-> assert_match("\[\d+, \d+\]", term_getline(buf, 3))}, 1000)
4179+
4180+
call StopVimInTerminal(buf)
4181+
endfunc
4182+
4183+
" Test for getcellpixels() on gVim
4184+
func Test_getcellpixels_gui()
4185+
" Not yet Windows-compatible
4186+
CheckNotMSWindows
4187+
if has("gui_running")
4188+
let cellpixels = getcellpixels()
4189+
call assert_equal(0, len(cellpixels))
4190+
endif
4191+
endfunc
4192+
41624193
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_utf8.vim

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,6 @@ func Test_setcellwidths()
273273
bwipe!
274274
endfunc
275275

276-
" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
277-
func Test_getcellpixels()
278-
" Not yet Windows-compatible
279-
CheckNotMSWindows
280-
let cellpixels = getcellpixels()
281-
call assert_equal(2, len(cellpixels))
282-
endfunc
283-
284276
func Test_getcellwidths()
285277
call setcellwidths([])
286278
call assert_equal([], getcellwidths())

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
863,
707709
/**/
708710
862,
709711
/**/

0 commit comments

Comments
 (0)