config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "gopkg.in/yaml.v3"
  6. )
  7. const Name = "discordo"
  8. type (
  9. MessagesTextKeysConfig struct {
  10. CopyContent string `yaml:"copy_content"`
  11. Reply string `yaml:"reply"`
  12. ReplyMention string `yaml:"reply_mention"`
  13. SelectReply string `yaml:"select_reply"`
  14. SelectPrevious string `yaml:"select_previous"`
  15. SelectNext string `yaml:"select_next"`
  16. SelectFirst string `yaml:"select_first"`
  17. SelectLast string `yaml:"select_last"`
  18. }
  19. MessageInputKeysConfig struct {
  20. Send string `yaml:"send"`
  21. Paste string `yaml:"paste"`
  22. LaunchEditor string `yaml:"launch_editor"`
  23. }
  24. KeysConfig struct {
  25. Cancel string `yaml:"cancel"`
  26. MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
  27. MessageInput MessageInputKeysConfig `yaml:"message_input"`
  28. }
  29. )
  30. type (
  31. GuildsTreeThemeConfig struct {
  32. Graphics bool `yaml:"graphics"`
  33. }
  34. MessagesTextThemeConfig struct {
  35. AuthorColor string `yaml:"author_color"`
  36. }
  37. MessageInputThemeConfig struct{}
  38. ThemeConfig struct {
  39. Border bool `yaml:"border"`
  40. BorderColor string `yaml:"border_color"`
  41. BorderPadding [4]int `yaml:"border_padding,flow"`
  42. TitleColor string `yaml:"title_color"`
  43. BackgroundColor string `yaml:"background_color"`
  44. GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
  45. MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
  46. MessageInput MessageInputThemeConfig `yaml:"message_input"`
  47. }
  48. )
  49. type Config struct {
  50. Mouse bool `yaml:"mouse"`
  51. MessagesLimit uint `yaml:"messages_limit"`
  52. Timestamps bool `yaml:"timestamps"`
  53. Editor string `yaml:"editor"`
  54. Keys KeysConfig `yaml:"keys"`
  55. Theme ThemeConfig `yaml:"theme"`
  56. }
  57. func Load() (*Config, error) {
  58. path, err := os.UserConfigDir()
  59. if err != nil {
  60. return nil, err
  61. }
  62. path = filepath.Join(path, Name)
  63. err = os.MkdirAll(path, os.ModePerm)
  64. if err != nil {
  65. return nil, err
  66. }
  67. c := Config{
  68. Mouse: true,
  69. Timestamps: false,
  70. MessagesLimit: 50,
  71. Editor: "default",
  72. Keys: KeysConfig{
  73. Cancel: "Esc",
  74. MessagesText: MessagesTextKeysConfig{
  75. CopyContent: "Rune[c]",
  76. Reply: "Rune[r]",
  77. ReplyMention: "Rune[R]",
  78. SelectReply: "Rune[s]",
  79. SelectPrevious: "Up",
  80. SelectNext: "Down",
  81. SelectFirst: "Home",
  82. SelectLast: "End",
  83. },
  84. MessageInput: MessageInputKeysConfig{
  85. Send: "Enter",
  86. Paste: "Ctrl+V",
  87. LaunchEditor: "Ctrl+E",
  88. },
  89. },
  90. Theme: ThemeConfig{
  91. Border: true,
  92. BorderColor: "default",
  93. BorderPadding: [...]int{0, 0, 1, 1},
  94. TitleColor: "default",
  95. BackgroundColor: "default",
  96. GuildsTree: GuildsTreeThemeConfig{
  97. Graphics: true,
  98. },
  99. MessagesText: MessagesTextThemeConfig{
  100. AuthorColor: "aqua",
  101. },
  102. MessageInput: MessageInputThemeConfig{},
  103. },
  104. }
  105. path = filepath.Join(path, "config.yml")
  106. _, err = os.Stat(path)
  107. if os.IsNotExist(err) {
  108. f, err := os.Create(path)
  109. if err != nil {
  110. return nil, err
  111. }
  112. defer f.Close()
  113. err = yaml.NewEncoder(f).Encode(c)
  114. if err != nil {
  115. return nil, err
  116. }
  117. } else {
  118. f, err := os.Open(path)
  119. if err != nil {
  120. return nil, err
  121. }
  122. defer f.Close()
  123. err = yaml.NewDecoder(f).Decode(&c)
  124. if err != nil {
  125. return nil, err
  126. }
  127. }
  128. return &c, nil
  129. }