messages_text.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/diamondburned/arikawa/v3/discord"
  6. "github.com/rivo/tview"
  7. )
  8. type MessagesText struct {
  9. *tview.TextView
  10. }
  11. func newMessagesText() *MessagesText {
  12. mt := &MessagesText{
  13. TextView: tview.NewTextView(),
  14. }
  15. mt.SetDynamicColors(true)
  16. mt.SetRegions(true)
  17. mt.SetWordWrap(true)
  18. mt.SetBorder(cfg.Theme.MessagesText.Border)
  19. padding := cfg.Theme.MessagesText.BorderPadding
  20. mt.SetBorderPadding(padding[0], padding[1], padding[2], padding[3])
  21. return mt
  22. }
  23. func (mt *MessagesText) newMessage(m *discord.Message) error {
  24. switch m.Type {
  25. case discord.DefaultMessage, discord.InlinedReplyMessage:
  26. // Region tags are square brackets that contain a region ID in double quotes
  27. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  28. fmt.Fprintf(mt, `["%s"]`, m.ID)
  29. if m.ReferencedMessage != nil {
  30. fmt.Fprint(mt, "> [::d]")
  31. // Author
  32. mt.newAuthor(m.ReferencedMessage)
  33. // Content
  34. mt.newContent(m.ReferencedMessage)
  35. fmt.Fprint(mt, "[::-]")
  36. fmt.Fprintln(mt)
  37. }
  38. // Timestamps
  39. mt.newTimestamp(m)
  40. // Author
  41. mt.newAuthor(m)
  42. // Content
  43. mt.newContent(m)
  44. // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
  45. fmt.Fprint(mt, `[""]`)
  46. }
  47. fmt.Fprintln(mt)
  48. return nil
  49. }
  50. func (mt *MessagesText) newAuthor(m *discord.Message) {
  51. fmt.Fprintf(mt, "[blue]%s[-] ", m.Author.Username)
  52. }
  53. func (mt *MessagesText) newTimestamp(m *discord.Message) {
  54. fmt.Fprintf(mt, "[::d]%s[::-] ", m.Timestamp.Format(time.Kitchen))
  55. }
  56. func (mt *MessagesText) newContent(m *discord.Message) {
  57. fmt.Fprint(mt, tview.Escape(m.Content))
  58. }