refactor(ui): share one clipboard write command

This commit is contained in:
ayn2op 2026-10-16 18:31:39 -04:00
parent b9e310e7fa
commit 12a4a6af48
4 changed files with 25 additions and 40 deletions

View file

@ -20,7 +20,6 @@ import (
"github.com/ayn2op/tview/viewport" "github.com/ayn2op/tview/viewport"
"github.com/ncruces/zenity" "github.com/ncruces/zenity"
"github.com/skratchdot/open-golang/open" "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. // 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 nil
} }
return func() tview.Msg { return ui.WriteClipboard(selectedMessage.ID.String())
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
}
} }
func (m Model) yankContent() tview.Cmd { func (m Model) yankContent() tview.Cmd {
@ -128,12 +122,7 @@ func (m Model) yankContent() tview.Cmd {
return nil return nil
} }
return func() tview.Msg { return ui.WriteClipboard(selectedMessage.Content)
if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(selectedMessage.Content)); err != nil {
slog.Error("failed to write to clipboard", "err", err)
}
return nil
}
} }
func (m Model) yankURL() tview.Cmd { func (m Model) yankURL() tview.Cmd {
@ -142,12 +131,7 @@ func (m Model) yankURL() tview.Cmd {
return nil return nil
} }
return func() tview.Msg { return ui.WriteClipboard(selectedMessage.URL())
if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(selectedMessage.URL())); err != nil {
slog.Error("failed to write to clipboard", "err", err)
}
return nil
}
} }
func (m Model) open() tview.Cmd { func (m Model) open() tview.Cmd {

19
internal/ui/clipboard.go Normal file
View file

@ -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
}
}

View file

@ -1,23 +1,12 @@
package login package login
import ( import (
"context"
"log/slog" "log/slog"
"github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/discordo/internal/ui"
"github.com/ayn2op/tview" "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. // selectTabMsg switches to the tab at an index.
type selectTabMsg int type selectTabMsg int
@ -25,7 +14,7 @@ func showErrorDialog(err error) tview.Cmd {
slog.Error("failed to login", "err", err) slog.Error("failed to login", "err", err)
message := err.Error() message := err.Error()
return ui.ShowDialog(message, 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"}, ui.DialogButton{Label: "Close"},
) )
} }

View file

@ -1,13 +1,11 @@
package tree package tree
import ( import (
"context"
"fmt" "fmt"
"log/slog"
"github.com/ayn2op/discordo/internal/ui"
"github.com/ayn2op/tview" "github.com/ayn2op/tview"
"github.com/ayn2op/tview/tree" "github.com/ayn2op/tview/tree"
"golang.design/x/clipboard"
) )
// YankID copies the ID of node to the clipboard. // YankID copies the ID of node to the clipboard.
@ -20,10 +18,5 @@ func YankID(node *tree.Node) tview.Cmd {
if !ok { if !ok {
return nil return nil
} }
return func() tview.Msg { return ui.WriteClipboard(id.String())
if _, err := clipboard.Write(context.Background(), clipboard.FmtText, []byte(id.String())); err != nil {
slog.Error("failed to write to clipboard", "err", err)
}
return nil
}
} }