config.go 3.4 KB

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