util.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package ui
  2. import (
  3. "strings"
  4. "github.com/ayn2op/discordo/internal/config"
  5. "github.com/ayn2op/tview"
  6. "github.com/diamondburned/arikawa/v3/discord"
  7. )
  8. // ConfigureBox configures the provided box according to the provided theme.
  9. func ConfigureBox(box *tview.Box, cfg *config.Theme) *tview.Box {
  10. border := cfg.Border
  11. normalBorderStyle, activeBorderStyle := border.NormalStyle.Style, border.ActiveStyle.Style
  12. normalBorderSet, activeBorderSet := border.NormalSet.BorderSet, border.ActiveSet.BorderSet
  13. title := cfg.Title
  14. normalTitleStyle, activeTitleStyle := title.NormalStyle.Style, title.ActiveStyle.Style
  15. footer := cfg.Footer
  16. normalFooterStyle, activeFooterStyle := footer.NormalStyle.Style, footer.ActiveStyle.Style
  17. padding := border.Padding
  18. box.
  19. SetBorderStyle(normalBorderStyle).
  20. SetBorderSet(normalBorderSet).
  21. SetBorderPadding(padding[0], padding[1], padding[2], padding[3]).
  22. SetTitleStyle(normalTitleStyle).
  23. SetTitleAlignment(title.Alignment.Alignment).
  24. SetFooterStyle(normalFooterStyle).
  25. SetFooterAlignment(footer.Alignment.Alignment).
  26. SetBlurFunc(func() {
  27. box.
  28. SetBorderStyle(normalBorderStyle).
  29. SetBorderSet(normalBorderSet)
  30. box.SetTitleStyle(normalTitleStyle).SetFooterStyle(normalFooterStyle)
  31. }).
  32. SetFocusFunc(func() {
  33. box.
  34. SetBorderStyle(activeBorderStyle).
  35. SetBorderSet(activeBorderSet)
  36. box.SetTitleStyle(activeTitleStyle).SetFooterStyle(activeFooterStyle)
  37. })
  38. if border.Enabled {
  39. box.SetBorders(tview.BordersAll)
  40. }
  41. return box
  42. }
  43. // Centered creates a new grid with provided primitive aligned in the center.
  44. func Centered(p tview.Primitive, width, height int) tview.Primitive {
  45. return tview.NewGrid().
  46. SetColumns(0, width, 0).
  47. SetRows(0, height, 0).
  48. AddItem(p, 1, 1, 1, 1, 0, 0, true)
  49. }
  50. func ChannelToString(channel discord.Channel) string {
  51. switch channel.Type {
  52. case discord.DirectMessage, discord.GroupDM:
  53. if channel.Name != "" {
  54. return channel.Name
  55. }
  56. recipients := make([]string, len(channel.DMRecipients))
  57. for i, r := range channel.DMRecipients {
  58. recipients[i] = r.DisplayOrUsername()
  59. }
  60. return strings.Join(recipients, ", ")
  61. case discord.GuildText:
  62. return "#" + channel.Name
  63. case discord.GuildVoice, discord.GuildStageVoice:
  64. return "v-" + channel.Name
  65. case discord.GuildAnnouncement:
  66. return "a-" + channel.Name
  67. case discord.GuildStore:
  68. return "s-" + channel.Name
  69. case discord.GuildForum:
  70. return "f-" + channel.Name
  71. case discord.GuildPublicThread, discord.GuildPrivateThread, discord.GuildAnnouncementThread:
  72. return "t-" + channel.Name
  73. default:
  74. return channel.Name
  75. }
  76. }