@@ -56,6 +56,7 @@ type editorComponent struct {
5656 exitKeyInDebounce bool
5757 historyIndex int // -1 means current (not in history)
5858 currentText string // Store current text when navigating history
59+ pasteCounter int
5960}
6061
6162func (m * editorComponent ) Init () tea.Cmd {
@@ -129,28 +130,47 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
129130 text , err := strconv .Unquote (`"` + text + `"` )
130131 if err != nil {
131132 slog .Error ("Failed to unquote text" , "error" , err )
132- m .textarea .InsertRunesFromUserInput ([]rune (msg ))
133+ text := string (msg )
134+ if m .shouldSummarizePastedText (text ) {
135+ m .handleLongPaste (text )
136+ } else {
137+ m .textarea .InsertRunesFromUserInput ([]rune (msg ))
138+ }
133139 return m , nil
134140 }
135141 if _ , err := os .Stat (text ); err != nil {
136142 slog .Error ("Failed to paste file" , "error" , err )
137- m .textarea .InsertRunesFromUserInput ([]rune (msg ))
143+ text := string (msg )
144+ if m .shouldSummarizePastedText (text ) {
145+ m .handleLongPaste (text )
146+ } else {
147+ m .textarea .InsertRunesFromUserInput ([]rune (msg ))
148+ }
138149 return m , nil
139150 }
140151
141152 filePath := text
142153
143154 attachment := m .createAttachmentFromFile (filePath )
144155 if attachment == nil {
145- m .textarea .InsertRunesFromUserInput ([]rune (msg ))
156+ if m .shouldSummarizePastedText (text ) {
157+ m .handleLongPaste (text )
158+ } else {
159+ m .textarea .InsertRunesFromUserInput ([]rune (msg ))
160+ }
146161 return m , nil
147162 }
148163
149164 m .textarea .InsertAttachment (attachment )
150165 m .textarea .InsertString (" " )
151166 case tea.ClipboardMsg :
152167 text := string (msg )
153- m .textarea .InsertRunesFromUserInput ([]rune (text ))
168+ // Check if the pasted text is long and should be summarized
169+ if m .shouldSummarizePastedText (text ) {
170+ m .handleLongPaste (text )
171+ } else {
172+ m .textarea .InsertRunesFromUserInput ([]rune (text ))
173+ }
154174 case dialog.ThemeSelectedMsg :
155175 m .textarea = updateTextareaStyles (m .textarea )
156176 m .spinner = createSpinner ()
@@ -392,6 +412,7 @@ func (m *editorComponent) Clear() (tea.Model, tea.Cmd) {
392412 m .textarea .Reset ()
393413 m .historyIndex = - 1
394414 m .currentText = ""
415+ m .pasteCounter = 0
395416 return m , nil
396417}
397418
@@ -421,7 +442,13 @@ func (m *editorComponent) Paste() (tea.Model, tea.Cmd) {
421442
422443 textBytes := clipboard .Read (clipboard .FmtText )
423444 if textBytes != nil {
424- m .textarea .InsertRunesFromUserInput ([]rune (string (textBytes )))
445+ text := string (textBytes )
446+ // Check if the pasted text is long and should be summarized
447+ if m .shouldSummarizePastedText (text ) {
448+ m .handleLongPaste (text )
449+ } else {
450+ m .textarea .InsertRunesFromUserInput ([]rune (text ))
451+ }
425452 return m , nil
426453 }
427454
@@ -490,6 +517,48 @@ func (m *editorComponent) getExitKeyText() string {
490517 return m .app .Commands [commands .AppExitCommand ].Keys ()[0 ]
491518}
492519
520+ // shouldSummarizePastedText determines if pasted text should be summarized
521+ func (m * editorComponent ) shouldSummarizePastedText (text string ) bool {
522+ lines := strings .Split (text , "\n " )
523+ lineCount := len (lines )
524+ charCount := len (text )
525+
526+ // Consider text long if it has more than 3 lines or more than 150 characters
527+ return lineCount > 3 || charCount > 150
528+ }
529+
530+ // handleLongPaste handles long pasted text by creating a summary attachment
531+ func (m * editorComponent ) handleLongPaste (text string ) {
532+ lines := strings .Split (text , "\n " )
533+ lineCount := len (lines )
534+
535+ // Increment paste counter
536+ m .pasteCounter ++
537+
538+ // Create attachment with full text as base64 encoded data
539+ fileBytes := []byte (text )
540+ base64EncodedText := base64 .StdEncoding .EncodeToString (fileBytes )
541+ url := fmt .Sprintf ("data:text/plain;base64,%s" , base64EncodedText )
542+
543+ fileName := fmt .Sprintf ("pasted-text-%d.txt" , m .pasteCounter )
544+ displayText := fmt .Sprintf ("[pasted #%d %d+ lines]" , m .pasteCounter , lineCount )
545+
546+ attachment := & attachment.Attachment {
547+ ID : uuid .NewString (),
548+ Type : "text" ,
549+ MediaType : "text/plain" ,
550+ Display : displayText ,
551+ URL : url ,
552+ Filename : fileName ,
553+ Source : & attachment.TextSource {
554+ Value : text ,
555+ },
556+ }
557+
558+ m .textarea .InsertAttachment (attachment )
559+ m .textarea .InsertString (" " )
560+ }
561+
493562func updateTextareaStyles (ta textarea.Model ) textarea.Model {
494563 t := theme .CurrentTheme ()
495564 bgColor := t .BackgroundElement ()
@@ -551,6 +620,7 @@ func NewEditorComponent(app *app.App) EditorComponent {
551620 spinner : s ,
552621 interruptKeyInDebounce : false ,
553622 historyIndex : - 1 ,
623+ pasteCounter : 0 ,
554624 }
555625
556626 return m
0 commit comments