on main help sign in
feat: paste as file (#1800)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

commit1e83e6657d52c1a7746a6b9425861de1cf83ff28 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateFri 9 Jan 2026, 08:44 (9 months ago) parent7c99274
1 file changed+38
internal/ui/model/ui.go+38
internal/ui/model/ui.go+38
@@ -9,7 +9,9 @@ import (
99 "net/http"
1010 "os"
1111 "path/filepath"
12+ "regexp"
1213 "slices"
14+ "strconv"
1315 "strings"
1416
1517 "charm.land/bubbles/v2/help"
@@ -1917,6 +1919,25 @@ func (m *UI) handlePasteMsg(msg tea.PasteMsg) tea.Cmd {
19171919 return nil
19181920 }
19191921
1922+ // If pasted text has more than 2 newlines, treat it as a file attachment.
1923+ if strings.Count(msg.Content, "\n") > 2 {
1924+ return func() tea.Msg {
1925+ content := []byte(msg.Content)
1926+ if int64(len(content)) > maxAttachmentSize {
1927+ return uiutil.ReportWarn("Paste is too big (>5mb)")
1928+ }
1929+ name := fmt.Sprintf("paste_%d.txt", m.pasteIdx())
1930+ mimeBufferSize := min(512, len(content))
1931+ mimeType := http.DetectContentType(content[:mimeBufferSize])
1932+ return message.Attachment{
1933+ FileName: name,
1934+ FilePath: name,
1935+ MimeType: mimeType,
1936+ Content: content,
1937+ }
1938+ }
1939+ }
1940+
19201941 var cmd tea.Cmd
19211942 path := strings.ReplaceAll(msg.Content, "\\ ", " ")
19221943 // Try to get an image.
@@ -1966,6 +1987,23 @@ func (m *UI) handlePasteMsg(msg tea.PasteMsg) tea.Cmd {
19661987 }
19671988}
19681989
1990+var pasteRE = regexp.MustCompile(`paste_(\d+).txt`)
1991+
1992+func (m *UI) pasteIdx() int {
1993+ result := 0
1994+ for _, at := range m.attachments.List() {
1995+ found := pasteRE.FindStringSubmatch(at.FileName)
1996+ if len(found) == 0 {
1997+ continue
1998+ }
1999+ idx, err := strconv.Atoi(found[1])
2000+ if err == nil {
2001+ result = max(result, idx)
2002+ }
2003+ }
2004+ return result + 1
2005+}
2006+
19692007// renderLogo renders the Crush logo with the given styles and dimensions.
19702008func renderLogo(t *styles.Styles, compact bool, width int) string {
19712009 return logo.Render(version.Version, compact, logo.Opts{