kitty

package
v0.0.0-...-ae655b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: GPL-3.0 Imports: 15 Imported by: 0

README

Kitty Graphics Protocol Support

This package implements image rendering using the Kitty graphics protocol, which allows displaying images directly in supported terminals.

Supported Terminals

The Kitty graphics protocol is supported by:

  • Kitty - Full support (the original implementation)
  • WezTerm - Full support
  • Konsole - Partial support (KDE terminal)
  • Terminology - Partial support (Enlightenment terminal)

Other terminals may have varying levels of support or may not support the protocol at all.

Features

  • Image Fetching: Downloads images from URLs with built-in HTTP client
  • Caching: Caches downloaded images to avoid redundant downloads
  • Resizing: Automatically resizes images to fit terminal dimensions
  • Chunked Transmission: Handles large images by splitting them into chunks
  • Fallback Support: Provides text placeholders when images fail to load

Usage

Basic Example
import "github.com/Justice-Caban/Miryokusha/internal/tui/kitty"

// Create a renderer
renderer := kitty.NewImageRenderer()

// Configure image options
opts := kitty.ImageOptions{
    Width:               10,  // Width in terminal columns
    Height:              10,  // Height in terminal rows
    PreserveAspectRatio: true,
    ImageID:             1,   // Unique ID for this image
}

// Render an image from URL
imageStr, err := renderer.RenderImageFromURL("https://example.com/cover.jpg", opts)
if err != nil {
    // Use placeholder on error
    imageStr = kitty.CreatePlaceholder(opts.Width, opts.Height, "[IMG]")
}

// Print to terminal (will display the image)
fmt.Print(imageStr)
In the Library View

The library view in Miryokusha uses this package to display manga cover images:

  • Press i to toggle image display on/off
  • Configure in config.yaml with show_thumbnails: true/false
  • Images are automatically cached for performance
Clearing Images
// Clear a specific image
fmt.Print(kitty.ClearImage(1))

// Clear all images
fmt.Print(kitty.ClearAllImages())

Configuration

In your config.yaml:

preferences:
  show_thumbnails: true  # Enable image display in library

Protocol Details

The Kitty graphics protocol uses escape sequences to transmit images:

ESC _G <parameters> ; <base64 data> ESC \

Key parameters:

  • a=T - Action: transmit and display
  • f=100 - Format: PNG
  • i=<id> - Image ID (unique identifier)
  • c=<cols> - Width in columns
  • r=<rows> - Height in rows
  • m=1/0 - More data coming (1) or last chunk (0)

Performance Considerations

  • Images are cached in memory after first download
  • Large images are automatically resized to terminal dimensions
  • Images are transmitted in 4KB chunks to avoid terminal buffer limits
  • Use ClearImage() to free memory when images are no longer needed

Troubleshooting

Images not displaying?

  • Ensure you're using a supported terminal (Kitty, WezTerm, etc.)
  • Check that show_thumbnails is enabled in config
  • Verify the terminal supports the graphics protocol: echo -e "\e_Ga=q,i=1\e\\"

Images appear corrupted?

  • Try a different terminal emulator
  • Check terminal color depth and graphics settings
  • Ensure images are valid (PNG, JPEG, GIF)

Performance issues?

  • Disable images with i key in library view
  • Reduce cache size in configuration
  • Consider using text-only mode for slow connections

References

Documentation

Overview

Package kitty provides image rendering support using the Kitty graphics protocol.

The Kitty graphics protocol is a terminal graphics protocol that allows displaying images directly in the terminal. This package provides utilities for:

  • Fetching images from URLs
  • Resizing images to fit terminal dimensions
  • Encoding images using the Kitty protocol
  • Caching images to avoid redundant downloads

Usage:

renderer := kitty.NewImageRenderer()
opts := kitty.DefaultImageOptions()
imageStr, err := renderer.RenderImageFromURL("https://example.com/image.jpg", opts)
if err != nil {
    // Handle error or use placeholder
    imageStr = kitty.CreatePlaceholder(opts.Width, opts.Height, "[IMG]")
}
fmt.Print(imageStr)

The Kitty protocol uses escape sequences in the format:

ESC _G <key=value,...> ; <base64 data> ESC \

For more information on the Kitty graphics protocol, see: https://sw.kovidgoyal.net/kitty/graphics-protocol/

Index

Constants

View Source
const (
	ESC = "\x1b"
	APC = "\x1b_"
	ST  = "\x1b\\"
)

Protocol control characters

Variables

This section is empty.

Functions

func ClearAllImages

func ClearAllImages() string

ClearAllImages clears all images from the screen

func ClearImage

func ClearImage(imageID uint32) string

ClearImage clears an image with the given ID

func CreatePlaceholder

func CreatePlaceholder(width, height int, text string) string

CreatePlaceholder creates a text placeholder for images that fail to load

func ResizeImage

func ResizeImage(imgData []byte, maxWidth, maxHeight int) ([]byte, error)

ResizeImage resizes an image to fit within the specified dimensions

Types

type ImageOptions

type ImageOptions struct {
	Width               int // Width in cells (terminal columns)
	Height              int // Height in cells (terminal rows)
	PreserveAspectRatio bool
	ImageID             uint32 // Unique ID for this image
}

ImageOptions contains options for image rendering

func DefaultImageOptions

func DefaultImageOptions() ImageOptions

DefaultImageOptions returns sensible defaults for image rendering

type ImageRenderer

type ImageRenderer struct {
	// contains filtered or unexported fields
}

ImageRenderer handles Kitty protocol image rendering

func NewImageRenderer

func NewImageRenderer() *ImageRenderer

NewImageRenderer creates a new image renderer

func (*ImageRenderer) FetchImage

func (ir *ImageRenderer) FetchImage(url string) ([]byte, error)

FetchImage downloads an image from a URL

func (*ImageRenderer) RenderImage

func (ir *ImageRenderer) RenderImage(imgData []byte, opts ImageOptions) (string, error)

RenderImage renders an image using the Kitty graphics protocol via rasterm library

func (*ImageRenderer) RenderImageFromURL

func (ir *ImageRenderer) RenderImageFromURL(url string, opts ImageOptions) (string, error)

RenderImageFromURL fetches an image from a URL and renders it

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL