config.go 2.9 KB

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