diff --git a/attachment_image.go b/attachment_image.go index 84f257f..c20eb1b 100644 --- a/attachment_image.go +++ b/attachment_image.go @@ -6,6 +6,7 @@ import ( _ "image/png" "net/http" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/discord" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -21,13 +22,13 @@ func newAttachmentImage(a discord.Attachment) (*AttachmentImage, error) { } ai.SetInputCapture(ai.onInputCapture) - ai.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) - ai.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) + ai.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor)) + ai.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor)) ai.SetTitleAlign(tview.AlignLeft) - p := cfg.Theme.BorderPadding - ai.SetBorder(cfg.Theme.Border) - ai.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) + p := config.Current.Theme.BorderPadding + ai.SetBorder(config.Current.Theme.Border) + ai.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor)) ai.SetBorderPadding(p[0], p[1], p[2], p[3]) resp, err := http.Get(a.URL) @@ -47,7 +48,7 @@ func newAttachmentImage(a discord.Attachment) (*AttachmentImage, error) { } func (ai *AttachmentImage) onInputCapture(event *tcell.EventKey) *tcell.EventKey { - if event.Name() == cfg.Keys.Cancel { + if event.Name() == config.Current.Keys.Cancel { app.SetRoot(mainFlex, true) app.SetFocus(messagesText) return nil diff --git a/guilds_tree.go b/guilds_tree.go index cc4b4c5..a2cb190 100644 --- a/guilds_tree.go +++ b/guilds_tree.go @@ -6,6 +6,7 @@ import ( "sort" "strings" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/gateway" "github.com/gdamore/tcell/v2" @@ -26,20 +27,20 @@ func newGuildsTree() *GuildsTree { root: tview.NewTreeNode(""), } - gt.SetGraphics(cfg.Theme.GuildsTree.Graphics) + gt.SetGraphics(config.Current.Theme.GuildsTree.Graphics) gt.SetRoot(gt.root) gt.SetTopLevel(1) gt.SetSelectedFunc(gt.onSelected) - gt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) + gt.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor)) gt.SetTitle("Guilds") - gt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) + gt.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor)) gt.SetTitleAlign(tview.AlignLeft) - p := cfg.Theme.BorderPadding - gt.SetBorder(cfg.Theme.Border) - gt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) + p := config.Current.Theme.BorderPadding + gt.SetBorder(config.Current.Theme.Border) + gt.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor)) gt.SetBorderPadding(p[0], p[1], p[2], p[3]) return gt @@ -162,7 +163,7 @@ func (gt *GuildsTree) onSelected(n *tview.TreeNode) { gt.createChannelNodes(n, cs) case discord.ChannelID: - ms, err := discordState.Messages(ref, cfg.MessagesLimit) + ms, err := discordState.Messages(ref, config.Current.MessagesLimit) if err != nil { log.Println(err) return diff --git a/config/config.go b/internal/config/config.go similarity index 72% rename from config/config.go rename to internal/config/config.go index 2a8f1fb..0f6b1bd 100644 --- a/config/config.go +++ b/internal/config/config.go @@ -9,6 +9,8 @@ import ( const Name = "discordo" +var Current = defConfig() + type Config struct { // Mouse indicates whether the mouse is usable or not. Mouse bool `yaml:"mouse"` @@ -23,53 +25,56 @@ type Config struct { Theme Theme `yaml:"theme"` } -// Load reads the configuration file and decodes the configuration file or creates a new one if it does not exist already and writes the default configuration to the newly-created configuration file. -func Load() (*Config, error) { - path, err := os.UserConfigDir() - if err != nil { - return nil, err - } - - path = filepath.Join(path, Name) - err = os.MkdirAll(path, os.ModePerm) - if err != nil { - return nil, err - } - - c := Config{ +func defConfig() Config { + return Config{ Mouse: true, Timestamps: false, MessagesLimit: 50, Editor: "default", - Keys: newKeys(), - Theme: newTheme(), + Keys: defKeys(), + Theme: defTheme(), } +} + +func Load() error { + path, err := os.UserConfigDir() + if err != nil { + return err + } + + // Create the configuration directory if it does not exist already. + path = filepath.Join(path, Name) + err = os.MkdirAll(path, os.ModePerm) + if err != nil { + return err + } + path = filepath.Join(path, "config.yml") _, err = os.Stat(path) if os.IsNotExist(err) { f, err := os.Create(path) if err != nil { - return nil, err + return err } defer f.Close() - err = yaml.NewEncoder(f).Encode(c) + err = yaml.NewEncoder(f).Encode(Current) if err != nil { - return nil, err + return err } } else { f, err := os.Open(path) if err != nil { - return nil, err + return err } defer f.Close() - err = yaml.NewDecoder(f).Decode(&c) + err = yaml.NewDecoder(f).Decode(&Current) if err != nil { - return nil, err + return err } } - return &c, nil + return err } diff --git a/config/keys.go b/internal/config/keys.go similarity index 98% rename from config/keys.go rename to internal/config/keys.go index 81b098b..e6106ab 100644 --- a/config/keys.go +++ b/internal/config/keys.go @@ -39,7 +39,7 @@ type Keys struct { MessageInput MessageInputKeys `yaml:"message_input"` } -func newKeys() Keys { +func defKeys() Keys { return Keys{ Cancel: "Esc", diff --git a/config/theme.go b/internal/config/theme.go similarity index 97% rename from config/theme.go rename to internal/config/theme.go index 1343a71..a4a7671 100644 --- a/config/theme.go +++ b/internal/config/theme.go @@ -25,7 +25,7 @@ type Theme struct { MessageInput MessageInputTheme `yaml:"message_input"` } -func newTheme() Theme { +func defTheme() Theme { return Theme{ Border: true, BorderColor: "default", diff --git a/login_form.go b/login_form.go index 6522029..26b9fca 100644 --- a/login_form.go +++ b/login_form.go @@ -4,6 +4,7 @@ import ( "context" "log" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/api" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -24,11 +25,11 @@ func newLoginForm() *LoginForm { lf.AddButton("Login", lf.onLoginButtonSelected) lf.SetTitle("Login") - lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) + lf.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor)) - p := cfg.Theme.BorderPadding - lf.SetBorder(cfg.Theme.Border) - lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) + p := config.Current.Theme.BorderPadding + lf.SetBorder(config.Current.Theme.Border) + lf.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor)) lf.SetBorderPadding(p[0], p[1], p[2], p[3]) return lf diff --git a/main.go b/main.go index 2b423f8..3b5cea6 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "os" "path/filepath" - "github.com/ayn2op/discordo/config" + "github.com/ayn2op/discordo/internal/config" "github.com/rivo/tview" "github.com/zalando/go-keyring" ) @@ -14,7 +14,6 @@ import ( var ( token string - cfg *config.Config discordState *State app = tview.NewApplication() @@ -62,8 +61,7 @@ func main() { } } - cfg, err = config.Load() - if err != nil { + if err = config.Load(); err != nil { log.Fatal(err) } @@ -94,7 +92,7 @@ func main() { app.SetRoot(mainFlex, true) } - app.EnableMouse(cfg.Mouse) + app.EnableMouse(config.Current.Mouse) err = app.Run() if err != nil { log.Fatal(err) diff --git a/main_flex.go b/main_flex.go index a9eee21..e2c0f7b 100644 --- a/main_flex.go +++ b/main_flex.go @@ -1,18 +1,19 @@ package main import ( + "github.com/ayn2op/discordo/internal/config" "github.com/gdamore/tcell/v2" ) func onInputCapture(event *tcell.EventKey) *tcell.EventKey { switch event.Name() { - case cfg.Keys.GuildsTree.Focus: + case config.Current.Keys.GuildsTree.Focus: app.SetFocus(guildsTree) return nil - case cfg.Keys.MessagesText.Focus: + case config.Current.Keys.MessagesText.Focus: app.SetFocus(messagesText) return nil - case cfg.Keys.MessageInput.Focus: + case config.Current.Keys.MessageInput.Focus: app.SetFocus(messageInput) return nil } diff --git a/message_input.go b/message_input.go index b821d38..e0914de 100644 --- a/message_input.go +++ b/message_input.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/atotto/clipboard" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/utils/json/option" @@ -24,15 +25,15 @@ func newMessageInput() *MessageInput { } mi.SetInputCapture(mi.onInputCapture) - mi.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) - mi.SetFieldBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) + mi.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor)) + mi.SetFieldBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor)) - mi.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) + mi.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor)) mi.SetTitleAlign(tview.AlignLeft) - p := cfg.Theme.BorderPadding - mi.SetBorder(cfg.Theme.Border) - mi.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) + p := config.Current.Theme.BorderPadding + mi.SetBorder(config.Current.Theme.Border) + mi.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor)) mi.SetBorderPadding(p[0], p[1], p[2], p[3]) return mi @@ -45,16 +46,16 @@ func (mi *MessageInput) reset() { func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey { switch event.Name() { - case cfg.Keys.MessageInput.Send: + case config.Current.Keys.MessageInput.Send: mi.sendAction() return nil - case cfg.Keys.MessageInput.Paste: + case config.Current.Keys.MessageInput.Paste: mi.pasteAction() return nil - case cfg.Keys.MessageInput.LaunchEditor: + case config.Current.Keys.MessageInput.LaunchEditor: messageInput.launchEditorAction() return nil - case cfg.Keys.Cancel: + case config.Current.Keys.Cancel: mi.reset() return nil } @@ -116,7 +117,7 @@ func (mi *MessageInput) pasteAction() { } func (mi *MessageInput) launchEditorAction() { - e := cfg.Editor + e := config.Current.Editor if e == "default" { e = os.Getenv("EDITOR") } diff --git a/messages_text.go b/messages_text.go index 8e0465a..f1aabb6 100644 --- a/messages_text.go +++ b/messages_text.go @@ -8,6 +8,7 @@ import ( "github.com/atotto/clipboard" "github.com/ayn2op/discordo/discordmd" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/discord" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -37,15 +38,15 @@ func newMessagesText() *MessagesText { app.Draw() }) - mt.SetBackgroundColor(tcell.GetColor(cfg.Theme.BackgroundColor)) + mt.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor)) mt.SetTitle("Messages") - mt.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) + mt.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor)) mt.SetTitleAlign(tview.AlignLeft) - p := cfg.Theme.BorderPadding - mt.SetBorder(cfg.Theme.Border) - mt.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) + p := config.Current.Theme.BorderPadding + mt.SetBorder(config.Current.Theme.Border) + mt.SetBorderColor(tcell.GetColor(config.Current.Theme.BorderColor)) mt.SetBorderPadding(p[0], p[1], p[2], p[3]) return mt @@ -86,9 +87,9 @@ func (mt *MessagesText) createMessage(m discord.Message) { } func (mt *MessagesText) createHeader(w io.Writer, m discord.Message) { - fmt.Fprintf(w, "[%s]%s[-] ", cfg.Theme.MessagesText.AuthorColor, m.Author.Username) + fmt.Fprintf(w, "[%s]%s[-] ", config.Current.Theme.MessagesText.AuthorColor, m.Author.Username) - if cfg.Timestamps { + if config.Current.Timestamps { fmt.Fprintf(w, "[::d]%s[::-] ", m.Timestamp.Format(time.Kitchen)) } } @@ -106,34 +107,34 @@ func (mt *MessagesText) createFooter(w io.Writer, m discord.Message) { func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey { switch event.Name() { - case cfg.Keys.MessagesText.CopyContent: + case config.Current.Keys.MessagesText.CopyContent: mt.copyContentAction() return nil - case cfg.Keys.MessagesText.Reply: + case config.Current.Keys.MessagesText.Reply: mt.replyAction(false) return nil - case cfg.Keys.MessagesText.ReplyMention: + case config.Current.Keys.MessagesText.ReplyMention: mt.replyAction(true) return nil - case cfg.Keys.MessagesText.SelectPrevious: + case config.Current.Keys.MessagesText.SelectPrevious: mt.selectPreviousAction() return nil - case cfg.Keys.MessagesText.SelectNext: + case config.Current.Keys.MessagesText.SelectNext: mt.selectNextAction() return nil - case cfg.Keys.MessagesText.SelectFirst: + case config.Current.Keys.MessagesText.SelectFirst: mt.selectFirstAction() return nil - case cfg.Keys.MessagesText.SelectLast: + case config.Current.Keys.MessagesText.SelectLast: mt.selectLastAction() return nil - case cfg.Keys.MessagesText.SelectReply: + case config.Current.Keys.MessagesText.SelectReply: mt.selectReplyAction() return nil - case cfg.Keys.MessagesText.ShowImage: + case config.Current.Keys.MessagesText.ShowImage: mt.showImageAction() return nil - case cfg.Keys.Cancel: + case config.Current.Keys.Cancel: guildsTree.selectedChannelID = 0 messagesText.reset() diff --git a/state.go b/state.go index d6bed72..66b6c7d 100644 --- a/state.go +++ b/state.go @@ -6,7 +6,7 @@ import ( "log" "runtime" - "github.com/ayn2op/discordo/config" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/gateway" "github.com/diamondburned/arikawa/v3/state"