config.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. "github.com/BurntSushi/toml"
  7. "github.com/ayn2op/discordo/internal/constants"
  8. )
  9. type Config struct {
  10. Mouse bool `toml:"mouse"`
  11. HideBlockedUsers bool `toml:"hide_blocked_users"`
  12. MessagesLimit uint8 `toml:"messages_limit"`
  13. Editor string `toml:"editor"`
  14. Timestamps bool `toml:"timestamps"`
  15. TimestampsBeforeAuthor bool `toml:"timestamps_before_author"`
  16. TimestampsFormat string `toml:"timestamps_format"`
  17. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  18. Keys Keys `toml:"keys"`
  19. Theme Theme `toml:"theme"`
  20. }
  21. func defaultConfig() *Config {
  22. return &Config{
  23. Mouse: true,
  24. HideBlockedUsers: true,
  25. MessagesLimit: 50,
  26. Editor: "default",
  27. Timestamps: false,
  28. TimestampsBeforeAuthor: false,
  29. TimestampsFormat: time.Kitchen,
  30. ShowAttachmentLinks: true,
  31. Keys: defaultKeys(),
  32. Theme: defaultTheme(),
  33. }
  34. }
  35. // Reads the configuration file and parses it.
  36. func Load() (*Config, error) {
  37. path := filepath.Join(constants.ConfigDirPath, "config.toml")
  38. f, err := os.Open(path)
  39. cfg := defaultConfig()
  40. if os.IsNotExist(err) {
  41. return cfg, nil
  42. }
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer f.Close()
  47. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  48. return nil, err
  49. }
  50. return cfg, nil
  51. }