diff --git a/internal/ui/chat/messageslist/msg.go b/internal/ui/chat/messageslist/msg.go index 316255c..6798ab7 100644 --- a/internal/ui/chat/messageslist/msg.go +++ b/internal/ui/chat/messageslist/msg.go @@ -20,7 +20,6 @@ import ( "github.com/ayn2op/tview/viewport" "github.com/ncruces/zenity" "github.com/skratchdot/open-golang/open" - "golang.design/x/clipboard" ) // Msg is implemented by the messages the list sends itself, which must reach it whatever has the focus. @@ -114,12 +113,7 @@ func (m Model) yankMessageID() tview.Cmd { return nil } - return func() tview.Msg { - if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(selectedMessage.ID.String())); err != nil { - slog.Error("failed to write to clipboard", "err", err) - } - return nil - } + return ui.WriteClipboard(selectedMessage.ID.String()) } func (m Model) yankContent() tview.Cmd { @@ -128,12 +122,7 @@ func (m Model) yankContent() tview.Cmd { return nil } - return func() tview.Msg { - if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(selectedMessage.Content)); err != nil { - slog.Error("failed to write to clipboard", "err", err) - } - return nil - } + return ui.WriteClipboard(selectedMessage.Content) } func (m Model) yankURL() tview.Cmd { @@ -142,12 +131,7 @@ func (m Model) yankURL() tview.Cmd { return nil } - return func() tview.Msg { - if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(selectedMessage.URL())); err != nil { - slog.Error("failed to write to clipboard", "err", err) - } - return nil - } + return ui.WriteClipboard(selectedMessage.URL()) } func (m Model) open() tview.Cmd { diff --git a/internal/ui/clipboard.go b/internal/ui/clipboard.go new file mode 100644 index 0000000..6fdf5c5 --- /dev/null +++ b/internal/ui/clipboard.go @@ -0,0 +1,19 @@ +package ui + +import ( + "context" + "log/slog" + + "github.com/ayn2op/tview" + "golang.design/x/clipboard" +) + +// WriteClipboard copies text to the clipboard. +func WriteClipboard(text string) tview.Cmd { + return func() tview.Msg { + if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(text)); err != nil { + slog.Error("failed to write to clipboard", "err", err) + } + return nil + } +} diff --git a/internal/ui/login/msg.go b/internal/ui/login/msg.go index 89750e8..297bbb6 100644 --- a/internal/ui/login/msg.go +++ b/internal/ui/login/msg.go @@ -1,23 +1,12 @@ package login import ( - "context" "log/slog" "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" - "golang.design/x/clipboard" ) -func setClipboard(content string) tview.Cmd { - return func() tview.Msg { - if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(content)); err != nil { - slog.Error("failed to write to clipboard", "err", err) - } - return nil - } -} - // selectTabMsg switches to the tab at an index. type selectTabMsg int @@ -25,7 +14,7 @@ func showErrorDialog(err error) tview.Cmd { slog.Error("failed to login", "err", err) message := err.Error() return ui.ShowDialog(message, - ui.DialogButton{Label: "Copy", Cmd: setClipboard(message), KeepOpen: true}, + ui.DialogButton{Label: "Copy", Cmd: ui.WriteClipboard(message), KeepOpen: true}, ui.DialogButton{Label: "Close"}, ) } diff --git a/internal/ui/tree/msg.go b/internal/ui/tree/msg.go index 5cb1990..3df99d9 100644 --- a/internal/ui/tree/msg.go +++ b/internal/ui/tree/msg.go @@ -1,13 +1,11 @@ package tree import ( - "context" "fmt" - "log/slog" + "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" "github.com/ayn2op/tview/tree" - "golang.design/x/clipboard" ) // YankID copies the ID of node to the clipboard. @@ -20,10 +18,5 @@ func YankID(node *tree.Node) tview.Cmd { if !ok { return nil } - return func() tview.Msg { - if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(id.String())); err != nil { - slog.Error("failed to write to clipboard", "err", err) - } - return nil - } + return ui.WriteClipboard(id.String()) }