فهرست منبع

feat: render forwarded messages (#538)

Ted 11 ماه پیش
والد
کامیت
5efbed2d8c
2فایلهای تغییر یافته به همراه26 افزوده شده و 3 حذف شده
  1. 24 3
      cmd/messages_text.go
  2. 2 0
      internal/config/theme.go

+ 24 - 3
cmd/messages_text.go

@@ -126,7 +126,11 @@ func (mt *MessagesText) createMsg(msg discord.Message) {
 
 	switch msg.Type {
 	case discord.DefaultMessage:
-		mt.createDefaultMsg(msg)
+		if msg.Reference != nil && msg.Reference.Type == discord.MessageReferenceTypeForward {
+			mt.createForwardedMsg(msg)
+		} else {
+			mt.createDefaultMsg(msg)
+		}
 	case discord.InlinedReplyMessage:
 		mt.createReplyMsg(msg)
 
@@ -141,9 +145,12 @@ func (mt *MessagesText) createMsg(msg discord.Message) {
 	fmt.Fprintln(mt)
 }
 
+func (mt *MessagesText) formatTimestamp(ts discord.Timestamp) string {
+	return ts.Time().In(time.Local).Format(mt.cfg.Timestamps.Format)
+}
+
 func (mt *MessagesText) drawTimestamps(ts discord.Timestamp) {
-	time := ts.Time().In(time.Local).Format(mt.cfg.Timestamps.Format)
-	fmt.Fprintf(mt, "[::d]%s[::D] ", time)
+	fmt.Fprintf(mt, "[::d]%s[::D] ", mt.formatTimestamp(ts))
 }
 
 func (mt *MessagesText) drawAuthor(msg discord.Message) {
@@ -158,6 +165,12 @@ func (mt *MessagesText) drawContent(msg discord.Message) {
 	markdown.DefaultRenderer.Render(mt, c, ast)
 }
 
+func (mt *MessagesText) drawSnapshotContent(msg discord.MessageSnapshotMessage) {
+	c := []byte(tview.Escape(msg.Content))
+	// discordmd doesn't support MessageSnapshotMessage, so we just use write it as is. todo?
+	mt.Write(c)
+}
+
 func (mt *MessagesText) createDefaultMsg(msg discord.Message) {
 	if mt.cfg.Timestamps.Enabled {
 		mt.drawTimestamps(msg.Timestamp)
@@ -206,6 +219,14 @@ func (mt *MessagesText) authorName(user discord.User, gID discord.GuildID) strin
 	return name
 }
 
+func (mt *MessagesText) createForwardedMsg(msg discord.Message) {
+	mt.drawTimestamps(msg.Timestamp)
+	mt.drawAuthor(msg)
+	fmt.Fprintf(mt, "[::d]%s [::-]", mt.cfg.Theme.MessagesText.ForwardedIndicator)
+	mt.drawSnapshotContent(msg.MessageSnapshots[0].Message)
+	fmt.Fprintf(mt, " [::d](%s)[-:-:-] ", mt.formatTimestamp(msg.MessageSnapshots[0].Message.Timestamp))
+}
+
 func (mt *MessagesText) authorColor(user discord.User, gID discord.GuildID) string {
 	color := mt.cfg.Theme.MessagesText.AuthorColor
 	if app.cfg.Theme.MessagesText.ShowUsernameColors && gID.IsValid() {

+ 2 - 0
internal/config/theme.go

@@ -59,6 +59,7 @@ type (
 		ShowUsernameColors bool `toml:"show_user_colors"`
 
 		ReplyIndicator string `toml:"reply_indicator"`
+		ForwardedIndicator string `toml:"forwarded_indicator"`
 
 		AuthorColor     string `toml:"author_color"`
 		ContentColor    string `toml:"content_color"`
@@ -99,6 +100,7 @@ func defaultTheme() Theme {
 			ShowUsernameColors: true,
 
 			ReplyIndicator: ">",
+			ForwardedIndicator: "<",
 
 			AuthorColor:     "aqua",
 			ContentColor:    tview.Styles.PrimaryTextColor.String(),