util.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. title := cfg.Title
  12. normalBorderStyle, activeBorderStyle := border.NormalStyle.Style, border.ActiveStyle.Style
  13. normalBorderSet, activeBorderSet := border.NormalSet.BorderSet, border.ActiveSet.BorderSet
  14. normalTitleStyle, activeTitleStyle := title.NormalStyle.Style, title.ActiveStyle.Style
  15. p := border.Padding
  16. box.
  17. SetBorderStyle(normalBorderStyle).
  18. SetBorderSet(normalBorderSet).
  19. SetBorderPadding(p[0], p[1], p[2], p[3]).
  20. SetTitleStyle(normalTitleStyle).
  21. SetTitleAlignment(title.Alignment.Alignment).
  22. SetBlurFunc(func() {
  23. box.
  24. SetBorderStyle(normalBorderStyle).
  25. SetBorderSet(normalBorderSet)
  26. box.SetTitleStyle(normalTitleStyle)
  27. }).
  28. SetFocusFunc(func() {
  29. box.
  30. SetBorderStyle(activeBorderStyle).
  31. SetBorderSet(activeBorderSet)
  32. box.SetTitleStyle(activeTitleStyle)
  33. })
  34. if border.Enabled {
  35. box.SetBorders(tview.BordersAll)
  36. }
  37. return box
  38. }
  39. // Centered creates a new grid with provided primitive aligned in the center.
  40. func Centered(p tview.Primitive, width, height int) tview.Primitive {
  41. return tview.NewGrid().
  42. SetColumns(0, width, 0).
  43. SetRows(0, height, 0).
  44. AddItem(p, 1, 1, 1, 1, 0, 0, true)
  45. }
  46. func ChannelToString(channel discord.Channel) string {
  47. switch channel.Type {
  48. case discord.DirectMessage, discord.GroupDM:
  49. if channel.Name != "" {
  50. return channel.Name
  51. }
  52. recipients := make([]string, len(channel.DMRecipients))
  53. for i, r := range channel.DMRecipients {
  54. recipients[i] = r.DisplayOrUsername()
  55. }
  56. return strings.Join(recipients, ", ")
  57. case discord.GuildText:
  58. return "#" + channel.Name
  59. case discord.GuildVoice, discord.GuildStageVoice:
  60. return "v-" + channel.Name
  61. case discord.GuildAnnouncement:
  62. return "a-" + channel.Name
  63. case discord.GuildStore:
  64. return "s-" + channel.Name
  65. case discord.GuildForum:
  66. return "f-" + channel.Name
  67. default:
  68. return channel.Name
  69. }
  70. }