messages_text.go 3.2 KB

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