From bddfb173b7d072094ec7854e84dac322d735b43e Mon Sep 17 00:00:00 2001 From: ayntgl Date: Thu, 17 Feb 2022 12:00:46 +0400 Subject: [PATCH] refactor: divide util package --- util/config.go => config/mod.go | 78 +++++++++++++++--------------- util/discord.go => discord/mod.go | 2 +- main.go | 6 +-- ui/app.go | 6 +-- ui/builder.go | 9 ++-- ui/handlers.go | 18 +++---- util/ui.go => ui/util.go | 8 +-- util/ui_test.go => ui/util_test.go | 6 +-- 8 files changed, 66 insertions(+), 67 deletions(-) rename util/config.go => config/mod.go (97%) rename util/discord.go => discord/mod.go (98%) rename util/ui.go => ui/util.go (86%) rename util/ui_test.go => ui/util_test.go (92%) diff --git a/util/config.go b/config/mod.go similarity index 97% rename from util/config.go rename to config/mod.go index 799ce83..8d0f860 100644 --- a/util/config.go +++ b/config/mod.go @@ -1,4 +1,4 @@ -package util +package config import ( "os" @@ -35,44 +35,7 @@ type Config struct { General GeneralConfig `toml:"general"` } -func LoadConfig() *Config { - configPath, err := os.UserConfigDir() - if err != nil { - panic(err) - } - - configPath += "/discordo/config.toml" - // Create a directory as well as create all of the nested directories, recursively. - err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm) - if err != nil { - panic(err) - } - - c := &Config{} - // If the configuration file does not exist, create and write the default configuration to the file. - if _, err = os.Stat(configPath); os.IsNotExist(err) { - f, err := os.Create(configPath) - if err != nil { - panic(err) - } - defer f.Close() - - c = newDefaultConfig() - err = toml.NewEncoder(f).Encode(c) - if err != nil { - panic(err) - } - } else { - _, err = toml.DecodeFile(configPath, c) - if err != nil { - panic(err) - } - } - - return c -} - -func newDefaultConfig() *Config { +func NewConfig() *Config { return &Config{ General: GeneralConfig{ UserAgent: userAgent, @@ -95,3 +58,40 @@ func newDefaultConfig() *Config { }, } } + +func LoadConfig() *Config { + configPath, err := os.UserConfigDir() + if err != nil { + panic(err) + } + + configPath += "/discordo/config.toml" + // Create a directory as well as create all of the nested directories, recursively. + err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm) + if err != nil { + panic(err) + } + + c := &Config{} + // If the configuration file does not exist, create and write the default configuration to the file. + if _, err = os.Stat(configPath); os.IsNotExist(err) { + f, err := os.Create(configPath) + if err != nil { + panic(err) + } + defer f.Close() + + c = NewConfig() + err = toml.NewEncoder(f).Encode(c) + if err != nil { + panic(err) + } + } else { + _, err = toml.DecodeFile(configPath, c) + if err != nil { + panic(err) + } + } + + return c +} diff --git a/util/discord.go b/discord/mod.go similarity index 98% rename from util/discord.go rename to discord/mod.go index 3f160af..ecd4a9b 100644 --- a/util/discord.go +++ b/discord/mod.go @@ -1,4 +1,4 @@ -package util +package discord import ( "encoding/json" diff --git a/main.go b/main.go index 840ee98..5a361d3 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( "os" + "github.com/ayntgl/discordo/discord" "github.com/ayntgl/discordo/ui" - "github.com/ayntgl/discordo/util" "github.com/rivo/tview" "github.com/zalando/go-keyring" ) @@ -39,7 +39,7 @@ func main() { } // Login using the email and password - lr, err := util.Login(app.Session, email, password) + lr, err := discord.Login(app.Session, email, password) if err != nil { panic(err) } @@ -64,7 +64,7 @@ func main() { return } - lr, err = util.TOTP(app.Session, mfaCode, lr.Ticket) + lr, err = discord.TOTP(app.Session, mfaCode, lr.Ticket) if err != nil { panic(err) } diff --git a/ui/app.go b/ui/app.go index 54399c9..d88660f 100644 --- a/ui/app.go +++ b/ui/app.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/ayntgl/discordgo" - "github.com/ayntgl/discordo/util" + "github.com/ayntgl/discordo/config" "github.com/rivo/tview" ) @@ -18,7 +18,7 @@ type App struct { MessageInputField *tview.InputField Session *discordgo.Session SelectedChannel *discordgo.Channel - Config *util.Config + Config *config.Config SelectedMessage int } @@ -34,7 +34,7 @@ func NewApp() *App { MessageInputField: tview.NewInputField(), Session: s, - Config: util.LoadConfig(), + Config: config.LoadConfig(), SelectedMessage: -1, } } diff --git a/ui/builder.go b/ui/builder.go index 231e500..66ea4dc 100644 --- a/ui/builder.go +++ b/ui/builder.go @@ -6,7 +6,6 @@ import ( "time" "github.com/ayntgl/discordgo" - "github.com/ayntgl/discordo/util" ) func buildMessage(app *App, m *discordgo.Message) []byte { @@ -89,7 +88,7 @@ func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID if rm.Content != "" { rm.Content = buildMentions(rm.Content, rm.Mentions, clientID) - b.WriteString(util.ParseMarkdown(rm.Content)) + b.WriteString(parseMarkdown(rm.Content)) } b.WriteString("[::-]") @@ -100,7 +99,7 @@ func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID func buildContent(b *strings.Builder, m *discordgo.Message, clientID string) { if m.Content != "" { m.Content = buildMentions(m.Content, m.Mentions, clientID) - b.WriteString(util.ParseMarkdown(m.Content)) + b.WriteString(parseMarkdown(m.Content)) } } @@ -138,7 +137,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) { embedBuilder.WriteString("\n\n") } - embedBuilder.WriteString(util.ParseMarkdown(e.Description)) + embedBuilder.WriteString(parseMarkdown(e.Description)) } if len(e.Fields) != 0 { @@ -151,7 +150,7 @@ func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) { embedBuilder.WriteString(ef.Name) embedBuilder.WriteString("[::-]") embedBuilder.WriteByte('\n') - embedBuilder.WriteString(util.ParseMarkdown(ef.Value)) + embedBuilder.WriteString(parseMarkdown(ef.Value)) if i != len(e.Fields)-1 { embedBuilder.WriteString("\n\n") diff --git a/ui/handlers.go b/ui/handlers.go index 03263bd..1f37d8e 100644 --- a/ui/handlers.go +++ b/ui/handlers.go @@ -9,7 +9,7 @@ import ( "github.com/atotto/clipboard" "github.com/ayntgl/discordgo" - "github.com/ayntgl/discordo/util" + "github.com/ayntgl/discordo/discord" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -59,7 +59,7 @@ func onGuildsListSelected(app *App, guildIdx int) { }) for _, c := range cs { - channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)). + channelTreeNode := tview.NewTreeNode(channelToString(c)). SetReference(c.ID) rootTreeNode.AddChild(channelTreeNode) } @@ -71,7 +71,7 @@ func onGuildsListSelected(app *App, guildIdx int) { for _, c := range cs { if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") { - channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)). + channelTreeNode := tview.NewTreeNode(channelToString(c)). SetReference(c.ID) rootTreeNode.AddChild(channelTreeNode) } @@ -108,7 +108,7 @@ func onGuildsListSelected(app *App, guildIdx int) { }) if parentTreeNode != nil { - channelTreeNode := tview.NewTreeNode(util.ChannelToString(c)). + channelTreeNode := tview.NewTreeNode(channelToString(c)). SetReference(c.ID) parentTreeNode.AddChild(channelTreeNode) } @@ -139,7 +139,7 @@ func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) { app.SelectedChannel = c - app.MessagesTextView.SetTitle(util.ChannelToString(c)) + app.MessagesTextView.SetTitle(channelToString(c)) app.SetFocus(app.MessageInputField) go func() { @@ -223,12 +223,12 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey return nil } - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0]) if m == nil { return nil } - if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) { + if discord.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) { messageActionsList. AddItem("Reply", "", 'r', nil). AddItem("Mention Reply", "", 'R', nil) @@ -286,7 +286,7 @@ func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Messag SetRoot(app.MainFlex, false). SetFocus(app.MessageInputField) case "Select Reply": - app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) + app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) app.MessagesTextView. Highlight(m.ReferencedMessage.ID). ScrollToHighlight() @@ -309,7 +309,7 @@ func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKe } if len(app.MessagesTextView.GetHighlights()) != 0 { - _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0]) d := &discordgo.MessageSend{ Content: t, Reference: m.Reference(), diff --git a/util/ui.go b/ui/util.go similarity index 86% rename from util/ui.go rename to ui/util.go index f3b801c..a713f0e 100644 --- a/util/ui.go +++ b/ui/util.go @@ -1,4 +1,4 @@ -package util +package ui import ( "regexp" @@ -14,7 +14,7 @@ var ( strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`) ) -func ParseMarkdown(md string) string { +func parseMarkdown(md string) string { var res string res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]") res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]") @@ -24,7 +24,7 @@ func ParseMarkdown(md string) string { return res } -func ChannelToString(c *discordgo.Channel) string { +func channelToString(c *discordgo.Channel) string { var repr string if c.Name != "" { repr = "#" + c.Name @@ -43,7 +43,7 @@ func ChannelToString(c *discordgo.Channel) string { return repr } -func HasKeybinding(ks []string, k string) bool { +func hasKeybinding(ks []string, k string) bool { for _, repr := range ks { if repr == k { return true diff --git a/util/ui_test.go b/ui/util_test.go similarity index 92% rename from util/ui_test.go rename to ui/util_test.go index 6d4460b..27cb361 100644 --- a/util/ui_test.go +++ b/ui/util_test.go @@ -1,4 +1,4 @@ -package util +package ui import ( "testing" @@ -18,7 +18,7 @@ func TestParseMarkdown(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - if got := ParseMarkdown(test.in); got != test.want { + if got := parseMarkdown(test.in); got != test.want { t.Errorf("got: %s\nwant: %s", got, test.want) } }) @@ -31,6 +31,6 @@ func BenchmarkParseMarkdown(b *testing.B) { Sit commodi sed iure et sed quae eveniet. *Sit non distinctio nihil sunt. Nesciunt cumque aspernatur *nulla* porro et earum quidem.* Sed omnis at commodi vel quasi. Fuga et **consequatur** molestias dicta vel provident et aspernatur. Dolorem molestias ipsa aut ~~facilis quae dolorem~~ eveniet dicta.` for i := 0; i < b.N; i++ { - ParseMarkdown(in) + parseMarkdown(in) } }