config.go 3.5 KB

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