attach

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package attach — ANSI to PNG conversion. Renders terminal text (with or without ANSI escape sequences) to a PNG image. Mirrors the intent of src/utils/ansiToPng.ts.

Implementation uses Go's standard image library with a monospace pixel font. ANSI SGR codes are parsed to apply foreground/background colors (8-color, 256-color, and truecolor). Unsupported sequences are ignored.

Package attach handles clipboard attachments — images and large text pastes. Mirrors the clipboard-reading logic in src/utils/imagePaste.ts.

Platform support:

  • macOS: osascript (NSPasteboard)
  • Linux: xclip or wl-paste
  • Windows: not implemented (returns ErrNotSupported)

Index

Constants

This section is empty.

Variables

View Source
var ErrNoImage = errors.New("no image on clipboard")

ErrNoImage is returned when the clipboard contains no image data.

View Source
var ErrNoPDF = errors.New("no PDF on clipboard")

ErrNoPDF is returned when the clipboard contains no PDF data.

View Source
var ErrNotSupported = errors.New("image paste not supported on this platform")

ErrNotSupported is returned on platforms where clipboard image access is not implemented.

Functions

func ANSIToPNG

func ANSIToPNG(text string) string

ANSIToPNG renders the given terminal text (may contain ANSI escapes) to a PNG image and returns it as a base64-encoded string. The image uses a dark terminal theme (black background, light text). Returns "" on failure.

func DetectDroppedPaths

func DetectDroppedPaths(content string) ([]string, bool)

DetectDroppedPaths is the multi-file version of ConvertDroppedFiles — it returns the list of resolved absolute paths instead of @mentions. Returns (nil, false) when the content doesn't look like dropped files.

func FormatAtResult

func FormatAtResult(r AtResult) string

FormatAtResult wraps a file attachment in the XML envelope that CC uses.

func MaybeResize

func MaybeResize(img *Image) error

MaybeResize decodes the base64 image in img and checks whether its dimensions or byte-size exceed the Anthropic API limits. If they do, the image is resized (and re-encoded as JPEG for smaller output) and img is updated in-place. If the image is within limits, it is returned unchanged.

Mirrors src/utils/imageResizer.ts:maybeResizeAndDownsampleImageBuffer.

func MentionPath

func MentionPath(p string) string

MentionPath builds an @mention string for an arbitrary dropped file path.

func StripANSI

func StripANSI(s string) string

StripANSI removes all ANSI escape sequences from s.

Types

type AtMention

type AtMention struct {
	Original  string // original token as written, e.g. "@src/main.go#L1-5"
	Path      string // path portion without line range
	LineStart int    // 1-based; 0 = not specified
	LineEnd   int    // 1-based inclusive; 0 = not specified
}

func ExtractAtMentions

func ExtractAtMentions(text string) []AtMention

ExtractAtMentions parses @path tokens from user text. Tokens like @"agent (agent)" (agent mentions) are excluded.

type AtResult

type AtResult struct {
	DisplayPath string // path relative to cwd for display in the system block
	Content     string // file or directory listing content (empty for PDFs)
	// PDF fields — set when the @mention points to a .pdf file.
	IsPDF   bool
	PDFData string // base64-encoded PDF bytes (non-empty when IsPDF)
}

AtResult is the resolved content for one @mention.

func ProcessAtMentions

func ProcessAtMentions(text, cwd string) []AtResult

ProcessAtMentions reads all @-mentioned paths and returns content blocks suitable for prepending to the API user message. cwd is used to expand relative paths and compute display paths.

type DropType

type DropType int

DropType classifies a dropped file.

const (
	DropOther DropType = iota
	DropImage
	DropPDF
)

func DroppedFileType

func DroppedFileType(p string) DropType

DroppedFileType classifies a path by extension.

type Image

type Image struct {
	Data      string // base64-encoded PNG bytes
	MediaType string // "image/png" always for now
}

Image holds a base64-encoded PNG image grabbed from the clipboard.

func ReadClipboardImage

func ReadClipboardImage() (*Image, error)

ReadClipboardImage attempts to read a PNG image from the system clipboard. Returns ErrNoImage when the clipboard holds text or is empty. Returns ErrNotSupported on Windows.

func ReadImageFile

func ReadImageFile(p string) (*Image, error)

ReadImageFile reads an image from disk and returns it as an attach.Image.

type PDF

type PDF struct {
	Data      string // base64-encoded PDF bytes
	MediaType string // always "application/pdf"
}

PDF holds a base64-encoded PDF grabbed from the clipboard.

func ReadClipboardPDF

func ReadClipboardPDF() (*PDF, error)

ReadClipboardPDF attempts to read a PDF from the system clipboard. On macOS this works when a PDF file is copied in Finder (puts the file path on the clipboard) or when apps copy PDF data directly. Returns ErrNoPDF when no PDF is found. Returns ErrNotSupported on Windows.

func ReadPDFFile

func ReadPDFFile(p string) (*PDF, error)

ReadPDFFile reads a PDF from disk and returns it as an attach.PDF.

Jump to

Keyboard shortcuts

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