]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0863: getcellpixels() can be further improved v9.1.0863
authormikoto2000 <mikoto2000@gmail.com>
Thu, 14 Nov 2024 21:13:48 +0000 (22:13 +0100)
committerChristian Brabandt <cb@256bit.org>
Thu, 14 Nov 2024 21:13:48 +0000 (22:13 +0100)
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>
runtime/doc/builtin.txt
src/os_unix.c
src/os_unix.h
src/testdir/test_functions.vim
src/testdir/test_utf8.vim
src/version.c

index 1c7814fe61d2310e95cf8c5b51dee2f22409c801..ab63681980f233d5c67a45138968a59b564b5fde 100644 (file)
@@ -1,4 +1,4 @@
-*builtin.txt*  For Vim version 9.1.  Last change: 2024 Nov 11
+*builtin.txt*  For Vim version 9.1.  Last change: 2024 Nov 14
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -3790,10 +3790,10 @@ getbufvar({buf}, {varname} [, {def}])                           *getbufvar()*
 getcellpixels()                                                *getcellpixels()*
                Returns a |List| of terminal cell pixel size.
                List format is [xpixels, ypixels].
-               Only works on Unix. For gVim and on other systems,
-               returns [-1, -1].
+               Only works on (terminal) Unix.  For gVim, on other systems and
+               on failure returns [].
 
-               Return type: list<Number>
+               Return type: list<any>
 
 
 getcellwidths()                                                *getcellwidths()*
index c69798d7d59ac3c5f018d130f200611f34633c64..80ca0ce26de2acffa29974a71b93f21f5da7e1e6 100644 (file)
@@ -4358,6 +4358,17 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
     if (rettv_list_alloc(rettv) == FAIL)
         return;
 
+    // failed get pixel size.
+    if (cs.cs_xpixel == -1)
+        return;
+
+#if defined(FEAT_GUI)
+    // gui return [].
+    if (gui.in_use)
+        return;
+#endif
+
+    // success pixel size and no gui.
     list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
     list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
 }
@@ -4365,48 +4376,38 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
 
 /*
  * Try to get the current terminal cell size.
- * If faile get cell size, fallback 5x10 pixel.
+ * On failure, returns -1x-1
  */
     void
 mch_calc_cell_size(struct cellsize *cs_out)
 {
-#if defined(FEAT_GUI)
-    if (!gui.in_use)
-    {
+   // get current tty size.
+   struct winsize ws;
+   int fd = 1;
+   int retval = -1;
+   retval = ioctl(fd, TIOCGWINSZ, &ws);
+
+#ifdef FEAT_EVAL
+   ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
 #endif
-        // get current tty size.
-        struct winsize ws;
-        int fd = 1;
-        int retval = -1;
-        retval = ioctl(fd, TIOCGWINSZ, &ws);
-#  ifdef FEAT_EVAL
-        ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
-#  endif
-        if (retval == -1)
-        {
-            cs_out->cs_xpixel = -1;
-            cs_out->cs_ypixel = -1;
-            return;
-        }
 
-        // calculate parent tty's pixel per cell.
-        int x_cell_size = ws.ws_xpixel / ws.ws_col;
-        int y_cell_size = ws.ws_ypixel / ws.ws_row;
+   if (retval == -1)
+   {
+       cs_out->cs_xpixel = -1;
+       cs_out->cs_ypixel = -1;
+       return;
+   }
 
-        // calculate current tty's pixel
-        cs_out->cs_xpixel = x_cell_size;
-        cs_out->cs_ypixel = y_cell_size;
+   // calculate parent tty's pixel per cell.
+   int x_cell_size = ws.ws_xpixel / ws.ws_col;
+   int y_cell_size = ws.ws_ypixel / ws.ws_row;
 
-#  ifdef FEAT_EVAL
-        ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
-#  endif
-#if defined(FEAT_GUI)
-    }
-    else
-    {
-        cs_out->cs_xpixel = -1;
-        cs_out->cs_ypixel = -1;
-    }
+   // calculate current tty's pixel
+   cs_out->cs_xpixel = x_cell_size;
+   cs_out->cs_ypixel = y_cell_size;
+
+#ifdef FEAT_EVAL
+   ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
 #endif
 }
 
@@ -4433,8 +4434,18 @@ mch_report_winsize(int fd, int rows, int cols)
     // calcurate and set tty pixel size
     struct cellsize cs;
     mch_calc_cell_size(&cs);
-    ws.ws_xpixel = cols * cs.cs_xpixel;
-    ws.ws_ypixel = rows * cs.cs_ypixel;
+
+    if (cs.cs_xpixel == -1)
+    {
+        // failed get pixel size.
+        ws.ws_xpixel = 0;
+        ws.ws_ypixel = 0;
+    }
+    else
+    {
+        ws.ws_xpixel = cols * cs.cs_xpixel;
+        ws.ws_ypixel = rows * cs.cs_ypixel;
+    }
 
     retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
     ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
index fac2c0fd99f5e15c86139566dffcce29af31cb2e..f017d56829e725b45b4f095d0915b70b6ed17437 100644 (file)
@@ -489,8 +489,9 @@ int mch_rename(const char *src, const char *dest);
 // We have three kinds of ACL support.
 #define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
 
+// Defined as signed, to return -1 on error
 struct cellsize {
-    unsigned int cs_xpixel;
-    unsigned int cs_ypixel;
+    int cs_xpixel;
+    int cs_ypixel;
 };
 
index 85a34052e4a985d497a050fa2308a04836717b0d..f5556210412b060465448d72566f50b78850cef8 100644 (file)
@@ -4159,4 +4159,35 @@ func Test_slice()
   call assert_equal(0, slice(v:true, 1))
 endfunc
 
+
+" Test for getcellpixels()
+" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
+func Test_getcellpixels()
+  " Not yet Windows-compatible
+  CheckNotMSWindows
+  CheckRunVimInTerminal
+
+  let buf = RunVimInTerminal('', #{rows: 6})
+
+  " write getcellpixels() result to current buffer.
+  call term_sendkeys(buf, ":redi @\"\<CR>")
+  call term_sendkeys(buf, ":echo getcellpixels()\<CR>")
+  call term_sendkeys(buf, ":redi END\<CR>")
+  call term_sendkeys(buf, "P")
+
+  call WaitForAssert({-> assert_match("\[\d+, \d+\]", term_getline(buf, 3))}, 1000)
+
+  call StopVimInTerminal(buf)
+endfunc
+
+" Test for getcellpixels() on gVim
+func Test_getcellpixels_gui()
+  " Not yet Windows-compatible
+  CheckNotMSWindows
+  if has("gui_running")
+    let cellpixels = getcellpixels()
+    call assert_equal(0, len(cellpixels))
+  endif
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index d820e3517d79702c854068bd1601373eb8ef7b5f..991a095380411409cf8d0f667a90111d54a654e2 100644 (file)
@@ -273,14 +273,6 @@ func Test_setcellwidths()
   bwipe!
 endfunc
 
-" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
-func Test_getcellpixels()
-  " Not yet Windows-compatible
-  CheckNotMSWindows
-  let cellpixels = getcellpixels()
-  call assert_equal(2, len(cellpixels))
-endfunc
-
 func Test_getcellwidths()
   call setcellwidths([])
   call assert_equal([], getcellwidths())
index 69b5416094c6503c59ae23cab20734eac4456de9..d0aa4036a6c7af4c71fe15a5ee14fc28e220af81 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    863,
 /**/
     862,
 /**/