From 0f85fcc3fac833b1635af111e1a7024254fcf3c6 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Sun, 26 Apr 2020 20:02:35 +0300 Subject: Implement copying to clipboard "github.com/atotto/clipboard" --- ui/command-processor.go | 1 + ui/commands.go | 10 ++++++++++ ui/room-view.go | 6 ++++++ 3 files changed, 17 insertions(+) (limited to 'ui') diff --git a/ui/command-processor.go b/ui/command-processor.go index 10478cc..e9485c4 100644 --- a/ui/command-processor.go +++ b/ui/command-processor.go @@ -119,6 +119,7 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor { "react": cmdReact, "download": cmdDownload, "open": cmdOpen, + "copy": cmdCopy, "sendevent": cmdSendEvent, "msendevent": cmdMSendEvent, "setstate": cmdSetState, diff --git a/ui/commands.go b/ui/commands.go index 4dd1482..27c9caf 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -34,6 +34,7 @@ import ( "github.com/lucasb-eyer/go-colorful" "github.com/russross/blackfriday/v2" + "github.com/atotto/clipboard" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" @@ -158,6 +159,7 @@ const ( SelectRedact = "redact" SelectDownload = "download" SelectOpen = "open" + SelectCopy = "copy" ) func cmdReply(cmd *Command) { @@ -176,6 +178,14 @@ func cmdOpen(cmd *Command) { cmd.Room.StartSelecting(SelectOpen, strings.Join(cmd.Args, " ")) } +func cmdCopy(cmd *Command) { + if clipboard.Unsupported { + cmd.Reply("Clipboard unsupported.") + } else { + cmd.Room.StartSelecting(SelectCopy, strings.Join(cmd.Args, " ")) + } +} + func cmdReact(cmd *Command) { if len(cmd.Args) == 0 { cmd.Reply("Usage: /react ") diff --git a/ui/room-view.go b/ui/room-view.go index 5792626..641e9d1 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -25,6 +25,7 @@ import ( "github.com/kyokomi/emoji" "github.com/mattn/go-runewidth" + "github.com/atotto/clipboard" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -204,6 +205,11 @@ func (view *RoomView) OnSelect(message *messages.UIMessage) { } go view.Download(msg.URL, path, view.selectReason == SelectOpen) } + case SelectCopy: + msg, ok := message.Renderer.(*messages.TextMessage) + if ok { + go clipboard.WriteAll(msg.PlainText()) + } } view.selecting = false view.selectContent = "" -- cgit v1.2.3 From b0983a47c9cb8499ca91327cc8b70e7752e911ad Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Sun, 26 Apr 2020 22:19:41 +0300 Subject: Use github.com/zyedidia/clipboard for clipboard. Now one can choose as register clipboard and primary. --- ui/commands.go | 11 +++++++---- ui/room-view.go | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'ui') diff --git a/ui/commands.go b/ui/commands.go index 27c9caf..ed853a2 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -34,7 +34,6 @@ import ( "github.com/lucasb-eyer/go-colorful" "github.com/russross/blackfriday/v2" - "github.com/atotto/clipboard" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" @@ -179,10 +178,14 @@ func cmdOpen(cmd *Command) { } func cmdCopy(cmd *Command) { - if clipboard.Unsupported { - cmd.Reply("Clipboard unsupported.") + register := strings.Join(cmd.Args, " ") + if len(register) == 0 { + register = "clipboard" + } + if (register == "clipboard" || register == "primary") { + cmd.Room.StartSelecting(SelectCopy, register) } else { - cmd.Room.StartSelecting(SelectCopy, strings.Join(cmd.Args, " ")) + cmd.Reply("Usage: /copy [register], where register is either \"clipboard\" or \"primary\". Defaults to \"clipboard\".") } } diff --git a/ui/room-view.go b/ui/room-view.go index 641e9d1..e9523d9 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -25,7 +25,7 @@ import ( "github.com/kyokomi/emoji" "github.com/mattn/go-runewidth" - "github.com/atotto/clipboard" + "github.com/zyedidia/clipboard" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -208,7 +208,7 @@ func (view *RoomView) OnSelect(message *messages.UIMessage) { case SelectCopy: msg, ok := message.Renderer.(*messages.TextMessage) if ok { - go clipboard.WriteAll(msg.PlainText()) + go view.CopyToClipboard(msg.PlainText(),view.selectContent) } } view.selecting = false @@ -630,6 +630,21 @@ func (view *RoomView) InputSubmit(text string) { view.SetInputText("") } +func (view *RoomView) CopyToClipboard(text string, register string) { + if (register == "clipboard" || register == "primary") { + err := clipboard.WriteAll(text,register) + if err != nil { + view.AddServiceMessage(fmt.Sprintf("Clipboard unsupported: %v", err)) + view.parent.parent.Render() + return + } + } else { + view.AddServiceMessage(fmt.Sprintf("Clipboard register %v unsupported", register)) + view.parent.parent.Render() + return + } +} + func (view *RoomView) Download(url id.ContentURI, filename string, openFile bool) { path, err := view.parent.matrix.DownloadToDisk(url, filename) if err != nil { -- cgit v1.2.3 From 12fb6c9076d0f70ef38f104e2bcf703084d85373 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Sun, 26 Apr 2020 22:36:33 +0300 Subject: remove unnecessary returns --- ui/room-view.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'ui') diff --git a/ui/room-view.go b/ui/room-view.go index e9523d9..0cdc7f1 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -636,12 +636,10 @@ func (view *RoomView) CopyToClipboard(text string, register string) { if err != nil { view.AddServiceMessage(fmt.Sprintf("Clipboard unsupported: %v", err)) view.parent.parent.Render() - return } } else { view.AddServiceMessage(fmt.Sprintf("Clipboard register %v unsupported", register)) view.parent.parent.Render() - return } } -- cgit v1.2.3