config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  40. MessageInputThemeConfig struct {
  41. CommonThemeConfig `yaml:",inline"`
  42. }
  43. ThemeConfig struct {
  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. Keys KeysConfig `yaml:"keys"`
  54. Theme ThemeConfig `yaml:"theme"`
  55. }
  56. func newConfig() (*Config, error) {
  57. path, err := os.UserConfigDir()
  58. if err != nil {
  59. return nil, err
  60. }
  61. path = filepath.Join(path, name)
  62. if err = os.MkdirAll(path, os.ModePerm); err != nil {
  63. return nil, err
  64. }
  65. commonTheme := CommonThemeConfig{
  66. Border: true,
  67. BorderPadding: [...]int{0, 0, 1, 1},
  68. TitleColor: "default",
  69. BackgroundColor: "default",
  70. }
  71. commonKeys := CommonKeysConfig{
  72. Cancel: "Esc",
  73. }
  74. c := Config{
  75. Mouse: true,
  76. Timestamps: false,
  77. MessagesLimit: 50,
  78. Keys: KeysConfig{
  79. MessagesText: MessagesTextKeysConfig{
  80. CommonKeysConfig: commonKeys,
  81. Reply: "Rune[r]",
  82. ReplyMention: "Rune[R]",
  83. },
  84. MessageInput: MessageInputKeysConfig{
  85. CommonKeysConfig: commonKeys,
  86. Send: "Enter",
  87. },
  88. },
  89. Theme: ThemeConfig{
  90. GuildsTree: GuildsTreeThemeConfig{
  91. CommonThemeConfig: commonTheme,
  92. Graphics: true,
  93. },
  94. MessagesText: MessagesTextThemeConfig{
  95. CommonThemeConfig: commonTheme,
  96. },
  97. MessageInput: MessageInputThemeConfig{
  98. CommonThemeConfig: commonTheme,
  99. },
  100. },
  101. }
  102. path = filepath.Join(path, "config.yml")
  103. if _, err = os.Stat(path); os.IsNotExist(err) {
  104. f, err := os.Create(path)
  105. if err != nil {
  106. return nil, err
  107. }
  108. defer f.Close()
  109. e := yaml.NewEncoder(f)
  110. if err = e.Encode(c); err != nil {
  111. return nil, err
  112. }
  113. } else {
  114. f, err := os.Open(path)
  115. if err != nil {
  116. return nil, err
  117. }
  118. defer f.Close()
  119. if err = yaml.NewDecoder(f).Decode(&c); err != nil {
  120. return nil, err
  121. }
  122. }
  123. return &c, nil
  124. }