From f116be593c2b8c27daf0a764826468419623c7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sat, 3 Apr 2021 22:24:20 -0500 Subject: [PATCH] Implemented open and share --- go.mod | 1 + go.sum | 2 ++ mast/cmd.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/go.mod b/go.mod index e45085d..4dab33f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/mrusme/gomphotherium go 1.15 require ( + github.com/atotto/clipboard v0.1.4 // indirect github.com/eliukblau/pixterm v1.3.1 github.com/gdamore/tcell/v2 v2.2.0 // indirect github.com/gizak/termui/v3 v3.1.0 diff --git a/go.sum b/go.sum index 0e79b62..e20f66f 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= diff --git a/mast/cmd.go b/mast/cmd.go index 3cbcfda..04b0d3e 100644 --- a/mast/cmd.go +++ b/mast/cmd.go @@ -5,6 +5,10 @@ import ( "strconv" "regexp" "errors" + "os/exec" + "runtime" + + "github.com/atotto/clipboard" ) type CmdReturnCode int @@ -192,6 +196,20 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { } return CmdUnfav(timeline, tootId) + case "open": + tootId, err := CmdHelperGetOpenParams(args) + if err != nil { + return CodeNotOk + } + + return CmdOpen(timeline, tootId) + case "share": + tootId, err := CmdHelperGetShareParams(args) + if err != nil { + return CodeNotOk + } + + return CmdShare(timeline, tootId) case "quit", "exit", "bye": return CodeQuit } @@ -233,6 +251,14 @@ func CmdHelperGetFavParams(args string) (int, error) { return CmdHelperGetTootIDFromString(args) } +func CmdHelperGetOpenParams(args string) (int, error) { + return CmdHelperGetTootIDFromString(args) +} + +func CmdHelperGetShareParams(args string) (int, error) { + return CmdHelperGetTootIDFromString(args) +} + func CmdToot(timeline *Timeline, content string, inReplyTo int, visibility string) (CmdReturnCode) { var status string = "" var spoiler string = "" @@ -302,3 +328,37 @@ func CmdUnfav(timeline *Timeline, tootID int) (CmdReturnCode) { return CodeOk } + +func CmdOpen(timeline *Timeline, tootID int) (CmdReturnCode) { + var err error + + url := timeline.Toots[tootID].Status.URL + + switch runtime.GOOS { + case "darwin": + err = exec.Command("open", url).Start() + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + default: + err = errors.New("Platform not supported!") + } + + if err != nil { + return CodeNotOk + } + + return CodeOk +} + +func CmdShare(timeline *Timeline, tootID int) (CmdReturnCode) { + url := timeline.Toots[tootID].Status.URL + + err := clipboard.WriteAll(url) + if err != nil { + return CodeNotOk + } + + return CodeOk +}