messages_text.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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) newMessage(m *discord.Message) error {
  37. mt.buffer.Reset()
  38. switch m.Type {
  39. case discord.DefaultMessage, discord.InlinedReplyMessage:
  40. // Region tags are square brackets that contain a region ID in double quotes
  41. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  42. mt.buffer.WriteString(`["`)
  43. mt.buffer.WriteString(m.ID.String())
  44. mt.buffer.WriteString(`"]`)
  45. if m.ReferencedMessage != nil {
  46. mt.buffer.WriteString("[::d] ")
  47. mt.buffer.WriteRune(replyIndicator)
  48. mt.buffer.WriteByte(' ')
  49. // Author
  50. mt.newAuthor(m.ReferencedMessage)
  51. // Content
  52. mt.newContent(m.ReferencedMessage)
  53. mt.buffer.WriteString("[::-]\n")
  54. }
  55. if cfg.Timestamps {
  56. // Timestamps
  57. mt.newTimestamp(m)
  58. }
  59. // Author
  60. mt.newAuthor(m)
  61. // Content
  62. mt.newContent(m)
  63. // Attachments
  64. mt.newAttachments(m)
  65. // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
  66. mt.buffer.WriteString(`[""]`)
  67. mt.buffer.WriteByte('\n')
  68. }
  69. _, err := mt.buffer.WriteTo(mt)
  70. return err
  71. }
  72. func (mt *MessagesText) newAuthor(m *discord.Message) {
  73. mt.buffer.WriteByte('[')
  74. mt.buffer.WriteString(cfg.Theme.MessagesText.AuthorColor)
  75. mt.buffer.WriteByte(']')
  76. mt.buffer.WriteString(m.Author.Username)
  77. mt.buffer.WriteString("[-] ")
  78. }
  79. func (mt *MessagesText) newTimestamp(m *discord.Message) {
  80. mt.buffer.WriteString("[::d]")
  81. mt.buffer.WriteString(m.Timestamp.Format(time.Kitchen))
  82. mt.buffer.WriteString("[::-] ")
  83. }
  84. func (mt *MessagesText) newContent(m *discord.Message) {
  85. mt.buffer.WriteString(discordmd.Parse(tview.Escape(m.Content)))
  86. }
  87. func (mt *MessagesText) newAttachments(m *discord.Message) {
  88. for _, a := range m.Attachments {
  89. mt.buffer.WriteByte('\n')
  90. mt.buffer.WriteByte('[')
  91. mt.buffer.WriteString(a.Filename)
  92. mt.buffer.WriteString("]: ")
  93. mt.buffer.WriteString(a.URL)
  94. }
  95. }
  96. func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
  97. if len(added) == 0 {
  98. return
  99. }
  100. sf, err := discord.ParseSnowflake(added[0])
  101. if err != nil {
  102. log.Println(err)
  103. return
  104. }
  105. m, err := discordState.Cabinet.Message(guildsTree.selectedChannel.ID, discord.MessageID(sf))
  106. if err != nil {
  107. log.Println(err)
  108. return
  109. }
  110. mt.selectedMessage = m
  111. }
  112. func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  113. switch event.Name() {
  114. case cfg.Keys.MessagesText.Reply:
  115. mt.replyAction(false)
  116. return nil
  117. case cfg.Keys.MessagesText.ReplyMention:
  118. mt.replyAction(true)
  119. return nil
  120. case cfg.Keys.MessagesText.Cancel:
  121. // TODO
  122. guildsTree.selectedChannel = nil
  123. mt.selectedMessage = nil
  124. mt.Clear()
  125. mt.SetTitle("")
  126. mt.Highlight()
  127. messageInput.SetText("")
  128. messageInput.SetTitle("")
  129. return nil
  130. }
  131. return event
  132. }
  133. func (mt *MessagesText) replyAction(mention bool) {
  134. if mt.selectedMessage == nil {
  135. return
  136. }
  137. var title string
  138. if mention {
  139. title += "[@] Replying to "
  140. } else {
  141. title += "Replying to "
  142. }
  143. title += mt.selectedMessage.Author.Tag()
  144. messageInput.SetTitle(title)
  145. app.SetFocus(messageInput)
  146. }