On this page:
kitty-image
kitty-payload
make-kitty-payload
current-kitty-graphics?
kitty-graphics-supported?
current-kitty-unicode-placeholders?
kitty-unicode-placeholders-supported?
probe-kitty-graphics!
kitty-placement
kitty-image-cache
current-kitty-image-cache
make-kitty-image-cache
kitty-cache-clear!
kitty-cache-empty?
kitty-cache-image
kitty-cache-placement
kitty-cache-placements
put-kitty-graphics-command
kitty-place-image
kitty-place-image-at
put-kitty-image
put-kitty-image-at
kitty-placement-delete
kitty-image-delete
kitty-image-delete-all

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?)
An image to transmit, described exactly as kitty’s f, s, v and payload are. id is in 1 to 4294967295.

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?)
The t, o, S and O control keys of one image payload. medium says where the terminal gets the data:

  • '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
Keyword constructor for kitty-payload.

parameter

(current-kitty-graphics?) → boolean?

(current-kitty-graphics? on?) → void?
  on? : boolean?
Whether kitty graphics were detected (or forced) for the current session. Set by probe-kitty-graphics! and restored by tui-exit-kitty.

procedure

(kitty-graphics-supported?) → boolean?

True when current-kitty-graphics? is true.

parameter

(current-kitty-unicode-placeholders?) → boolean?

(current-kitty-unicode-placeholders? on?) → void?
  on? : boolean?
Whether the terminal reported the Unicode-placeholder feature (U=1).

procedure

(kitty-unicode-placeholders-supported?) → boolean?

True when both graphics and Unicode placeholders are available.

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
Sends the basic graphics query and the Unicode-placeholder feature query a=q,i=31,e=T,U=1, then sets current-kitty-graphics? and current-kitty-unicode-placeholders? from the replies. Must run in raw mode before the application event loop starts. Returns whether basic graphics support was detected, and #f on error.

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)
One placement of an image, as recorded by the cache. kind is 'image for a normal placement and 'unicode for a virtual placement used by Unicode placeholders.

struct

(struct kitty-image-cache (images placements next-placement-id)
    #:mutable
    #:transparent)
  images : hash?
  placements : hash?
  next-placement-id : exact-positive-integer?
The per-session transmission and placement cache. images maps an image id to a kitty-image; placements maps (cons image-id placement-id) to a kitty-placement.

The cache used by the output functions. tui-init-kitty installs a fresh cache and tui-exit-kitty restores the previous one.

Creates an empty cache.

procedure

(kitty-cache-clear! [cache]) → void?

  cache : kitty-image-cache? = (current-kitty-image-cache)
Empties cache without sending anything to the terminal.

procedure

(kitty-cache-empty? [cache]) → boolean?

  cache : kitty-image-cache? = (current-kitty-image-cache)
True when no image and no placement is cached.

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)
Cache queries.

procedure

(put-kitty-graphics-command control    
  payload    
  [#:force? force?]) → boolean?
  control : string?
  payload : bytes?
  force? : boolean? = #f
Formats and writes one graphics command. Returns #f without writing when graphics are unavailable and force? is #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
Transmits img if the cache does not already hold that exact image, then places it at the current cursor position. Returns the placement, or #f when graphics are unavailable and force? is #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
Like kitty-place-image, but first moves the cursor to rowxcol.

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
Boolean-returning variants of kitty-place-image and kitty-place-image-at.

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
Delete one placement (d=i, keeping the image data), one image and all of its placements (d=I), or every image (d=A, which also clears the cache).

(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.