messages_text.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.ScrollToEnd()
  19. mt.SetBorder(cfg.Theme.MessagesText.Border)
  20. padding := cfg.Theme.MessagesText.BorderPadding
  21. mt.SetBorderPadding(padding[0], padding[1], padding[2], padding[3])
  22. return mt
  23. }
  24. func (mt *MessagesText) newMessage(m *discord.Message) error {
  25. switch m.Type {
  26. case discord.DefaultMessage, discord.InlinedReplyMessage:
  27. // Region tags are square brackets that contain a region ID in double quotes
  28. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  29. fmt.Fprintf(mt, `["%s"]`, m.ID)
  30. if m.ReferencedMessage != nil {
  31. fmt.Fprint(mt, "[::d] ╭ ")
  32. // Author
  33. mt.newAuthor(m.ReferencedMessage)
  34. // Content
  35. mt.newContent(m.ReferencedMessage)
  36. fmt.Fprint(mt, "[::-]")
  37. fmt.Fprintln(mt)
  38. }
  39. if cfg.Timestamps {
  40. // Timestamps
  41. mt.newTimestamp(m)
  42. }
  43. // Author
  44. mt.newAuthor(m)
  45. // Content
  46. mt.newContent(m)
  47. // Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region.
  48. fmt.Fprint(mt, `[""]`)
  49. }
  50. fmt.Fprintln(mt)
  51. return nil
  52. }
  53. func (mt *MessagesText) newAuthor(m *discord.Message) {
  54. fmt.Fprintf(mt, "[blue]%s[-] ", m.Author.Username)
  55. }
  56. func (mt *MessagesText) newTimestamp(m *discord.Message) {
  57. fmt.Fprintf(mt, "[::d]%s[::-] ", m.Timestamp.Format(time.Kitchen))
  58. }
  59. func (mt *MessagesText) newContent(m *discord.Message) {
  60. fmt.Fprint(mt, tview.Escape(m.Content))
  61. }