termimage

package
v0.0.289 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxCols      = 80
	DefaultMaxRows      = 40
	DefaultCellWidthPx  = 10
	DefaultCellHeightPx = 20
)

Variables

This section is empty.

Functions

func CleanupSequence

func CleanupSequence(env Environment) string

CleanupSequence returns terminal image cleanup bytes appropriate for env. It is intentionally conservative: only Kitty-style graphics currently need an explicit cleanup command for alt-screen teardown.

func ClearCache

func ClearCache()

ClearCache clears cached terminal image render results.

func Debugf added in v0.0.233

func Debugf(env Environment, format string, args ...any)

func KittyDeleteImageSequence added in v0.0.233

func KittyDeleteImageSequence(imageIDs ...uint32) string

KittyDeleteImageSequence returns Kitty graphics commands that delete the specific image ids owned by this process. Prefer this to global cleanup when the ids are known, so unrelated Kitty graphics are not disturbed.

func KittyDeletePlacementSequence added in v0.0.236

func KittyDeletePlacementSequence(imageID, placementID uint32) string

KittyDeletePlacementSequence deletes one direct placement while keeping any underlying image data available for later re-placement.

func KittyDeleteVisibleSequence

func KittyDeleteVisibleSequence() string

KittyDeleteVisibleSequence returns a Kitty graphics command that deletes all image placements currently visible on the terminal screen and asks the terminal to free associated image data where possible. It is useful when leaving an alternate-screen UI so Kitty/Ghostty placements do not bleed into the restored main screen.

func KittyDirectPlaceSequence added in v0.0.236

func KittyDirectPlaceSequence(imageID, placementID uint32, cols, rows int) string

KittyDirectPlaceSequence places an already-transmitted image at the current cursor position. If placementID is non-zero, repeating the same image and placement id moves/replaces that placement instead of creating another one.

func KittyPlaceSequence added in v0.0.233

func KittyPlaceSequence(imageID uint32, cols, rows int) string

KittyPlaceSequence creates or updates a virtual Unicode-placeholder placement for an already-transmitted Kitty image id.

func KittyPlaceholderGrid added in v0.0.233

func KittyPlaceholderGrid(imageID uint32, cols, rows int) string

KittyPlaceholderGrid returns the Unicode placeholder grid for imageID.

Types

type Environment

type Environment struct {
	Term          string
	TermProgram   string
	LCTerminal    string
	KittyWindowID string
	ColorTerm     string
	Tmux          string
	Screen        string

	ForcedProtocol string
	Debug          bool
	DebugFile      string
}

Environment captures terminal-related environment used for deterministic protocol selection. DefaultEnvironment reads these fields from os.Environ.

func DefaultEnvironment

func DefaultEnvironment() Environment

DefaultEnvironment returns terminal-image relevant environment variables.

type Mode

type Mode string

Mode identifies the output surface the rendered image is destined for.

const (
	// ModeViewport is Bubble Tea viewport content. Results must be safe to line
	// clip and redraw; raw image upload escapes are returned separately in Upload.
	ModeViewport Mode = "viewport"
	// ModeScrollback is inline/scrollback terminal output. One-shot escape based
	// renderers are acceptable here.
	ModeScrollback Mode = "scrollback"
	// ModeOneShot is direct terminal output outside a redrawable viewport.
	ModeOneShot Mode = "oneshot"
)

type Protocol

type Protocol string

Protocol identifies a terminal image rendering protocol/strategy.

const (
	ProtocolAuto  Protocol = "auto"
	ProtocolKitty Protocol = "kitty"
	ProtocolITerm Protocol = "iterm"
	ProtocolSixel Protocol = "sixel"
	ProtocolANSI  Protocol = "ansi"
	ProtocolNone  Protocol = "none"
)

type Request

type Request struct {
	Path     string
	MaxCols  int
	MaxRows  int
	Mode     Mode
	Protocol Protocol

	// Background is used by text-based renderers when compositing transparent
	// pixels. If nil or transparent, a dark terminal-style fallback is used.
	Background color.Color

	// AllowEscapeUploads is reserved for callers that want to explicitly document
	// whether they can emit Upload out-of-band. Protocol selection currently uses
	// Protocol=ansi/none for text-only operation.
	AllowEscapeUploads bool

	// CellWidthPx/CellHeightPx optionally override terminal cell pixel size.
	// When zero, the renderer tries to detect the size and falls back to 10x20.
	CellWidthPx  int
	CellHeightPx int

	// SliceStartRow/SliceRows request a vertical cell slice of the scaled image.
	// This is used by redrawable viewport integrations that need to display only
	// the visible portion of an image. Zero SliceRows means render the full image.
	SliceStartRow int
	SliceRows     int
}

Request describes an image render request.

type Result

type Result struct {
	Protocol Protocol

	// Upload contains terminal control bytes that must be emitted directly to the
	// terminal, outside any line-wrapping/slicing viewport content.
	Upload string
	// Place contains terminal control bytes that create/update the visible image
	// placement after Upload has completed. It is separate for redrawable viewport
	// integrations that need to gate placeholders until both transmit and
	// placement commands have been flushed.
	Place string
	// Display is safe for the requested output surface. For Kitty viewport mode it
	// is only the Unicode placeholder grid. For ANSI it is ordinary ANSI text.
	Display string
	// Full is a one-shot convenience string equal to Upload + Place + Display.
	Full string

	WidthCells  int
	HeightCells int

	ImageID     uint32
	PlacementID uint32

	TextFallback string
	CacheKey     string
	Warnings     []string
}

Result is the normalized output shape returned by all rendering strategies.

func KittyPlacementForPath added in v0.0.233

func KittyPlacementForPath(req Request, imageID uint32) (Result, error)

KittyPlacementForPath calculates viewport cell dimensions for path and returns a placement-only Kitty result for an already-transmitted image id. It decodes image metadata but does not re-encode or retransmit PNG bytes.

func Render

func Render(req Request) (Result, error)

Render renders an image using the current process environment.

func RenderWithEnvironment

func RenderWithEnvironment(req Request, env Environment) (Result, error)

RenderWithEnvironment renders an image using an explicit environment. It is primarily useful for deterministic tests and debugging.

type Strategy

type Strategy struct {
	Protocol Protocol
	Name     string
	Warnings []string
}

Strategy is the selected rendering adventure for a request/environment pair.

func Select

func Select(req Request, env Environment) Strategy

Select chooses the rendering strategy for a request and environment.

Jump to

Keyboard shortcuts

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