fix: improve long text detection to account for long text in a single line (#2442)

Andrey Nering created

Change summary

internal/ui/model/ui.go | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)

Detailed changes

internal/ui/model/ui.go 🔗

@@ -66,9 +66,12 @@ const (
 	compactModeHeightBreakpoint = 30
 )
 
-// If pasted text has more than 2 newlines, treat it as a file attachment.
+// If pasted text has more than 10 newlines, treat it as a file attachment.
 const pasteLinesThreshold = 10
 
+// If pasted text has more than 1000 columns, treat it as a file attachment.
+const pasteColsThreshold = 1000
+
 // Session details panel max height.
 const sessionDetailsMaxHeight = 20
 
@@ -3160,7 +3163,7 @@ func (m *UI) handlePasteMsg(msg tea.PasteMsg) tea.Cmd {
 		return nil
 	}
 
-	if strings.Count(msg.Content, "\n") > pasteLinesThreshold {
+	if hasPasteExceededThreshold(msg) {
 		return func() tea.Msg {
 			content := []byte(msg.Content)
 			if int64(len(content)) > common.MaxAttachmentSize {
@@ -3218,6 +3221,22 @@ func (m *UI) handlePasteMsg(msg tea.PasteMsg) tea.Cmd {
 	return tea.Batch(cmds...)
 }
 
+func hasPasteExceededThreshold(msg tea.PasteMsg) bool {
+	var (
+		lineCount = 0
+		colCount  = 0
+	)
+	for line := range strings.SplitSeq(msg.Content, "\n") {
+		lineCount++
+		colCount = max(colCount, len(line))
+
+		if lineCount > pasteLinesThreshold || colCount > pasteColsThreshold {
+			return true
+		}
+	}
+	return false
+}
+
 // handleFilePathPaste handles a pasted file path.
 func (m *UI) handleFilePathPaste(path string) tea.Cmd {
 	return func() tea.Msg {