diff --git a/config.go b/config/config.go similarity index 96% rename from config.go rename to config/config.go index 57bb153..5194783 100644 --- a/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package main +package config import ( "os" @@ -7,7 +7,7 @@ import ( "gopkg.in/yaml.v3" ) -const name = "discordo" +const Name = "discordo" type ( MessagesTextKeysConfig struct { @@ -73,13 +73,13 @@ type Config struct { Theme ThemeConfig `yaml:"theme"` } -func newConfig() (*Config, error) { +func Load() (*Config, error) { path, err := os.UserConfigDir() if err != nil { return nil, err } - path = filepath.Join(path, name) + path = filepath.Join(path, Name) err = os.MkdirAll(path, os.ModePerm) if err != nil { return nil, err diff --git a/guilds_tree.go b/guilds_tree.go index 8b926e9..f587261 100644 --- a/guilds_tree.go +++ b/guilds_tree.go @@ -24,20 +24,20 @@ func newGuildsTree() *GuildsTree { root: tview.NewTreeNode(""), } - gt.SetGraphics(config.Theme.GuildsTree.Graphics) + gt.SetGraphics(cfg.Theme.GuildsTree.Graphics) gt.SetRoot(gt.root) gt.SetTopLevel(1) gt.SetSelectedFunc(gt.onSelected) - gt.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor)) + gt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) gt.SetTitle("Guilds") - gt.SetTitleColor(tcell.GetColor(config.Theme.TitleColor)) + gt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) gt.SetTitleAlign(tview.AlignLeft) - p := config.Theme.BorderPadding - gt.SetBorder(config.Theme.Border) - gt.SetBorderColor(tcell.GetColor(config.Theme.BorderColor)) + p := cfg.Theme.BorderPadding + gt.SetBorder(cfg.Theme.Border) + gt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) gt.SetBorderPadding(p[0], p[1], p[2], p[3]) return gt @@ -150,7 +150,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) { gt.selectedChannel = c messagesText.SetTitle(gt.channelToString(*c)) - ms, err := discordState.Messages(ref, config.MessagesLimit) + ms, err := discordState.Messages(ref, cfg.MessagesLimit) if err != nil { log.Println(err) return diff --git a/login_form.go b/login_form.go index 847dadc..fae0f0d 100644 --- a/login_form.go +++ b/login_form.go @@ -24,11 +24,11 @@ func newLoginForm() *LoginForm { lf.AddButton("Login", lf.onLoginButtonSelected) lf.SetTitle("Login") - lf.SetTitleColor(tcell.GetColor(config.Theme.TitleColor)) + lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) - p := config.Theme.BorderPadding - lf.SetBorder(config.Theme.Border) - lf.SetBorderColor(tcell.GetColor(config.Theme.BorderColor)) + p := cfg.Theme.BorderPadding + lf.SetBorder(cfg.Theme.Border) + lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) lf.SetBorderPadding(p[0], p[1], p[2], p[3]) return lf diff --git a/main.go b/main.go index a386117..2062064 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" + "github.com/ayn2op/discordo/config" "github.com/rivo/tview" "github.com/zalando/go-keyring" ) @@ -14,7 +15,7 @@ import ( var ( token string - config *Config + cfg *config.Config discordState *State app = tview.NewApplication() @@ -32,7 +33,7 @@ func init() { log.Fatal(err) } - path = filepath.Join(path, name) + path = filepath.Join(path, config.Name) err = os.MkdirAll(path, os.ModePerm) if err != nil { log.Fatal(err) @@ -54,16 +55,16 @@ func main() { var err error // If the token is passed via the flag, set it in the keyring. if token != "" { - go keyring.Set(name, "token", token) + go keyring.Set(config.Name, "token", token) } else { - token, err = keyring.Get(name, "token") + token, err = keyring.Get(config.Name, "token") if err != nil { log.Println(err) return } } - config, err = newConfig() + cfg, err = config.Load() if err != nil { log.Fatal(err) } @@ -94,7 +95,7 @@ func main() { app.SetRoot(flex, true) } - app.EnableMouse(config.Mouse) + app.EnableMouse(cfg.Mouse) err = app.Run() if err != nil { log.Fatal(err) diff --git a/message_input.go b/message_input.go index 319d415..d218977 100644 --- a/message_input.go +++ b/message_input.go @@ -24,15 +24,15 @@ func newMessageInput() *MessageInput { } mi.SetInputCapture(mi.onInputCapture) - mi.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor)) - mi.SetFieldBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor)) + mi.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) + mi.SetFieldBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) - mi.SetTitleColor(tcell.GetColor(config.Theme.TitleColor)) + mi.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) mi.SetTitleAlign(tview.AlignLeft) - p := config.Theme.BorderPadding - mi.SetBorder(config.Theme.Border) - mi.SetBorderColor(tcell.GetColor(config.Theme.BorderColor)) + p := cfg.Theme.BorderPadding + mi.SetBorder(cfg.Theme.Border) + mi.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) mi.SetBorderPadding(p[0], p[1], p[2], p[3]) return mi @@ -45,16 +45,16 @@ func (mi *MessageInput) reset() { func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey { switch event.Name() { - case config.Keys.MessageInput.Send: + case cfg.Keys.MessageInput.Send: mi.sendAction() return nil - case config.Keys.MessageInput.Paste: + case cfg.Keys.MessageInput.Paste: mi.pasteAction() return nil - case config.Keys.MessageInput.LaunchEditor: + case cfg.Keys.MessageInput.LaunchEditor: messageInput.launchEditorAction() return nil - case config.Keys.Cancel: + case cfg.Keys.Cancel: mi.reset() return nil } @@ -116,7 +116,7 @@ func (mi *MessageInput) pasteAction() { } func (mi *MessageInput) launchEditorAction() { - e := config.Editor + e := cfg.Editor if e == "default" { e = os.Getenv("EDITOR") } diff --git a/messages_text.go b/messages_text.go index 37a2863..345f342 100644 --- a/messages_text.go +++ b/messages_text.go @@ -37,15 +37,15 @@ func newMessagesText() *MessagesText { app.Draw() }) - mt.SetBackgroundColor(tcell.GetColor(config.Theme.BackgroundColor)) + mt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) mt.SetTitle("Messages") - mt.SetTitleColor(tcell.GetColor(config.Theme.TitleColor)) + mt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) mt.SetTitleAlign(tview.AlignLeft) - p := config.Theme.BorderPadding - mt.SetBorder(config.Theme.Border) - mt.SetBorderColor(tcell.GetColor(config.Theme.BorderColor)) + p := cfg.Theme.BorderPadding + mt.SetBorder(cfg.Theme.Border) + mt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) mt.SetBorderPadding(p[0], p[1], p[2], p[3]) return mt @@ -74,7 +74,7 @@ func (mt *MessagesText) createMessage(m *discord.Message) error { mt.buf.WriteByte(' ') mt.buf.WriteByte('[') - mt.buf.WriteString(config.Theme.MessagesText.AuthorColor) + mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor) mt.buf.WriteByte(']') mt.buf.WriteString(m.ReferencedMessage.Author.Username) mt.buf.WriteString("[-] ") @@ -98,12 +98,12 @@ func (mt *MessagesText) createMessage(m *discord.Message) error { func (mt *MessagesText) createHeader(m *discord.Message) { mt.buf.WriteByte('[') - mt.buf.WriteString(config.Theme.MessagesText.AuthorColor) + mt.buf.WriteString(cfg.Theme.MessagesText.AuthorColor) mt.buf.WriteByte(']') mt.buf.WriteString(m.Author.Username) mt.buf.WriteString("[-] ") - if config.Timestamps { + if cfg.Timestamps { mt.buf.WriteString("[::d]") mt.buf.WriteString(m.Timestamp.Format(time.Kitchen)) mt.buf.WriteString("[::-] ") @@ -127,31 +127,31 @@ func (mt *MessagesText) createFooter(m *discord.Message) { func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey { switch event.Name() { - case config.Keys.MessagesText.CopyContent: + case cfg.Keys.MessagesText.CopyContent: mt.copyContentAction() return nil - case config.Keys.MessagesText.Reply: + case cfg.Keys.MessagesText.Reply: mt.replyAction(false) return nil - case config.Keys.MessagesText.ReplyMention: + case cfg.Keys.MessagesText.ReplyMention: mt.replyAction(true) return nil - case config.Keys.MessagesText.SelectPrevious: + case cfg.Keys.MessagesText.SelectPrevious: mt.selectPreviousAction() return nil - case config.Keys.MessagesText.SelectNext: + case cfg.Keys.MessagesText.SelectNext: mt.selectNextAction() return nil - case config.Keys.MessagesText.SelectFirst: + case cfg.Keys.MessagesText.SelectFirst: mt.selectFirstAction() return nil - case config.Keys.MessagesText.SelectLast: + case cfg.Keys.MessagesText.SelectLast: mt.selectLastAction() return nil - case config.Keys.MessagesText.SelectReply: + case cfg.Keys.MessagesText.SelectReply: mt.selectReplyAction() return nil - case config.Keys.Cancel: + case cfg.Keys.Cancel: guildsTree.selectedChannel = nil messagesText.reset() diff --git a/state.go b/state.go index 509a39c..adbec3a 100644 --- a/state.go +++ b/state.go @@ -5,6 +5,7 @@ import ( "log" "runtime" + "github.com/ayn2op/discordo/config" "github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/gateway" "github.com/diamondburned/arikawa/v3/state" @@ -12,10 +13,10 @@ import ( ) func init() { - api.UserAgent = fmt.Sprintf("%s/%s %s/%s", name, "0.1", "arikawa", "v3") + api.UserAgent = fmt.Sprintf("%s/%s %s/%s", config.Name, "0.1", "arikawa", "v3") gateway.DefaultIdentity = gateway.IdentifyProperties{ OS: runtime.GOOS, - Browser: name, + Browser: config.Name, Device: "", } }