@@ -24,6 +24,7 @@ import (
2424 tea "charm.land/bubbletea/v2"
2525 "charm.land/catwalk/pkg/catwalk"
2626 "charm.land/lipgloss/v2"
27+ nativeclipboard "github.com/aymanbagabas/go-nativeclipboard"
2728 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
2829 "github.com/charmbracelet/crush/internal/app"
2930 "github.com/charmbracelet/crush/internal/commands"
@@ -1549,6 +1550,9 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
15491550 cmds = append(cmds, cmd)
15501551 }
15511552
1553+ case key.Matches(msg, m.keyMap.Editor.PasteImage):
1554+ cmds = append(cmds, m.pasteImageFromClipboard)
1555+
15521556 case key.Matches(msg, m.keyMap.Editor.SendMessage):
15531557 value := m.textarea.Value()
15541558 if before, ok := strings.CutSuffix(value, "\\"); ok {
@@ -2081,6 +2085,7 @@ func (m *UI) FullHelp() [][]key.Binding {
20812085 []key.Binding{
20822086 k.Editor.Newline,
20832087 k.Editor.AddImage,
2088+ k.Editor.PasteImage,
20842089 k.Editor.MentionFile,
20852090 k.Editor.OpenEditor,
20862091 },
@@ -2129,6 +2134,7 @@ func (m *UI) FullHelp() [][]key.Binding {
21292134 []key.Binding{
21302135 k.Editor.Newline,
21312136 k.Editor.AddImage,
2137+ k.Editor.PasteImage,
21322138 k.Editor.MentionFile,
21332139 k.Editor.OpenEditor,
21342140 },
@@ -3061,6 +3067,80 @@ func (m *UI) handleFilePathPaste(path string) tea.Cmd {
30613067 }
30623068}
30633069
3070+// pasteImageFromClipboard reads image data from the system clipboard and
3071+// creates an attachment. If no image data is found, it falls back to
3072+// interpreting clipboard text as a file path.
3073+func (m *UI) pasteImageFromClipboard() tea.Msg {
3074+ imageData, err := nativeclipboard.Image.Read()
3075+ if int64(len(imageData)) > common.MaxAttachmentSize {
3076+ return util.InfoMsg{
3077+ Type: util.InfoTypeError,
3078+ Msg: "File too large, max 5MB",
3079+ }
3080+ }
3081+ name := fmt.Sprintf("paste_%d.png", m.pasteIdx())
3082+ if err == nil {
3083+ return message.Attachment{
3084+ FilePath: name,
3085+ FileName: name,
3086+ MimeType: mimeOf(imageData),
3087+ Content: imageData,
3088+ }
3089+ }
3090+
3091+ textData, textErr := nativeclipboard.Text.Read()
3092+ if textErr != nil || len(textData) == 0 {
3093+ return util.NewInfoMsg("Clipboard is empty or does not contain an image")
3094+ }
3095+
3096+ path := strings.TrimSpace(string(textData))
3097+ path = strings.ReplaceAll(path, "\\ ", " ")
3098+ if _, statErr := os.Stat(path); statErr != nil {
3099+ return util.NewInfoMsg("Clipboard does not contain an image or valid file path")
3100+ }
3101+
3102+ lowerPath := strings.ToLower(path)
3103+ isAllowed := false
3104+ for _, ext := range common.AllowedImageTypes {
3105+ if strings.HasSuffix(lowerPath, ext) {
3106+ isAllowed = true
3107+ break
3108+ }
3109+ }
3110+ if !isAllowed {
3111+ return util.NewInfoMsg("File type is not a supported image format")
3112+ }
3113+
3114+ fileInfo, statErr := os.Stat(path)
3115+ if statErr != nil {
3116+ return util.InfoMsg{
3117+ Type: util.InfoTypeError,
3118+ Msg: fmt.Sprintf("Unable to read file: %v", statErr),
3119+ }
3120+ }
3121+ if fileInfo.Size() > common.MaxAttachmentSize {
3122+ return util.InfoMsg{
3123+ Type: util.InfoTypeError,
3124+ Msg: "File too large, max 5MB",
3125+ }
3126+ }
3127+
3128+ content, readErr := os.ReadFile(path)
3129+ if readErr != nil {
3130+ return util.InfoMsg{
3131+ Type: util.InfoTypeError,
3132+ Msg: fmt.Sprintf("Unable to read file: %v", readErr),
3133+ }
3134+ }
3135+
3136+ return message.Attachment{
3137+ FilePath: path,
3138+ FileName: filepath.Base(path),
3139+ MimeType: mimeOf(content),
3140+ Content: content,
3141+ }
3142+}
3143+
30643144var pasteRE = regexp.MustCompile(`paste_(\d+).txt`)
30653145
30663146func (m *UI) pasteIdx() int {