messages_text.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. import (
  3. "bytes"
  4. "log"
  5. "time"
  6. "github.com/ayn2op/discordo/discordmd"
  7. "github.com/diamondburned/arikawa/v3/discord"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rivo/tview"
  10. )
  11. const replyIndicator = '╭'
  12. type MessagesText struct {
  13. *tview.TextView
  14. buffer bytes.Buffer
  15. selectedMessage *discord.Message
  16. }
  17. func newMessagesText() *MessagesText {
  18. mt := &MessagesText{
  19. TextView: tview.NewTextView(),
  20. }
  21. mt.SetDynamicColors(true)
  22. mt.SetRegions(true)
  23. mt.SetWordWrap(true)
  24. mt.ScrollToEnd()
  25. mt.SetHighlightedFunc(mt.onHighlighted)
  26. mt.SetInputCapture(mt.onInputCapture)
  27. mt.SetBackgroundColor(tcell.GetColor(cfg.Theme.MessagesText.BackgroundColor))
  28. mt.SetTitle("Messages")
  29. mt.SetTitleColor(tcell.GetColor(cfg.Theme.MessagesText.TitleColor))
  30. mt.SetTitleAlign(tview.AlignLeft)
  31. padding := cfg.Theme.MessagesText.BorderPadding
  32. mt.SetBorder(cfg.Theme.MessagesText.Border)
  33. mt.SetBorderPadding(padding[0], padding[1], padding[2], padding[3])
  34. return mt
  35. }
  36. func (mt *MessagesText) reset() {
  37. messagesText.selectedMessage = nil
  38. mt.SetTitle("")
  39. mt.Clear()
  40. mt.Highlight()
  41. }
  42. func (mt *MessagesText) newMessage(m *discord.Message) error {
  43. mt.buffer.Reset()
  44. switch m.Type {
  45. case discord.DefaultMessage, discord.InlinedReplyMessage:
  46. // Region tags are square brackets that contain a region ID in double quotes
  47. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  48. mt.buffer.WriteString(`["`)
  49. mt.buffer.WriteString(m.ID.String())
  50. mt.buffer.WriteString(`"]`)
  51. if m.ReferencedMessage != nil {
  52. mt.buffer.WriteString("[::d] ")
  53. mt.buffer.WriteRune(replyIndicator)
  54. mt.buffer.WriteByte(' ')
  55. // Author
  56. mt.newAuthor(m.ReferencedMessage)
  57. // Content
  58. mt.newContent(m.ReferencedMessage)
  59. mt.buffer.WriteString("[::-]\n")
  60. }
  61. if cfg.Timestamps {
  62. // Timestamps
  63. mt.newTimestamp(m)
  64. }
  65. // Author
  66. mt.newAuthor(m)
  67. // Content
  68. mt.newContent(m)
  69. // Attachments
  70. mt.newAttachments(m)
  71. // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
  72. mt.buffer.WriteString(`[""]`)
  73. mt.buffer.WriteByte('\n')
  74. }
  75. _, err := mt.buffer.WriteTo(mt)
  76. return err
  77. }
  78. func (mt *MessagesText) newAuthor(m *discord.Message) {
  79. mt.buffer.WriteByte('[')
  80. mt.buffer.WriteString(cfg.Theme.MessagesText.AuthorColor)
  81. mt.buffer.WriteByte(']')
  82. mt.buffer.WriteString(m.Author.Username)
  83. mt.buffer.WriteString("[-] ")
  84. }
  85. func (mt *MessagesText) newTimestamp(m *discord.Message) {
  86. mt.buffer.WriteString("[::d]")
  87. mt.buffer.WriteString(m.Timestamp.Format(time.Kitchen))
  88. mt.buffer.WriteString("[::-] ")
  89. }
  90. func (mt *MessagesText) newContent(m *discord.Message) {
  91. mt.buffer.WriteString(discordmd.Parse(tview.Escape(m.Content)))
  92. }
  93. func (mt *MessagesText) newAttachments(m *discord.Message) {
  94. for _, a := range m.Attachments {
  95. mt.buffer.WriteByte('\n')
  96. mt.buffer.WriteByte('[')
  97. mt.buffer.WriteString(a.Filename)
  98. mt.buffer.WriteString("]: ")
  99. mt.buffer.WriteString(a.URL)
  100. }
  101. }
  102. func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
  103. if len(added) == 0 {
  104. return
  105. }
  106. sf, err := discord.ParseSnowflake(added[0])
  107. if err != nil {
  108. log.Println(err)
  109. return
  110. }
  111. m, err := discordState.Cabinet.Message(guildsTree.selectedChannel.ID, discord.MessageID(sf))
  112. if err != nil {
  113. log.Println(err)
  114. return
  115. }
  116. mt.selectedMessage = m
  117. }
  118. func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  119. switch event.Name() {
  120. case cfg.Keys.MessagesText.Reply:
  121. mt.replyAction(false)
  122. return nil
  123. case cfg.Keys.MessagesText.ReplyMention:
  124. mt.replyAction(true)
  125. return nil
  126. case cfg.Keys.MessagesText.Cancel:
  127. // TODO
  128. guildsTree.selectedChannel = nil
  129. messagesText.reset()
  130. messageInput.reset()
  131. return nil
  132. }
  133. return event
  134. }
  135. func (mt *MessagesText) replyAction(mention bool) {
  136. if mt.selectedMessage == nil {
  137. return
  138. }
  139. var title string
  140. if mention {
  141. title += "[@] Replying to "
  142. } else {
  143. title += "Replying to "
  144. }
  145. title += mt.selectedMessage.Author.Tag()
  146. messageInput.SetTitle(title)
  147. app.SetFocus(messageInput)
  148. }