config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  21. KeysConfig struct {
  22. MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
  23. MessageInput MessageInputKeysConfig `yaml:"message_input"`
  24. }
  25. )
  26. type (
  27. CommonThemeConfig struct {
  28. Border bool `yaml:"border"`
  29. BorderPadding [4]int `yaml:"border_padding,flow"`
  30. TitleColor string `yaml:"title_color"`
  31. BackgroundColor string `yaml:"background_color"`
  32. }
  33. GuildsTreeThemeConfig struct {
  34. CommonThemeConfig `yaml:",inline"`
  35. Graphics bool `yaml:"graphics"`
  36. }
  37. MessagesTextThemeConfig struct {
  38. CommonThemeConfig `yaml:",inline"`
  39. AuthorColor string `yaml:"author_color"`
  40. }
  41. MessageInputThemeConfig struct {
  42. CommonThemeConfig `yaml:",inline"`
  43. }
  44. ThemeConfig struct {
  45. GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
  46. MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
  47. MessageInput MessageInputThemeConfig `yaml:"message_input"`
  48. }
  49. )
  50. type Config struct {
  51. Mouse bool `yaml:"mouse"`
  52. MessagesLimit uint `yaml:"messages_limit"`
  53. Timestamps bool `yaml:"timestamps"`
  54. Keys KeysConfig `yaml:"keys"`
  55. Theme ThemeConfig `yaml:"theme"`
  56. }
  57. func newConfig() (*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. commonTheme := CommonThemeConfig{
  68. Border: true,
  69. BorderPadding: [...]int{0, 0, 1, 1},
  70. TitleColor: "default",
  71. BackgroundColor: "default",
  72. }
  73. commonKeys := CommonKeysConfig{
  74. Cancel: "Esc",
  75. }
  76. c := Config{
  77. Mouse: true,
  78. Timestamps: false,
  79. MessagesLimit: 50,
  80. Keys: KeysConfig{
  81. MessagesText: MessagesTextKeysConfig{
  82. CommonKeysConfig: commonKeys,
  83. Reply: "Rune[r]",
  84. ReplyMention: "Rune[R]",
  85. },
  86. MessageInput: MessageInputKeysConfig{
  87. CommonKeysConfig: commonKeys,
  88. Send: "Enter",
  89. },
  90. },
  91. Theme: ThemeConfig{
  92. GuildsTree: GuildsTreeThemeConfig{
  93. CommonThemeConfig: commonTheme,
  94. Graphics: true,
  95. },
  96. MessagesText: MessagesTextThemeConfig{
  97. CommonThemeConfig: commonTheme,
  98. AuthorColor: "aqua",
  99. },
  100. MessageInput: MessageInputThemeConfig{
  101. CommonThemeConfig: commonTheme,
  102. },
  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. }