config.go 3.2 KB

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