refactor: move ChannelToString() and GetNodeByReference() to util/ui.go

This commit is contained in:
ayntgl 2021-12-02 21:10:17 +04:00
parent 88600de318
commit 313a754ade
3 changed files with 56 additions and 45 deletions

View file

@ -8,6 +8,7 @@ import (
"strings"
"github.com/ayntgl/discordgo"
"github.com/ayntgl/discordo/util"
"github.com/gen2brain/beeep"
"github.com/rivo/tview"
)
@ -93,11 +94,11 @@ func onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
}
}
cn := getTreeNodeByReference(channelsTree, c.ID)
cn := util.GetNodeByReference(channelsTree, c.ID)
if cn == nil {
return
}
cn.SetText("[::b]" + channelToString(c) + "[::-]")
cn.SetText("[::b]" + util.ChannelToString(c) + "[::-]")
app.Draw()
} else {
selectedChannel.Messages = append(selectedChannel.Messages, m.Message)
@ -109,26 +110,6 @@ func onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
}
}
// channelToString constructs a string representation of the given channel. The string representation may vary for different channel types.
func channelToString(c *discordgo.Channel) string {
var repr string
if c.Name != "" {
repr = "#" + c.Name
} else if len(c.Recipients) == 1 {
rp := c.Recipients[0]
repr = rp.Username + "#" + rp.Discriminator
} else {
rps := make([]string, len(c.Recipients))
for i, r := range c.Recipients {
rps[i] = r.Username + "#" + r.Discriminator
}
repr = strings.Join(rps, ", ")
}
return repr
}
type loginResponse struct {
MFA bool `json:"mfa"`
SMS bool `json:"sms"`

34
ui.go
View file

@ -6,6 +6,7 @@ import (
"github.com/atotto/clipboard"
"github.com/ayntgl/discordgo"
"github.com/ayntgl/discordo/util"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
@ -57,7 +58,7 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
tag = "[::b]"
}
cn := tview.NewTreeNode(tag + channelToString(c) + "[::-]").
cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
SetReference(c.ID).
Collapse()
n.AddChild(cn)
@ -93,18 +94,18 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
switch c.Type {
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
title := channelToString(c)
title := util.ChannelToString(c)
if c.Topic != "" {
title += " - " + c.Topic
}
messagesView.SetTitle(title)
case discordgo.ChannelTypeDM, discordgo.ChannelTypeGroupDM:
messagesView.SetTitle(channelToString(c))
messagesView.SetTitle(util.ChannelToString(c))
}
if strings.HasPrefix(n.GetText(), "[::b]") {
n.SetText("[::d]" + channelToString(c) + "[::-]")
n.SetText("[::d]" + util.ChannelToString(c) + "[::-]")
}
go func() {
@ -172,7 +173,7 @@ func createSecondLevelChannelsNodes(treeView *tview.TreeView, s *discordgo.State
continue
}
pn := getTreeNodeByReference(treeView, c.ParentID)
pn := util.GetNodeByReference(treeView, c.ParentID)
if pn != nil {
pn.AddChild(createChannelNode(s, c))
}
@ -356,20 +357,6 @@ func newLoginForm(onLoginFormLoginButtonSelected func(), mfa bool) *tview.Form {
return w
}
// getTreeNodeByReference 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.
func getTreeNodeByReference(treeView *tview.TreeView, r interface{}) (mn *tview.TreeNode) {
treeView.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
if n.GetReference() == r {
mn = n
return false
}
return true
})
return
}
// createChannelNode builds (encorporates unread channels in bold tag, otherwise dim, etc.) and returns a node according to the type of the given channel *c*.
func createChannelNode(s *discordgo.State, c *discordgo.Channel) *tview.TreeNode {
var cn *tview.TreeNode
@ -380,7 +367,7 @@ func createChannelNode(s *discordgo.State, c *discordgo.Channel) *tview.TreeNode
tag = "[::b]"
}
cn = tview.NewTreeNode(tag + channelToString(c) + "[::-]").
cn = tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
SetReference(c.ID)
case discordgo.ChannelTypeGuildCategory:
cn = tview.NewTreeNode(c.Name).
@ -400,9 +387,10 @@ func hasPermission(s *discordgo.State, cID string, p int64) bool {
return perm&p == p
}
func hasKeybinding(sl []string, s string) bool {
for _, str := range sl {
if str == s {
// hasKeybinding returns a boolean that indicates whether the given keybinding string representation *k* is in the slice *ks*.
func hasKeybinding(ks []string, k string) bool {
for _, repr := range ks {
if repr == k {
return true
}
}

42
util/ui.go Normal file
View file

@ -0,0 +1,42 @@
package util
import (
"strings"
"github.com/ayntgl/discordgo"
"github.com/rivo/tview"
)
// 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.
func GetNodeByReference(treeView *tview.TreeView, r interface{}) (mn *tview.TreeNode) {
treeView.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
if n.GetReference() == r {
mn = n
return false
}
return true
})
return
}
// ChannelToString constructs a string representation of the given channel. The string representation may vary for different channel types.
func ChannelToString(c *discordgo.Channel) string {
var repr string
if c.Name != "" {
repr = "#" + c.Name
} else if len(c.Recipients) == 1 {
rp := c.Recipients[0]
repr = rp.Username + "#" + rp.Discriminator
} else {
rps := make([]string, len(c.Recipients))
for i, r := range c.Recipients {
rps[i] = r.Username + "#" + r.Discriminator
}
repr = strings.Join(rps, ", ")
}
return repr
}