ayn2op 3 rokov pred
rodič
commit
b211eb08ae
6 zmenil súbory, kde vykonal 42 pridanie a 47 odobranie
  1. 29 44
      config.go
  2. 1 0
      go.mod
  3. 3 0
      go.sum
  4. 3 1
      guilds_tree.go
  5. 3 1
      message_input.go
  6. 3 1
      messages_text.go

+ 29 - 44
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]
-}

+ 1 - 0
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
 )

+ 3 - 0
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=

+ 3 - 1
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
 }

+ 3 - 1
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
 }

+ 3 - 1
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
 }