From b211eb08aecac6710331abd1567bf10a8dead237 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Sun, 1 Jan 2023 15:44:49 +0530 Subject: [PATCH] Switch to yaml --- config.go | 73 +++++++++++++++++++----------------------------- go.mod | 1 + go.sum | 3 ++ guilds_tree.go | 4 ++- message_input.go | 4 ++- messages_text.go | 4 ++- 6 files changed, 42 insertions(+), 47 deletions(-) diff --git a/config.go b/config.go index b0f004b..f229133 100644 --- a/config.go +++ b/config.go @@ -1,49 +1,43 @@ package main import ( - "encoding/json" "os" "path/filepath" + + "gopkg.in/yaml.v3" ) const name = "discordo" +type CommonThemeConfig struct { + Border bool `yaml:"border"` + BorderPadding [4]int `yaml:"border_padding"` +} + type GuildsTreeThemeConfig struct { - Border bool - Graphics bool + CommonThemeConfig `yaml:",inline"` + Graphics bool `yaml:"graphics"` } type MessagesTextThemeConfig struct { - Border bool + CommonThemeConfig `yaml:",inline"` } type MessageInputThemeConfig struct { - Border bool + CommonThemeConfig `yaml:",inline"` } type ThemeConfig struct { - BorderPadding [4]int - - GuildsTree GuildsTreeThemeConfig - MessagesText MessagesTextThemeConfig - MessageInput MessageInputThemeConfig -} - -type MessagesTextKeysConfig struct { - SelectPreviousMessage string - SelectNextMessage string -} - -type KeysConfig struct { - MessagesText MessagesTextKeysConfig + GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"` + MessagesText MessagesTextThemeConfig `yaml:"messages_text"` + MessageInput MessageInputThemeConfig `yaml:"message_input"` } type Config struct { - Mouse bool - MessagesLimit uint + Mouse bool `yaml:"mouse"` + MessagesLimit uint `yaml:"messages_limit"` - Theme ThemeConfig - Keys KeysConfig + Theme ThemeConfig `yaml:"theme"` } func newConfig() (*Config, error) { @@ -57,32 +51,29 @@ func newConfig() (*Config, error) { return nil, err } + common := CommonThemeConfig{ + Border: true, + BorderPadding: [...]int{1, 1, 1, 1}, + } + c := Config{ Mouse: true, MessagesLimit: 50, Theme: ThemeConfig{ - BorderPadding: [...]int{1, 1, 1, 1}, - GuildsTree: GuildsTreeThemeConfig{ - Border: true, - Graphics: true, + CommonThemeConfig: common, + Graphics: true, }, MessagesText: MessagesTextThemeConfig{ - Border: true, + CommonThemeConfig: common, }, MessageInput: MessageInputThemeConfig{ - Border: true, - }, - }, - Keys: KeysConfig{ - MessagesText: MessagesTextKeysConfig{ - SelectPreviousMessage: "Up", - SelectNextMessage: "Down", + CommonThemeConfig: common, }, }, } - path = filepath.Join(path, "config.json") + path = filepath.Join(path, "config.yaml") if _, err = os.Stat(path); os.IsNotExist(err) { f, err := os.Create(path) if err != nil { @@ -90,8 +81,7 @@ func newConfig() (*Config, error) { } defer f.Close() - e := json.NewEncoder(f) - e.SetIndent("", "\t") + e := yaml.NewEncoder(f) if err = e.Encode(c); err != nil { return nil, err } @@ -102,15 +92,10 @@ func newConfig() (*Config, error) { } defer f.Close() - if err = json.NewDecoder(f).Decode(&c); err != nil { + if err = yaml.NewDecoder(f).Decode(&c); err != nil { return nil, err } } return &c, nil } - -func (c *Config) BorderPadding() (int, int, int, int) { - pad := c.Theme.BorderPadding - return pad[0], pad[1], pad[2], pad[3] -} diff --git a/go.mod b/go.mod index f7373dd..1fc2a0e 100644 --- a/go.mod +++ b/go.mod @@ -20,4 +20,5 @@ require ( golang.org/x/term v0.3.0 // indirect golang.org/x/text v0.5.0 // indirect golang.org/x/time v0.3.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6085ebd..096b877 100644 --- a/go.sum +++ b/go.sum @@ -42,3 +42,6 @@ golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/guilds_tree.go b/guilds_tree.go index df23d1a..a955865 100644 --- a/guilds_tree.go +++ b/guilds_tree.go @@ -28,7 +28,9 @@ func newGuildsTree() *GuildsTree { gt.SetSelectedFunc(gt.onSelected) gt.SetBorder(cfg.Theme.GuildsTree.Border) - gt.SetBorderPadding(cfg.BorderPadding()) + + padding := cfg.Theme.GuildsTree.BorderPadding + gt.SetBorderPadding(padding[0], padding[1], padding[2], padding[3]) return gt } diff --git a/message_input.go b/message_input.go index 656e3f5..20cdf34 100644 --- a/message_input.go +++ b/message_input.go @@ -14,7 +14,9 @@ func newMessageInput() *MessageInput { } mi.SetBorder(cfg.Theme.MessageInput.Border) - mi.SetBorderPadding(cfg.BorderPadding()) + + padding := cfg.Theme.MessageInput.BorderPadding + mi.SetBorderPadding(padding[0], padding[1], padding[2], padding[3]) return mi } diff --git a/messages_text.go b/messages_text.go index e4af360..f448db1 100644 --- a/messages_text.go +++ b/messages_text.go @@ -24,7 +24,9 @@ func newMessagesText() *MessagesText { mt.SetWordWrap(true) mt.SetBorder(cfg.Theme.MessagesText.Border) - mt.SetBorderPadding(cfg.BorderPadding()) + + padding := cfg.Theme.MessagesText.BorderPadding + mt.SetBorderPadding(padding[0], padding[1], padding[2], padding[3]) return mt }