diff --git a/docs/graphics-protocol.rst b/docs/graphics-protocol.rst index f6e6e4ce0..c977a569f 100644 --- a/docs/graphics-protocol.rst +++ b/docs/graphics-protocol.rst @@ -75,6 +75,47 @@ You can also use the *CSI t* escape code to get the screen size. Send ``[4;;t`` where *height* and *width* are the window size in pixels. This escape code is supported in many terminals, not just kitty. +A minimal example +------------------ + +Some minimal python code to display PNG images in kitty, using the most basic +features of the graphics protocol: + +.. code-block:: python + + import sys + from base64 import standard_b64encode + + def serialize_gr_command(cmd, payload=None): + cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items()) + ans = [] + w = ans.append + w(b'\033_G'), w(cmd.encode('ascii')) + if payload: + w(b';') + w(payload) + w(b'\033\\') + return b''.join(ans) + + def write_chunked(cmd, data): + data = standard_b64encode(data) + while data: + chunk, data = data[:4096], data[4096:] + m = 1 if data else 0 + cmd['m'] = m + sys.stdout.buffer.write(serialize_gr_command(cmd, chunk)) + sys.stdout.flush() + cmd.clear() + + write_chunked({'a': 'T', 'f': 100}, open(sys.argv[-1], 'rb').read()) + + +Save this script as :file:`png.py`, then you can use it to display any PNG +file in kitty as:: + + python png.py file.png + + The graphics escape code ---------------------------