config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Graphics bool `yaml:"graphics"`
  52. } `yaml:"guilds_tree"`
  53. MessagesText struct {
  54. AuthorColor string `yaml:"author_color"`
  55. ReplyIndicator string `yaml:"reply_indicator"`
  56. } `yaml:"messages_text"`
  57. } `yaml:"theme"`
  58. }
  59. func Load() (*Config, error) {
  60. path, err := os.UserConfigDir()
  61. if err != nil {
  62. return nil, err
  63. }
  64. path = filepath.Join(path, constants.Name)
  65. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  66. return nil, err
  67. }
  68. path = filepath.Join(path, "config.yml")
  69. f, err := os.Open(path)
  70. reader := io.Reader(f)
  71. if os.IsNotExist(err) {
  72. err = os.WriteFile(path, defaultConfig, os.ModePerm)
  73. reader = bytes.NewReader(defaultConfig)
  74. }
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer f.Close()
  79. var cfg Config
  80. if err := yaml.NewDecoder(reader).Decode(&cfg); err != nil {
  81. return nil, err
  82. }
  83. return &cfg, nil
  84. }