Add an example for displaying a PNG with bash

This commit is contained in:
Kovid Goyal 2023-01-29 10:27:03 +05:30
parent 53482f4c84
commit f2c8819d25
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -112,15 +112,40 @@ kitty.
A minimal example A minimal example
------------------ ------------------
Some minimal python code to display PNG images in kitty, using the most basic Some minimal code to display PNG images in kitty, using the most basic
features of the graphics protocol: features of the graphics protocol:
.. tab:: Bash
.. code-block:: sh
#!/bin/bash
transmit_png() {
data=$(base64 "$1")
data="${data//[[:space:]]}"
builtin local pos=0
builtin local chunk_size=4096
while [ $pos -lt ${#data} ]; do
builtin printf "\e_G"
[ $pos = "0" ] && printf "a=T,f=100,"
builtin local chunk="${data:$pos:$chunk_size}"
pos=$(($pos+$chunk_size))
[ $pos -lt ${#data} ] && builtin printf "m=1"
[ ${#chunk} -gt 0 ] && builtin printf ";%s" "${chunk}"
builtin printf "\e\\"
done
}
transmit_png "$1"
.. tab:: Python
.. code-block:: python .. code-block:: python
#!/usr/bin/python
import sys import sys
from base64 import standard_b64encode from base64 import standard_b64encode
def serialize_gr_command(**cmd): def serialize_gr_command(**cmd):
payload = cmd.pop('payload', None) payload = cmd.pop('payload', None)
cmd = ','.join(f'{k}={v}' for k, v in cmd.items()) cmd = ','.join(f'{k}={v}' for k, v in cmd.items())
@ -133,7 +158,6 @@ features of the graphics protocol:
w(b'\033\\') w(b'\033\\')
return b''.join(ans) return b''.join(ans)
def write_chunked(**cmd): def write_chunked(**cmd):
data = standard_b64encode(cmd.pop('data')) data = standard_b64encode(cmd.pop('data'))
while data: while data:
@ -144,15 +168,15 @@ features of the graphics protocol:
sys.stdout.flush() sys.stdout.flush()
cmd.clear() cmd.clear()
with open(sys.argv[-1], 'rb') as f: with open(sys.argv[-1], 'rb') as f:
write_chunked(a='T', f=100, data=f.read()) write_chunked(a='T', f=100, data=f.read())
Save this script as :file:`png.py`, then you can use it to display any PNG Save this script as :file:`send-png`, then you can use it to display any PNG
file in kitty as:: file in kitty as::
python png.py file.png chmod +x send-png
./send-png file.png
The graphics escape code The graphics escape code