From 28d2f556b2a4197b8628bd6e5dbe25807dd56482 Mon Sep 17 00:00:00 2001 From: summus Date: Sat, 6 Nov 2021 10:11:09 +0800 Subject: [PATCH 1/2] fix: writing allocated url to clipboard doesn't work on some linux device --- cmd/alloc.go | 3 ++- internal/clipboard/local.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/alloc.go b/cmd/alloc.go index 11902ba..f9cd1c9 100644 --- a/cmd/alloc.go +++ b/cmd/alloc.go @@ -54,7 +54,8 @@ func allocate(dstpath, srcpath string) { status.Convert(err).Message()) } if out.URL != "" { - clipboard.Local.Write(types.MIMEPlainText, utils.StringToBytes(out.URL)) + //block util clipboard is updated + clipboard.WriteAndWaitChange(types.MIMEPlainText, utils.StringToBytes(out.URL)) fmt.Println(out.URL) } else { fmt.Printf("%v\n", out.Message) diff --git a/internal/clipboard/local.go b/internal/clipboard/local.go index 5f71f68..7b7bcc3 100644 --- a/internal/clipboard/local.go +++ b/internal/clipboard/local.go @@ -82,3 +82,18 @@ func (lc *local) Watch(ctx context.Context, dt types.MIME) <-chan []byte { } return nil } + +// WriteAndWaitChange writes the given buffer to the clipboard and wait util clipboard is updated. +// The reason do so is that Writing clipboard doesn't work when exiting immediately after +// golang.design/x/clipboard.Write return which cause URL can't be written into clipboard +// after allocation on my Linux device(Manjaro 21.0.2 with GNOME 3.38.5) +func WriteAndWaitChange(t types.MIME, buf []byte) { + var ch <-chan struct{} + switch t { + case types.MIMEPlainText: + ch = clipboard.Write(clipboard.FmtText, buf) + case types.MIMEImagePNG: + ch = clipboard.Write(clipboard.FmtImage, buf) + } + <-ch +} From c0e1fc6d6f8dec40ce21e1da3c909ae5d01d5382 Mon Sep 17 00:00:00 2001 From: Changkun Ou Date: Sun, 7 Nov 2021 10:38:57 +0100 Subject: [PATCH 2/2] cmd: remove clipboard write from command --- cmd/alloc.go | 7 ++----- internal/clipboard/local.go | 15 --------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/cmd/alloc.go b/cmd/alloc.go index f9cd1c9..da83780 100644 --- a/cmd/alloc.go +++ b/cmd/alloc.go @@ -10,10 +10,7 @@ import ( "log" "changkun.de/x/midgard/api/daemon" - "changkun.de/x/midgard/internal/clipboard" - "changkun.de/x/midgard/internal/types" "changkun.de/x/midgard/internal/types/proto" - "changkun.de/x/midgard/internal/utils" "github.com/spf13/cobra" "google.golang.org/grpc/status" ) @@ -54,8 +51,8 @@ func allocate(dstpath, srcpath string) { status.Convert(err).Message()) } if out.URL != "" { - //block util clipboard is updated - clipboard.WriteAndWaitChange(types.MIMEPlainText, utils.StringToBytes(out.URL)) + // Clipboard is updated on the daemon side, we don't have to + // write clipboard in the allocate command again, see PR#16. fmt.Println(out.URL) } else { fmt.Printf("%v\n", out.Message) diff --git a/internal/clipboard/local.go b/internal/clipboard/local.go index 7b7bcc3..5f71f68 100644 --- a/internal/clipboard/local.go +++ b/internal/clipboard/local.go @@ -82,18 +82,3 @@ func (lc *local) Watch(ctx context.Context, dt types.MIME) <-chan []byte { } return nil } - -// WriteAndWaitChange writes the given buffer to the clipboard and wait util clipboard is updated. -// The reason do so is that Writing clipboard doesn't work when exiting immediately after -// golang.design/x/clipboard.Write return which cause URL can't be written into clipboard -// after allocation on my Linux device(Manjaro 21.0.2 with GNOME 3.38.5) -func WriteAndWaitChange(t types.MIME, buf []byte) { - var ch <-chan struct{} - switch t { - case types.MIMEPlainText: - ch = clipboard.Write(clipboard.FmtText, buf) - case types.MIMEImagePNG: - ch = clipboard.Write(clipboard.FmtImage, buf) - } - <-ch -}