Transmit + display a PNG via kitty graphics protocol. Chunks at 4096 base64 chars (≈3072 raw bytes) to respect terminal IO buffer limits. Uses `q=2` (silent), `C=1` (don't move cursor), `a=T` (transmit and display). Reusing the same image_id makes kitty replace the previous frame in
(png: bytes, image_id: int = KITTY_IMAGE_ID)
| 100 | |
| 101 | |
| 102 | def kitty_send(png: bytes, image_id: int = KITTY_IMAGE_ID) -> None: |
| 103 | """Transmit + display a PNG via kitty graphics protocol. |
| 104 | |
| 105 | Chunks at 4096 base64 chars (≈3072 raw bytes) to respect terminal IO |
| 106 | buffer limits. Uses `q=2` (silent), `C=1` (don't move cursor), `a=T` |
| 107 | (transmit and display). Reusing the same image_id makes kitty replace |
| 108 | the previous frame in place, giving smooth playback. |
| 109 | """ |
| 110 | enc = base64.standard_b64encode(png) |
| 111 | chunk = 4096 |
| 112 | parts = [enc[i:i + chunk] for i in range(0, len(enc), chunk)] |
| 113 | out = sys.stdout.buffer |
| 114 | for i, p in enumerate(parts): |
| 115 | m = 1 if i < len(parts) - 1 else 0 |
| 116 | if i == 0: |
| 117 | head = f"\033_Ga=T,f=100,i={image_id},q=2,C=1,m={m};".encode() |
| 118 | else: |
| 119 | head = f"\033_Gm={m},q=2;".encode() |
| 120 | out.write(head) |
| 121 | out.write(p) |
| 122 | out.write(b"\033\\") |
| 123 | out.flush() |
| 124 | |
| 125 | |
| 126 | def kitty_delete(image_id: int = KITTY_IMAGE_ID) -> None: |