config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "gopkg.in/yaml.v3"
  6. )
  7. const Name = "discordo"
  8. var Current = defConfig()
  9. type Config struct {
  10. // Mouse indicates whether the mouse is usable or not.
  11. Mouse bool `yaml:"mouse"`
  12. // MessagesLimit is the number of messages to fetch when a text-based channel is selected.
  13. MessagesLimit uint `yaml:"messages_limit"`
  14. // Timestamps indicates whether to draw the timestamp in front of the message or not.
  15. Timestamps bool `yaml:"timestamps"`
  16. // Editor is the program to open when the `LaunchEditor` key is pressed. If the value of the field is "default", the `$EDITOR` environment variable is used instead.
  17. Editor string `yaml:"editor"`
  18. Keys Keys `yaml:"keys"`
  19. Theme Theme `yaml:"theme"`
  20. }
  21. func defConfig() Config {
  22. return Config{
  23. Mouse: true,
  24. Timestamps: false,
  25. MessagesLimit: 50,
  26. Editor: "default",
  27. Keys: defKeys(),
  28. Theme: defTheme(),
  29. }
  30. }
  31. func Load(path string) error {
  32. _, err := os.Stat(path)
  33. if os.IsNotExist(err) {
  34. f, err := os.Create(path)
  35. if err != nil {
  36. return err
  37. }
  38. defer f.Close()
  39. err = yaml.NewEncoder(f).Encode(Current)
  40. if err != nil {
  41. return err
  42. }
  43. } else {
  44. f, err := os.Open(path)
  45. if err != nil {
  46. return err
  47. }
  48. defer f.Close()
  49. err = yaml.NewDecoder(f).Decode(&Current)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }
  56. func DefaultPath() string {
  57. path, _ := os.UserConfigDir()
  58. return filepath.Join(path, Name, "config.yml")
  59. }
  60. func DefaultLogPath() string {
  61. path, _ := os.UserCacheDir()
  62. return filepath.Join(path, Name, "logs.txt")
  63. }