config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. "github.com/BurntSushi/toml"
  7. )
  8. const Name = "discordo"
  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. TimestampsFormat string `toml:"timestamps_format"`
  16. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  17. Keys Keys `toml:"keys"`
  18. Theme Theme `toml:"theme"`
  19. }
  20. func defaultConfig() *Config {
  21. return &Config{
  22. Mouse: true,
  23. HideBlockedUsers: true,
  24. MessagesLimit: 50,
  25. Editor: "default",
  26. Timestamps: false,
  27. TimestampsFormat: time.Kitchen,
  28. ShowAttachmentLinks: true,
  29. Keys: defaultKeys(),
  30. Theme: defaultTheme(),
  31. }
  32. }
  33. // Reads the configuration file and parses it.
  34. func Load() (*Config, error) {
  35. path, err := os.UserConfigDir()
  36. if err != nil {
  37. path = "."
  38. }
  39. path = filepath.Join(path, Name, "config.toml")
  40. f, err := os.Open(path)
  41. cfg := defaultConfig()
  42. if os.IsNotExist(err) {
  43. return cfg, nil
  44. }
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer f.Close()
  49. if _, err := toml.NewDecoder(f).Decode(&cfg); err != nil {
  50. return nil, err
  51. }
  52. return cfg, nil
  53. }