ui.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package util
  2. import (
  3. "strings"
  4. "github.com/ayntgl/discordgo"
  5. "github.com/rivo/tview"
  6. )
  7. // GetNodeByReference walks the root `*TreeNode` of the given `*TreeView` *treeView* and returns the TreeNode whose reference is equal to the given reference *r*. If the `*TreeNode` is not found, `nil` is returned instead.
  8. func GetNodeByReference(treeView *tview.TreeView, r interface{}) (mn *tview.TreeNode) {
  9. treeView.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
  10. if n.GetReference() == r {
  11. mn = n
  12. return false
  13. }
  14. return true
  15. })
  16. return
  17. }
  18. // ChannelToString constructs a string representation of the given channel. The string representation may vary for different channel types.
  19. func ChannelToString(c *discordgo.Channel) string {
  20. var repr string
  21. if c.Name != "" {
  22. repr = "#" + c.Name
  23. } else if len(c.Recipients) == 1 {
  24. rp := c.Recipients[0]
  25. repr = rp.Username + "#" + rp.Discriminator
  26. } else {
  27. rps := make([]string, len(c.Recipients))
  28. for i, r := range c.Recipients {
  29. rps[i] = r.Username + "#" + r.Discriminator
  30. }
  31. repr = strings.Join(rps, ", ")
  32. }
  33. return repr
  34. }