From 9eede1a54708db92503df50a5f9b577bd1c69d88 Mon Sep 17 00:00:00 2001 From: ayntgl Date: Fri, 29 Oct 2021 21:20:37 +0400 Subject: [PATCH] refactor: move config.go to util pkg --- main.go | 5 +-- config.go => util/config.go | 62 ++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 33 deletions(-) rename config.go => util/config.go (72%) diff --git a/main.go b/main.go index c959f44..a982eed 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/ayntgl/discordgo" + "github.com/ayntgl/discordo/util" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "github.com/zalando/go-keyring" @@ -17,12 +18,12 @@ var ( messageInputField *tview.InputField mainFlex *tview.Flex - conf *config + conf *util.Config session *discordgo.Session ) func main() { - conf = loadConfig() + conf = util.LoadConfig() tview.Borders = conf.Borders diff --git a/config.go b/util/config.go similarity index 72% rename from config.go rename to util/config.go index e7828ba..23b054f 100644 --- a/config.go +++ b/util/config.go @@ -1,4 +1,4 @@ -package main +package util import ( "os" @@ -58,7 +58,7 @@ type borders struct { BottomRightFocus rune } -type config struct { +type Config struct { Token string `toml:"token"` UserAgent string `toml:"user_agent"` Mouse bool `toml:"mouse"` @@ -70,15 +70,14 @@ type config struct { Borders borders `toml:"borders"` } -func loadConfig() *config { +func LoadConfig() *Config { configPath, err := os.UserConfigDir() if err != nil { panic(err) } configPath += "/discordo.toml" - - var c config + var c Config if _, err = os.Stat(configPath); os.IsNotExist(err) { f, err := os.Create(configPath) if err != nil { @@ -86,34 +85,35 @@ func loadConfig() *config { } defer f.Close() - c.Mouse = true - c.Notifications = true - c.UserAgent = userAgent - c.GetMessagesLimit = 50 - c.Theme = theme{ - Background: "black", + c = Config{ + Mouse: true, + Notifications: true, + UserAgent: userAgent, + GetMessagesLimit: 50, + Borders: tview.Borders, + Theme: theme{ + Background: "black", - Border: "white", - Title: "white", - Graphics: "white", - Text: "white", + Border: "white", + Title: "white", + Graphics: "white", + Text: "white", + }, + Keybindings: keybindings{ + FocusChannelsTree: "Alt+Left", + FocusMessagesView: "Alt+Right", + FocusMessageInputField: "Alt+Down", + + SelectPreviousMessage: "Up", + SelectNextMessage: "Down", + SelectFirstMessage: "Home", + SelectLastMessage: "End", + ReplySelectedMessage: "Rune[r]", + MentionReplySelectedMessage: "Rune[R]", + CopySelectedMessage: "Rune[c]", + SelectMessageReference: "Rune[m]", + }, } - c.Keybindings = keybindings{ - FocusChannelsTree: "Alt+Left", - FocusMessagesView: "Alt+Right", - FocusMessageInputField: "Alt+Down", - - SelectPreviousMessage: "Up", - SelectNextMessage: "Down", - SelectFirstMessage: "Home", - SelectLastMessage: "End", - ReplySelectedMessage: "Rune[r]", - MentionReplySelectedMessage: "Rune[R]", - CopySelectedMessage: "Rune[c]", - SelectMessageReference: "Rune[m]", - } - c.Borders = tview.Borders - err = toml.NewEncoder(f).Encode(c) if err != nil { panic(err)