Kitty Graphics Protocol in OCaml
terminal graphics ocaml

files

Changed files
+137
.tangled
workflows
+49
.tangled/workflows/build.yml
···
+
when:
+
- event: ["push", "pull_request"]
+
branch: ["main"]
+
+
engine: nixery
+
+
dependencies:
+
nixpkgs:
+
- shell
+
- stdenv
+
- findutils
+
- binutils
+
- libunwind
+
- ncurses
+
- opam
+
- git
+
- gawk
+
- gnupatch
+
- gnum4
+
- gnumake
+
- gnutar
+
- gnused
+
- gnugrep
+
- diffutils
+
- gzip
+
- bzip2
+
- gcc
+
- ocaml
+
+
steps:
+
- name: opam
+
command: |
+
opam init --disable-sandboxing -any
+
- name: switch
+
command: |
+
opam install . --confirm-level=unsafe-yes --deps-only
+
- name: build
+
command: |
+
opam exec -- dune build
+
- name: switch-test
+
command: |
+
opam install . --confirm-level=unsafe-yes --deps-only --with-test
+
- name: test
+
command: |
+
opam exec -- dune runtest --verbose
+
- name: doc
+
command: |
+
opam install -y odoc
+
opam exec -- dune build @doc
+4
CHANGES.md
···
+
v1.0.0 (dev)
+
------------
+
+
- Initial public release (@avsm)
+18
LICENSE.md
···
+
(*
+
* ISC License
+
*
+
* Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>
+
*
+
* Permission to use, copy, modify, and distribute this software for any
+
* purpose with or without fee is hereby granted, provided that the above
+
* copyright notice and this permission notice appear in all copies.
+
*
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
*
+
*)
+66
README.md
···
+
# kgp - Kitty Graphics Protocol for OCaml
+
+
An OCaml library for displaying images in terminals using the [Kitty Graphics Protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/).
+
+
This library can display PNG images in supported terminals (e.g. Kitty, WezTerm, Konsole, Ghostty) with varying levels of support depending on the terminal. Most core features work but some advanced things like animations might be only partially supported outside of Kitty.
+
+
Other features include:
+
- Image transmission with automatic chunking and base64 encoding
+
- Multiple placements of the same image
+
- Animation support with frame deltas
+
- Unicode placeholder mode for proper scrolling
+
- tmux passthrough support (requires tmux 3.3+)
+
- Terminal capability detection
+
+
The library provides a Cmdliner term via the `kgp.cli` package to make integration into other CLI tools easier.
+
+
## Installation
+
+
```
+
opam install kgp
+
```
+
+
## Usage
+
+
### Library
+
+
```ocaml
+
(* Display a PNG image *)
+
let png_data = read_file "image.png" in
+
let cmd = Kgp.transmit_and_display ~format:`Png () in
+
let buf = Buffer.create 1024 in
+
Kgp.write buf cmd ~data:png_data;
+
print_string (Buffer.contents buf)
+
```
+
+
```ocaml
+
(* Transmit once, display multiple times *)
+
let cmd = Kgp.transmit ~image_id:1 ~format:`Png () in
+
Kgp.write buf cmd ~data:png_data;
+
+
let cmd = Kgp.display ~image_id:1 () in
+
Kgp.write buf cmd ~data:""
+
```
+
+
### CLI
+
+
```
+
# Display an image
+
kgpcat image.png
+
+
# Display at specific size and position
+
kgpcat --place 40x20@10,5 image.png
+
+
# Display from stdin
+
curl -s https://example.com/image.png | kgpcat
+
+
# Detect terminal support
+
kgpcat --detect-support
+
+
# Clear displayed images
+
kgpcat --clear
+
```
+
+
## License
+
+
ISC