messages_text.go 3.1 KB

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