Add an example for displaying a PNG with bash
This commit is contained in:
parent
53482f4c84
commit
f2c8819d25
@ -112,15 +112,40 @@ kitty.
|
||||
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:
|
||||
|
||||
.. code-block:: python
|
||||
.. 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
|
||||
|
||||
#!/usr/bin/python
|
||||
import sys
|
||||
from base64 import standard_b64encode
|
||||
|
||||
|
||||
def serialize_gr_command(**cmd):
|
||||
payload = cmd.pop('payload', None)
|
||||
cmd = ','.join(f'{k}={v}' for k, v in cmd.items())
|
||||
@ -133,7 +158,6 @@ features of the graphics protocol:
|
||||
w(b'\033\\')
|
||||
return b''.join(ans)
|
||||
|
||||
|
||||
def write_chunked(**cmd):
|
||||
data = standard_b64encode(cmd.pop('data'))
|
||||
while data:
|
||||
@ -144,15 +168,15 @@ features of the graphics protocol:
|
||||
sys.stdout.flush()
|
||||
cmd.clear()
|
||||
|
||||
|
||||
with open(sys.argv[-1], 'rb') as f:
|
||||
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::
|
||||
|
||||
python png.py file.png
|
||||
chmod +x send-png
|
||||
./send-png file.png
|
||||
|
||||
|
||||
The graphics escape code
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user