config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package config
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "github.com/ayn2op/discordo/internal/constants"
  9. "gopkg.in/yaml.v3"
  10. )
  11. //go:embed config.yml
  12. var defaultConfig []byte
  13. type Config struct {
  14. Mouse bool `yaml:"mouse"`
  15. Timestamps bool `yaml:"timestamps"`
  16. TimestampsBeforeAuthor bool `yaml:"timestamps_before_author"`
  17. MessagesLimit uint8 `yaml:"messages_limit"`
  18. Editor string `yaml:"editor"`
  19. Keys struct {
  20. Cancel string `yaml:"cancel"`
  21. GuildsTree struct {
  22. Focus string `yaml:"focus"`
  23. Toggle string `yaml:"toggle"`
  24. } `yaml:"guilds_tree"`
  25. MessagesText struct {
  26. Focus string `yaml:"focus"`
  27. ShowImage string `yaml:"show_image"`
  28. CopyContent string `yaml:"copy_content"`
  29. Reply string `yaml:"reply"`
  30. ReplyMention string `yaml:"reply_mention"`
  31. Delete string `yaml:"delete"`
  32. SelectPrevious string `yaml:"select_previous"`
  33. SelectNext string `yaml:"select_next"`
  34. SelectFirst string `yaml:"select_first"`
  35. SelectLast string `yaml:"select_last"`
  36. SelectReply string `yaml:"select_reply"`
  37. } `yaml:"messages_text"`
  38. MessageInput struct {
  39. Focus string `yaml:"focus"`
  40. Send string `yaml:"send"`
  41. LaunchEditor string `yaml:"launch_editor"`
  42. } `yaml:"message_input"`
  43. } `yaml:"keys"`
  44. Theme struct {
  45. Border bool `yaml:"border"`
  46. BorderColor string `yaml:"border_color"`
  47. BorderPadding [4]int `yaml:"border_padding,flow"`
  48. TitleColor string `yaml:"title_color"`
  49. BackgroundColor string `yaml:"background_color"`
  50. GuildsTree struct {
  51. AutoExpandFolders bool `yaml:"auto_expand_folders"`
  52. Graphics bool `yaml:"graphics"`
  53. } `yaml:"guilds_tree"`
  54. MessagesText struct {
  55. AuthorColor string `yaml:"author_color"`
  56. ReplyIndicator string `yaml:"reply_indicator"`
  57. } `yaml:"messages_text"`
  58. } `yaml:"theme"`
  59. }
  60. // Recursively creates the configuration directory if it does not exist already and returns the path to the configuration file.
  61. func initialize() (string, error) {
  62. path, err := os.UserConfigDir()
  63. if err != nil {
  64. return "", err
  65. }
  66. path = filepath.Join(path, constants.Name)
  67. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  68. return "", err
  69. }
  70. return filepath.Join(path, "config.yml"), nil
  71. }
  72. // Reads the configuration file and parses it.
  73. func Load() (*Config, error) {
  74. path, err := initialize()
  75. if err != nil {
  76. return nil, err
  77. }
  78. f, err := os.Open(path)
  79. reader := io.Reader(f)
  80. if os.IsNotExist(err) {
  81. err = os.WriteFile(path, defaultConfig, os.ModePerm)
  82. reader = bytes.NewReader(defaultConfig)
  83. }
  84. if err != nil {
  85. return nil, err
  86. }
  87. defer f.Close()
  88. var cfg Config
  89. if err := yaml.NewDecoder(reader).Decode(&cfg); err != nil {
  90. return nil, err
  91. }
  92. return &cfg, nil
  93. }