style: shorten long lines (>80 characters)

This commit is contained in:
rigormorrtiss 2021-08-31 13:03:12 +04:00
parent 5656faf2e6
commit 4bd98d596d
6 changed files with 114 additions and 32 deletions

View file

@ -38,7 +38,11 @@ func main() {
guildsTreeView = ui.NewGuildsTreeView(onGuildsTreeViewSelected)
messagesTextView = ui.NewMessagesTextView(app)
messageInputField = ui.NewMessageInputField(onMessageInputFieldInputCapture)
mainFlex = ui.NewMainFlex(guildsTreeView, messagesTextView, messageInputField)
mainFlex = ui.NewMainFlex(
guildsTreeView,
messagesTextView,
messageInputField,
)
token := config.Token
if t := util.GetPassword("token"); t != "" {
@ -153,7 +157,11 @@ func onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
func onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
if selectedChannel != nil && selectedChannel.ID == m.ChannelID {
util.WriteMessage(messagesTextView, m.Message, session.State.Ready.User.ID)
util.WriteMessage(
messagesTextView,
m.Message,
session.State.Ready.User.ID,
)
}
}
@ -211,7 +219,11 @@ func onGuildsTreeViewSelected(n *tview.TreeNode) {
func writeMessages(cID string) {
msgs, _ := session.ChannelMessages(cID, config.GetMessagesLimit, "", "", "")
for i := len(msgs) - 1; i >= 0; i-- {
util.WriteMessage(messagesTextView, msgs[i], session.State.Ready.User.ID)
util.WriteMessage(
messagesTextView,
msgs[i],
session.State.Ready.User.ID,
)
}
}

View file

@ -5,7 +5,11 @@ import (
)
// NewMainFlex creates and returns a new main flex.
func NewMainFlex(treeV *tview.TreeView, textV *tview.TextView, i *tview.InputField) *tview.Flex {
func NewMainFlex(
treeV *tview.TreeView,
textV *tview.TextView,
i *tview.InputField,
) *tview.Flex {
rf := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(textV, 0, 1, false).

View file

@ -6,7 +6,9 @@ import (
)
// NewMessageInputField creates and returns a new message inputfield.
func NewMessageInputField(onMessageInputFieldInputCapture func(*tcell.EventKey) *tcell.EventKey) *tview.InputField {
func NewMessageInputField(
onMessageInputFieldInputCapture func(*tcell.EventKey) *tcell.EventKey,
) *tview.InputField {
i := tview.NewInputField()
i.
SetPlaceholder("Message...").

View file

@ -7,7 +7,9 @@ import (
)
// NewGuildsTreeView creates and returns a new guilds treeview.
func NewGuildsTreeView(onGuildsTreeViewSelected func(*tview.TreeNode)) *tview.TreeView {
func NewGuildsTreeView(
onGuildsTreeViewSelected func(*tview.TreeNode),
) *tview.TreeView {
v := tview.NewTreeView()
v.
SetTopLevel(1).
@ -29,8 +31,12 @@ func NewTextChannelTreeNode(c *discordgo.Channel) *tview.TreeNode {
return n
}
// GetTreeNodeByReference gets the TreeNode that has reference r from the given treeview.
func GetTreeNodeByReference(r interface{}, treeV *tview.TreeView) (mn *tview.TreeNode) {
// GetTreeNodeByReference gets the TreeNode that has reference r from the given
// treeview.
func GetTreeNodeByReference(
r interface{},
treeV *tview.TreeView,
) (mn *tview.TreeNode) {
treeV.GetRoot().Walk(func(n, _ *tview.TreeNode) bool {
if n.GetReference() == r {
mn = n
@ -43,11 +49,22 @@ func GetTreeNodeByReference(r interface{}, treeV *tview.TreeView) (mn *tview.Tre
return
}
// CreateTopLevelChannelsTreeNodes creates TreeNodes for the top-level (orphan) channels.
func CreateTopLevelChannelsTreeNodes(s *discordgo.State, n *tview.TreeNode, cs []*discordgo.Channel) {
// CreateTopLevelChannelsTreeNodes creates TreeNodes for the top-level (orphan)
// channels.
func CreateTopLevelChannelsTreeNodes(
s *discordgo.State,
n *tview.TreeNode,
cs []*discordgo.Channel,
) {
for _, c := range cs {
if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
if !util.HasPermission(s, s.User.ID, c.ID, discordgo.PermissionViewChannel) {
if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) &&
(c.ParentID == "") {
if !util.HasPermission(
s,
s.User.ID,
c.ID,
discordgo.PermissionViewChannel,
) {
continue
}
@ -58,8 +75,13 @@ func CreateTopLevelChannelsTreeNodes(s *discordgo.State, n *tview.TreeNode, cs [
}
}
// CreateCategoryChannelsTreeNodes creates TreeNodes for the category (parent) channels.
func CreateCategoryChannelsTreeNodes(s *discordgo.State, n *tview.TreeNode, cs []*discordgo.Channel) {
// CreateCategoryChannelsTreeNodes creates TreeNodes for the category (parent)
// channels.
func CreateCategoryChannelsTreeNodes(
s *discordgo.State,
n *tview.TreeNode,
cs []*discordgo.Channel,
) {
CategoryLoop:
for _, c := range cs {
if c.Type == discordgo.ChannelTypeGuildCategory {
@ -83,11 +105,22 @@ CategoryLoop:
}
}
// CreateSecondLevelChannelsTreeNodes creates TreeNodes for the second-level (category children) channels.
func CreateSecondLevelChannelsTreeNodes(s *discordgo.State, treeV *tview.TreeView, cs []*discordgo.Channel) {
// CreateSecondLevelChannelsTreeNodes creates TreeNodes for the second-level
// (category children) channels.
func CreateSecondLevelChannelsTreeNodes(
s *discordgo.State,
treeV *tview.TreeView,
cs []*discordgo.Channel,
) {
for _, c := range cs {
if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
if !util.HasPermission(s, s.User.ID, c.ID, discordgo.PermissionViewChannel) {
if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) &&
(c.ParentID != "") {
if !util.HasPermission(
s,
s.User.ID,
c.ID,
discordgo.PermissionViewChannel,
) {
continue
}

View file

@ -7,7 +7,8 @@ import (
"github.com/rivo/tview"
)
// Config consists of fields, such as theme, mouse, so on, that may be customized by the user.
// Config consists of fields, such as theme, mouse, so on, that may be
// customized by the user.
type Config struct {
Token string `json:"token,omitempty"`
Mouse bool `json:"mouse,omitempty"`

View file

@ -16,18 +16,24 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) {
switch m.Type {
case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
// Define a new region and assign message ID as the region ID.
// Learn more: https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
// Learn more:
// https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
b.WriteString("[\"")
b.WriteString(m.ID)
b.WriteString("\"]")
// Render the message associated with crosspost, channel follow add, pin, or a reply.
// Render the message associated with crosspost, channel follow add,
// pin, or a reply.
if rm := m.ReferencedMessage; rm != nil {
b.WriteString(" ╭ ")
b.WriteString("[::d]")
parseAuthor(&b, rm.Author, clientID)
if rm.Content != "" {
rm.Content = parseMessageMentions(rm.Content, rm.Mentions, clientID)
rm.Content = parseMessageMentions(
rm.Content,
rm.Mentions,
clientID,
)
b.WriteString(rm.Content)
}
@ -35,12 +41,15 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) {
}
// Render the author of the message.
parseAuthor(&b, m.Author, clientID)
// If the message content is not empty, parse the message mentions (users mentioned in the message) and render the message content.
// If the message content is not empty, parse the message mentions
// (users mentioned in the message) and render the message content.
if m.Content != "" {
m.Content = parseMessageMentions(m.Content, m.Mentions, clientID)
b.WriteString(m.Content)
}
// If the edited timestamp of the message is not empty; it implies that the message has been edited, hence render the message with edited label for distinction
// If the edited timestamp of the message is not empty; it implies that
// the message has been edited, hence render the message with edited
// label for distinction
if m.EditedTimestamp != "" {
b.WriteString(" [::d](edited)[::-]")
}
@ -67,7 +76,11 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) {
}
}
func parseMessageMentions(content string, mentions []*discordgo.User, clientID string) string {
func parseMessageMentions(
content string,
mentions []*discordgo.User,
clientID string,
) string {
for _, mUser := range mentions {
var color string
if mUser.ID == clientID {
@ -90,7 +103,8 @@ func parseMessageMentions(content string, mentions []*discordgo.User, clientID s
}
func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
// If the message author is the client, modify the text color for distinction.
// If the message author is the client, modify the text color for
// distinction.
if u.ID == clientID {
b.WriteString("[#57F287]")
} else {
@ -99,7 +113,8 @@ func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
b.WriteString(u.Username)
b.WriteString("[-] ")
// If the message author is a bot account, render the message with bot label for distinction.
// If the message author is a bot account, render the message with bot label
// for distinction.
if u.Bot {
b.WriteString("[#EB459E]BOT[-] ")
}
@ -112,13 +127,22 @@ type loginResponse struct {
Token string `json:"token"`
}
// Login creates a new request to the `/login` endpoint for essential login information.
func Login(s *discordgo.Session, email, password string) (*loginResponse, error) {
// Login creates a new request to the `/login` endpoint for essential login
// information.
func Login(
s *discordgo.Session,
email, password string,
) (*loginResponse, error) {
data := struct {
Email string `json:"email"`
Password string `json:"password"`
}{email, password}
resp, err := s.RequestWithBucketID("POST", discordgo.EndpointLogin, data, discordgo.EndpointLogin)
resp, err := s.RequestWithBucketID(
"POST",
discordgo.EndpointLogin,
data,
discordgo.EndpointLogin,
)
if err != nil {
return nil, err
}
@ -154,8 +178,14 @@ func TOTP(s *discordgo.Session, code, ticket string) (*loginResponse, error) {
return &lr, nil
}
// HasPermission returns a boolean representing whether the provided user has given permissions or not.
func HasPermission(s *discordgo.State, uID string, cID string, perm int64) bool {
// HasPermission returns a boolean representing whether the provided user has
// given permissions or not.
func HasPermission(
s *discordgo.State,
uID string,
cID string,
perm int64,
) bool {
p, err := s.UserChannelPermissions(uID, cID)
if err != nil {
return false