Add example code to get screen size in Bash

This commit is contained in:
Kovid Goyal 2023-04-21 15:18:30 +05:30
parent ccdb951716
commit 6cc8e67580
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -58,7 +58,8 @@ Getting the window size
In order to know what size of images to display and how to position them, the In order to know what size of images to display and how to position them, the
client must be able to get the window size in pixels and the number of cells client must be able to get the window size in pixels and the number of cells
per row and column. This can be done by using the ``TIOCGWINSZ`` ioctl. Some per row and column. The cell width is then simply the window size divided by the
number of rows. This can be done by using the ``TIOCGWINSZ`` ioctl. Some
code to demonstrate its use code to demonstrate its use
.. tab:: C .. tab:: C
@ -99,6 +100,20 @@ code to demonstrate its use
fmt.Println("rows: %v columns: %v width: %v height %v", sz.Row, sz.Col, sz.Xpixel, sz.Ypixel) fmt.Println("rows: %v columns: %v width: %v height %v", sz.Row, sz.Col, sz.Xpixel, sz.Ypixel)
.. tab:: Bash
.. code-block:: sh
#!/bin/bash
# This uses the kitten standalone binary from kitty to get the pixel sizes
# since we cant do IOCTLs directly. Fortunately, kitten is a static exe
# pre-built for every Unix like OS under the sun.
builtin read -r rows cols < <(command stty size)
IFS=x builtin read -r width height < <(command kitten icat --print-window-size); builtin unset IFS
builtin echo "number of rows: $rows number of columns: $cols screen width: $width screen height: $height"
Note that some terminals return ``0`` for the width and height values. Such Note that some terminals return ``0`` for the width and height values. Such
terminals should be modified to return the correct values. Examples of terminals should be modified to return the correct values. Examples of