refactor: move utility functions into separate package
This commit is contained in:
parent
2a7206b6a3
commit
207a4d43bd
5 changed files with 87 additions and 75 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/gen2brain/beeep"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
|
@ -71,11 +72,11 @@ func onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
}
|
||||
}
|
||||
|
||||
cn := getTreeNodeByReference(c.ID)
|
||||
cn := util.GetTreeNodeByReference(channelsTree, c.ID)
|
||||
if cn == nil {
|
||||
return
|
||||
}
|
||||
cn.SetText("[::b]" + generateChannelRepr(c) + "[::-]")
|
||||
cn.SetText("[::b]" + util.ChannelToString(c) + "[::-]")
|
||||
app.Draw()
|
||||
} else {
|
||||
selectedChannel.Messages = append(selectedChannel.Messages, m.Message)
|
||||
|
|
|
|||
19
ui.go
19
ui.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
|
@ -76,18 +77,18 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
|
|||
|
||||
switch c.Type {
|
||||
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews:
|
||||
title := generateChannelRepr(c)
|
||||
title := util.ChannelToString(c)
|
||||
if c.Topic != "" {
|
||||
title += " - " + c.Topic
|
||||
}
|
||||
|
||||
messagesView.SetTitle(title)
|
||||
case discordgo.ChannelTypeDM, discordgo.ChannelTypeGroupDM:
|
||||
messagesView.SetTitle(generateChannelRepr(c))
|
||||
messagesView.SetTitle(util.ChannelToString(c))
|
||||
}
|
||||
|
||||
if strings.HasPrefix(n.GetText(), "[::b]") {
|
||||
n.SetText("[::d]" + generateChannelRepr(c) + "[::-]")
|
||||
n.SetText("[::d]" + util.ChannelToString(c) + "[::-]")
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
|
@ -103,7 +104,7 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
|
|||
// Scroll to the end of the text after the messages have been written to the TextView.
|
||||
messagesView.ScrollToEnd()
|
||||
|
||||
if len(ms) != 0 && isUnread(c) {
|
||||
if len(ms) != 0 && util.ChannelIsUnread(session.State, c) {
|
||||
session.ChannelMessageAck(c.ID, c.LastMessageID, "")
|
||||
}
|
||||
}()
|
||||
|
|
@ -184,9 +185,9 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|||
return nil
|
||||
}
|
||||
|
||||
_, m := findByMessageID(hs[0])
|
||||
_, m := util.FindMessageByID(selectedChannel.Messages, hs[0])
|
||||
if m.ReferencedMessage != nil {
|
||||
selectedMessage, _ = findByMessageID(m.ReferencedMessage.ID)
|
||||
selectedMessage, _ = util.FindMessageByID(selectedChannel.Messages, m.ReferencedMessage.ID)
|
||||
messagesView.
|
||||
Highlight(m.ReferencedMessage.ID).
|
||||
ScrollToHighlight()
|
||||
|
|
@ -199,7 +200,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|||
return nil
|
||||
}
|
||||
|
||||
_, m := findByMessageID(hs[0])
|
||||
_, m := util.FindMessageByID(selectedChannel.Messages, hs[0])
|
||||
messageInputField.SetTitle("Replying to " + m.Author.String())
|
||||
app.SetFocus(messageInputField)
|
||||
return nil
|
||||
|
|
@ -209,7 +210,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|||
return nil
|
||||
}
|
||||
|
||||
_, m := findByMessageID(hs[0])
|
||||
_, m := util.FindMessageByID(selectedChannel.Messages, hs[0])
|
||||
messageInputField.SetTitle("[@] Replying to " + m.Author.String())
|
||||
app.SetFocus(messageInputField)
|
||||
return nil
|
||||
|
|
@ -219,7 +220,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
|
|||
return nil
|
||||
}
|
||||
|
||||
_, m := findByMessageID(hs[0])
|
||||
_, m := util.FindMessageByID(selectedChannel.Messages, hs[0])
|
||||
err := clipboard.WriteAll(m.Content)
|
||||
if err != nil {
|
||||
return nil
|
||||
|
|
|
|||
72
util.go
72
util.go
|
|
@ -2,41 +2,12 @@ package main
|
|||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
"github.com/ayntgl/discordo/util"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func generateChannelRepr(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
|
||||
}
|
||||
|
||||
func findByMessageID(mID string) (int, *discordgo.Message) {
|
||||
for i, m := range selectedChannel.Messages {
|
||||
if m.ID == mID {
|
||||
return i, m
|
||||
}
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func createPrivateChannels(n *tview.TreeNode) {
|
||||
cs := session.State.PrivateChannels
|
||||
sort.Slice(cs, func(i, j int) bool {
|
||||
|
|
@ -45,13 +16,13 @@ func createPrivateChannels(n *tview.TreeNode) {
|
|||
|
||||
for _, c := range cs {
|
||||
var tag string
|
||||
if isUnread(c) {
|
||||
if util.ChannelIsUnread(session.State, c) {
|
||||
tag = "[::b]"
|
||||
} else {
|
||||
tag = "[::d]"
|
||||
}
|
||||
|
||||
cn := tview.NewTreeNode(tag + generateChannelRepr(c) + "[::-]").
|
||||
cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
|
||||
SetReference(c.ID)
|
||||
n.AddChild(cn)
|
||||
}
|
||||
|
|
@ -107,13 +78,13 @@ func createTopLevelChannelsTreeNodes(
|
|||
}
|
||||
|
||||
var tag string
|
||||
if isUnread(c) {
|
||||
if util.ChannelIsUnread(session.State, c) {
|
||||
tag = "[::b]"
|
||||
} else {
|
||||
tag = "[::d]"
|
||||
}
|
||||
|
||||
cn := tview.NewTreeNode(tag + generateChannelRepr(c) + "[::-]").
|
||||
cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
|
||||
SetReference(c.ID)
|
||||
n.AddChild(cn)
|
||||
continue
|
||||
|
|
@ -159,45 +130,18 @@ func createSecondLevelChannelsTreeNodes(cs []*discordgo.Channel) {
|
|||
}
|
||||
|
||||
var tag string
|
||||
if isUnread(c) {
|
||||
if util.ChannelIsUnread(session.State, c) {
|
||||
tag = "[::b]"
|
||||
} else {
|
||||
tag = "[::d]"
|
||||
}
|
||||
|
||||
pn := getTreeNodeByReference(c.ParentID)
|
||||
pn := util.GetTreeNodeByReference(channelsTree, c.ParentID)
|
||||
if pn != nil {
|
||||
cn := tview.NewTreeNode(tag + generateChannelRepr(c) + "[::-]").
|
||||
cn := tview.NewTreeNode(tag + util.ChannelToString(c) + "[::-]").
|
||||
SetReference(c.ID)
|
||||
pn.AddChild(cn)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getTreeNodeByReference(r interface{}) (mn *tview.TreeNode) {
|
||||
channelsTree.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
|
||||
if n.GetReference() == r {
|
||||
mn = n
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func isUnread(c *discordgo.Channel) bool {
|
||||
if c.LastMessageID == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, rs := range session.State.ReadState {
|
||||
if c.ID == rs.ID {
|
||||
return c.LastMessageID != rs.LastMessageID
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
50
util/discord.go
Normal file
50
util/discord.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/ayntgl/discordgo"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func ChannelIsUnread(s *discordgo.State, c *discordgo.Channel) bool {
|
||||
if c.LastMessageID == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, rs := range s.ReadState {
|
||||
if c.ID == rs.ID {
|
||||
return c.LastMessageID != rs.LastMessageID
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
|
||||
for i, m := range ms {
|
||||
if m.ID == mID {
|
||||
return i, m
|
||||
}
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
16
util/ui.go
Normal file
16
util/ui.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package util
|
||||
|
||||
import "github.com/rivo/tview"
|
||||
|
||||
func GetTreeNodeByReference(tv *tview.TreeView, r interface{}) (mn *tview.TreeNode) {
|
||||
tv.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
|
||||
if n.GetReference() == r {
|
||||
mn = n
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue