config.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/ayn2op/discordo/internal/constants"
  6. "gopkg.in/yaml.v3"
  7. )
  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. // TimestampsBeforeAuthor indicates whether to draw the timestamp before or after the author.
  15. TimestampsBeforeAuthor bool `yaml:"timestamps_before_author"`
  16. // Timestamps indicates whether to draw the timestamp in front of the message or not.
  17. Timestamps bool `yaml:"timestamps"`
  18. // 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.
  19. Editor string `yaml:"editor"`
  20. Keys Keys `yaml:"keys"`
  21. Theme Theme `yaml:"theme"`
  22. }
  23. func defConfig() Config {
  24. return Config{
  25. Mouse: true,
  26. TimestampsBeforeAuthor: false,
  27. Timestamps: false,
  28. MessagesLimit: 50,
  29. Editor: "default",
  30. Keys: defKeys(),
  31. Theme: defTheme(),
  32. }
  33. }
  34. func Load(path string) error {
  35. _, err := os.Stat(path)
  36. if os.IsNotExist(err) {
  37. f, err := os.Create(path)
  38. if err != nil {
  39. return err
  40. }
  41. defer f.Close()
  42. err = yaml.NewEncoder(f).Encode(Current)
  43. if err != nil {
  44. return err
  45. }
  46. } else {
  47. f, err := os.Open(path)
  48. if err != nil {
  49. return err
  50. }
  51. defer f.Close()
  52. err = yaml.NewDecoder(f).Decode(&Current)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }
  59. func DefaultPath() string {
  60. path, _ := os.UserConfigDir()
  61. return filepath.Join(path, constants.Name, "config.yml")
  62. }
  63. func DefaultLogPath() string {
  64. path, _ := os.UserCacheDir()
  65. return filepath.Join(path, constants.Name, "logs.txt")
  66. }