messages_text.go 3.1 KB

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