Documentation
¶
Overview ¶
Package imgview renders raster image blobs as inline-image escape sequences for terminals that support the iTerm2 or Kitty graphics protocols.
Detection of which protocol the host terminal supports is owned by modules/term; this package is intentionally protocol-aware but terminal-agnostic so it stays easy to unit-test by inspecting the byte stream it writes.
Index ¶
Constants ¶
const MaxImageBytes = 5 << 20
MaxImageBytes caps the size of a single image payload this package will render. Anything larger should fall back to a hex dump in the caller.
Both protocols base64-encode the whole payload and inline it into the terminal stream; most terminal emulators (and the pseudo-terminal buffers they sit behind) start to choke well before tens of megabytes. 5 MiB is a conservative middle ground that comfortably covers screenshots, photos and typical asset blobs while keeping the worst-case escape-sequence size (~7 MiB after base64) within range of common terminal buffers.
Variables ¶
var ErrTooLarge = errors.New("imgview: image payload exceeds size limit")
ErrTooLarge is returned when the payload exceeds MaxImageBytes.
var ErrUnsupported = errors.New("imgview: terminal does not support inline images")
ErrUnsupported is returned when the requested protocol cannot render an image (either the terminal advertises no image protocol or the payload format is not supported by the chosen protocol).
Functions ¶
func CanRender ¶
func CanRender(proto term.ImageProtocol, m *mime.MIME) bool
CanRender reports whether the given protocol can render the given MIME type without server-side transcoding.
func IsRenderable ¶
IsRenderable reports whether the detected MIME type corresponds to a raster image format we are willing to render inline. It is protocol-agnostic; use CanRender for a protocol-aware check.
func Render ¶
Render writes an inline-image escape sequence representing data to w using the given protocol. The whole payload is buffered in memory because both supported protocols require a single base64 blob.
name is an optional filename hint (used by the iTerm2 protocol; ignored by Kitty). Passing the empty string is fine.
func Stream ¶
Stream reads up to MaxImageBytes from r, then renders the payload using Render. size is a best-effort hint used only for buffer pre-allocation; a non-positive value disables pre-allocation. If the payload exceeds the limit, ErrTooLarge is returned.
The MIME type, if non-nil, is used to derive a filename hint for protocols that accept one (iTerm2).
Types ¶
This section is empty.