5 Graphics cache and placements
Graphics output is opt-in and gated on a successful probe:
(with-kitty #:probe-graphics? #t (lambda () (define img (kitty-image 1 2 1 'rgb #"\0\0\0\0\0\0")) (put-kitty-image img)))
struct
(struct kitty-image (id width height format data))
id : exact-positive-integer? width : (or/c exact-positive-integer? #f) height : (or/c exact-positive-integer? #f) format : (or/c 'rgb 'rgba 'png 'indexed 'indexed-alpha) data : (or/c bytes? kitty-payload?)
format selects the pixel format: 'rgb (f=24, 3 bytes per pixel), 'rgba (f=32, 4 bytes per pixel), 'png (f=100, the terminal reads the dimensions from the PNG), 'indexed (f=8, 1 byte palette index per pixel) or 'indexed-alpha (f=16, palette index plus alpha). For every format except 'png width and height are required; for direct transmission the payload length is checked against width x height x bytes per pixel.
data is either the payload bytes (direct transmission, t=d, uncompressed) or a kitty-payload that selects the medium and the optional compression / size / offset. This library never decodes, resamples, color-converts or re-encodes image data: the payload is forwarded verbatim (after the base64 encoding and 4096-byte chunking that the protocol itself requires).
struct
(struct kitty-payload (data medium compression offset size))
data : (or/c bytes? path-string?) medium : (or/c 'direct 'file 'temp-file 'shm) compression : (or/c #f 'zlib) offset : (or/c #f exact-nonnegative-integer?) size : (or/c #f exact-positive-integer?)
'direct (t=d): data is the payload as bytes?; this library base64-encodes and chunks it.
'file (t=f) / 'temp-file (t=t): data is a path; the terminal reads the file and this library never opens it. For 'temp-file the path should contain "tty-graphics-protocol" so that kitty deletes it after reading.
'shm (t=s): data is a POSIX shared-memory object name.
compression adds o=z when 'zlib; the data must already be zlib-compressed by the caller. offset (O) and size (S) select a slice of a file or shared-memory object. offset is not allowed with 'direct, and compressed PNG data sent directly requires size.
procedure
(make-kitty-payload data [ #:medium medium #:compression compression #:offset offset #:size size]) → kitty-payload? data : (or/c bytes? path-string?) medium : (or/c 'direct 'file 'temp-file 'shm) = 'direct compression : (or/c #f 'zlib) = #f offset : (or/c #f exact-nonnegative-integer?) = #f size : (or/c #f exact-positive-integer?) = #f
parameter
(current-kitty-graphics?) → boolean?
(current-kitty-graphics? on?) → void? on? : boolean?
procedure
(kitty-graphics-supported?) → boolean?
parameter
(current-kitty-unicode-placeholders?) → boolean?
(current-kitty-unicode-placeholders? on?) → void? on? : boolean?
procedure
(kitty-unicode-placeholders-supported?) → boolean?
procedure
(probe-kitty-graphics! [ #:in in #:out out #:timeout timeout #:idle idle]) → boolean? in : input-port? = (current-input-port) out : output-port? = (current-output-port) timeout : real? = 0.3 idle : real? = 0.05
struct
(struct kitty-placement (image-id placement-id rows columns kind))
image-id : exact-positive-integer? placement-id : exact-positive-integer? rows : (or/c exact-positive-integer? #f) columns : (or/c exact-positive-integer? #f) kind : (or/c 'image 'unicode)
struct
(struct kitty-image-cache (images placements next-placement-id) #:mutable #:transparent) images : hash? placements : hash? next-placement-id : exact-positive-integer?
parameter
(current-kitty-image-cache cache) → void? cache : kitty-image-cache?
procedure
procedure
(kitty-cache-clear! [cache]) → void?
cache : kitty-image-cache? = (current-kitty-image-cache)
procedure
(kitty-cache-empty? [cache]) → boolean?
cache : kitty-image-cache? = (current-kitty-image-cache)
procedure
(kitty-cache-image image-id [cache]) → (or/c kitty-image? #f)
image-id : exact-positive-integer? cache : kitty-image-cache? = (current-kitty-image-cache)
procedure
(kitty-cache-placement image-id placement-id [ cache]) → (or/c kitty-placement? #f) image-id : exact-positive-integer? placement-id : exact-positive-integer? cache : kitty-image-cache? = (current-kitty-image-cache)
procedure
(kitty-cache-placements [cache]) → (listof kitty-placement?)
cache : kitty-image-cache? = (current-kitty-image-cache)
procedure
(put-kitty-graphics-command control payload [ #:force? force?]) → boolean? control : string? payload : bytes? force? : boolean? = #f
procedure
(kitty-place-image img [ #:placement-id placement-id #:columns columns #:rows rows #:force? force?]) → (or/c kitty-placement? #f) img : kitty-image? placement-id : (or/c exact-positive-integer? #f) = #f columns : (or/c exact-positive-integer? #f) = #f rows : (or/c exact-positive-integer? #f) = #f force? : boolean? = #f
procedure
(kitty-place-image-at row col img [ #:placement-id placement-id #:columns columns #:rows rows #:force? force?]) → (or/c kitty-placement? #f) row : exact-nonnegative-integer? col : exact-nonnegative-integer? img : kitty-image? placement-id : (or/c exact-positive-integer? #f) = #f columns : (or/c exact-positive-integer? #f) = #f rows : (or/c exact-positive-integer? #f) = #f force? : boolean? = #f
procedure
(put-kitty-image img [ #:placement-id placement-id #:columns columns #:rows rows #:force? force?]) → boolean? img : kitty-image? placement-id : (or/c exact-positive-integer? #f) = #f columns : (or/c exact-positive-integer? #f) = #f rows : (or/c exact-positive-integer? #f) = #f force? : boolean? = #f
procedure
(put-kitty-image-at row col img [ #:placement-id placement-id #:columns columns #:rows rows #:force? force?]) → boolean? row : exact-nonnegative-integer? col : exact-nonnegative-integer? img : kitty-image? placement-id : (or/c exact-positive-integer? #f) = #f columns : (or/c exact-positive-integer? #f) = #f rows : (or/c exact-positive-integer? #f) = #f force? : boolean? = #f
procedure
(kitty-placement-delete image-id placement-id [ #:force? force?]) → boolean? image-id : exact-positive-integer? placement-id : exact-positive-integer? force? : boolean? = #f
procedure
(kitty-image-delete id [#:force? force?]) → boolean?
id : exact-positive-integer? force? : boolean? = #f
procedure
(kitty-image-delete-all [#:force? force?]) → boolean?
force? : boolean? = #f
(put-kitty-image img) (put-kitty-image img) (kitty-place-image img #:placement-id 7) (kitty-place-image-at 5 10 img #:placement-id 7) (put-kitty-image-at 5 10 img #:columns 24 #:rows 8) (kitty-cache-image 1) (kitty-cache-placement 1 7) (kitty-cache-placements) (kitty-cache-empty?) (kitty-placement-delete 1 7) (kitty-image-delete 1) (kitty-image-delete-all)
Images are cached by id. Reusing the same id with identical data avoids retransmission; changing the data retransmits and invalidates that id’s cached placements. a=t and a=p use q=2 so the terminal does not send acknowledgements into the input stream.