Jelajahi Sumber

copy content action

ayn2op 3 tahun lalu
induk
melakukan
cd8177f0c7
5 mengubah file dengan 47 tambahan dan 0 penghapusan
  1. 4 0
      config.go
  2. 1 0
      go.mod
  3. 2 0
      go.sum
  4. 22 0
      main.go
  5. 18 0
      messages_text.go

+ 4 - 0
config.go

@@ -11,6 +11,8 @@ const name = "discordo"
 
 type (
 	MessagesTextKeysConfig struct {
+		CopyContent string `yaml:"copy_content"`
+
 		Reply        string `yaml:"reply"`
 		ReplyMention string `yaml:"reply_mention"`
 		SelectReply  string `yaml:"select_reply"`
@@ -90,6 +92,8 @@ func newConfig() (*Config, error) {
 			Cancel: "Esc",
 
 			MessagesText: MessagesTextKeysConfig{
+				CopyContent: "Rune[c]",
+
 				Reply:        "Rune[r]",
 				ReplyMention: "Rune[R]",
 				SelectReply:  "Rune[s]",

+ 1 - 0
go.mod

@@ -3,6 +3,7 @@ module github.com/ayn2op/discordo
 go 1.19
 
 require (
+	github.com/atotto/clipboard v0.1.4
 	github.com/diamondburned/arikawa/v3 v3.2.0
 	github.com/gdamore/tcell/v2 v2.5.4
 	github.com/rivo/tview v0.0.0-20230104153304-892d1a2eb0da

+ 2 - 0
go.sum

@@ -1,3 +1,5 @@
+github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
+github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
 github.com/diamondburned/arikawa/v3 v3.2.0 h1:aBUhg94pxblT6ks4EV7qxEk44tnl0ico67ydqjVnv9g=
 github.com/diamondburned/arikawa/v3 v3.2.0/go.mod h1:5jBSNnp82Z/EhsKa6Wk9FsOqSxfVkNZDTDBPOj47LpY=
 github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=

+ 22 - 0
main.go

@@ -4,6 +4,8 @@ import (
 	"context"
 	"flag"
 	"log"
+	"os"
+	"path/filepath"
 
 	"github.com/rivo/tview"
 )
@@ -24,6 +26,26 @@ var (
 
 func init() {
 	flag.StringVar(&token, "token", "", "The authentication token.")
+
+	path, err := os.UserCacheDir()
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	path = filepath.Join(path, name)
+	err = os.MkdirAll(path, os.ModePerm)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	path = filepath.Join(path, "logs.txt")
+	f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND, os.ModePerm)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	log.SetOutput(f)
+	log.SetFlags(log.LstdFlags | log.Llongfile)
 }
 
 func main() {

+ 18 - 0
messages_text.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"time"
 
+	"github.com/atotto/clipboard"
 	"github.com/ayn2op/discordo/discordmd"
 	"github.com/diamondburned/arikawa/v3/discord"
 	"github.com/gdamore/tcell/v2"
@@ -125,6 +126,9 @@ func (mt *MessagesText) createFooter(m *discord.Message) {
 
 func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
 	switch event.Name() {
+	case config.Keys.MessagesText.CopyContent:
+		mt.copyContentAction()
+		return nil
 	case config.Keys.MessagesText.Reply:
 		mt.replyAction(false)
 		return nil
@@ -268,3 +272,17 @@ func (mt *MessagesText) selectReplyAction() {
 		mt.ScrollToHighlight()
 	}
 }
+
+func (mt *MessagesText) copyContentAction() {
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	err = clipboard.WriteAll(ms[mt.selectedMessage].Content)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+}