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 ¶
- Variables
- func ANSIToPNG(text string) string
- func DetectDroppedPaths(content string) ([]string, bool)
- func FormatAtResult(r AtResult) string
- func MaybeResize(img *Image) error
- func MentionPath(p string) string
- func StripANSI(s string) string
- type AtMention
- type AtResult
- type DropType
- type Image
- type PDF
Constants ¶
This section is empty.
Variables ¶
var ErrNoImage = errors.New("no image on clipboard")
ErrNoImage is returned when the clipboard contains no image data.
var ErrNoPDF = errors.New("no PDF on clipboard")
ErrNoPDF is returned when the clipboard contains no PDF data.
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 ¶
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 ¶
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 ¶
FormatAtResult wraps a file attachment in the XML envelope that CC uses.
func MaybeResize ¶
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 ¶
MentionPath builds an @mention string for an arbitrary dropped file path.
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 ¶
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 ¶
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.
func DroppedFileType ¶
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 ¶
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 ¶
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 ¶
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 ¶
ReadPDFFile reads a PDF from disk and returns it as an attach.PDF.