config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package util
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. // Theme defines the theme for the application.
  7. type Theme struct {
  8. Background string `json:"background,omitempty"`
  9. Foreground string `json:"foreground,omitempty"`
  10. Borders bool `json:"borders,omitempty"`
  11. }
  12. // Config consists of fields, such as theme, mouse, so on, that may be customized by the user.
  13. type Config struct {
  14. Mouse bool `json:"mouse,omitempty"`
  15. GetMessagesLimit uint `json:"getMessagesLimit,omitempty"`
  16. Theme *Theme `json:"theme,omitempty"`
  17. }
  18. // NewConfig reads the configuration file (if exists) and returns a new config.
  19. func NewConfig() *Config {
  20. userHomeDir, err := os.UserHomeDir()
  21. if err != nil {
  22. panic(err)
  23. }
  24. var c Config = Config{
  25. Mouse: true,
  26. GetMessagesLimit: 50,
  27. Theme: &Theme{
  28. Borders: true,
  29. },
  30. }
  31. configPath := userHomeDir + "/.config/discordo/config.json"
  32. if _, err := os.Stat(configPath); os.IsNotExist(err) {
  33. return &c
  34. }
  35. d, err := os.ReadFile(configPath)
  36. if err != nil {
  37. panic(err)
  38. }
  39. if err = json.Unmarshal(d, &c); err != nil {
  40. panic(err)
  41. }
  42. return &c
  43. }