messages_text.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Author
  39. mt.newAuthor(m)
  40. // Timestamps
  41. mt.newTimestamp(m)
  42. // Content
  43. fmt.Fprintln(mt)
  44. mt.newContent(m)
  45. // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
  46. fmt.Fprint(mt, `[""]`)
  47. }
  48. fmt.Fprint(mt, "\n\n")
  49. return nil
  50. }
  51. func (mt *MessagesText) newAuthor(m *discord.Message) {
  52. fmt.Fprintf(mt, "[blue]%s[-] ", m.Author.Username)
  53. }
  54. func (mt *MessagesText) newTimestamp(m *discord.Message) {
  55. fmt.Fprintf(mt, "[::d]%s[::-]", m.Timestamp.Format(time.Kitchen))
  56. }
  57. func (mt *MessagesText) newContent(m *discord.Message) {
  58. fmt.Fprint(mt, tview.Escape(m.Content))
  59. }