config.go 2.9 KB

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