config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "gopkg.in/yaml.v3"
  6. )
  7. const name = "discordo"
  8. type (
  9. MessagesTextKeysConfig struct {
  10. CopyContent string `yaml:"copy_content"`
  11. Reply string `yaml:"reply"`
  12. ReplyMention string `yaml:"reply_mention"`
  13. SelectReply string `yaml:"select_reply"`
  14. SelectPrevious string `yaml:"select_previous"`
  15. SelectNext string `yaml:"select_next"`
  16. SelectFirst string `yaml:"select_first"`
  17. SelectLast string `yaml:"select_last"`
  18. }
  19. MessageInputKeysConfig struct {
  20. Send string `yaml:"send"`
  21. Paste string `yaml:"paste"`
  22. LaunchEditor string `yaml:"launch_editor"`
  23. }
  24. KeysConfig struct {
  25. Cancel string `yaml:"cancel"`
  26. MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
  27. MessageInput MessageInputKeysConfig `yaml:"message_input"`
  28. }
  29. )
  30. type (
  31. GuildsTreeThemeConfig struct {
  32. Graphics bool `yaml:"graphics"`
  33. }
  34. MessagesTextThemeConfig struct {
  35. AuthorColor string `yaml:"author_color"`
  36. }
  37. MessageInputThemeConfig struct{}
  38. ThemeConfig struct {
  39. Border bool `yaml:"border"`
  40. BorderPadding [4]int `yaml:"border_padding,flow"`
  41. TitleColor string `yaml:"title_color"`
  42. BackgroundColor string `yaml:"background_color"`
  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. Editor string `yaml:"editor"`
  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. err = os.MkdirAll(path, os.ModePerm)
  63. if err != nil {
  64. return nil, err
  65. }
  66. c := Config{
  67. Mouse: true,
  68. Timestamps: false,
  69. MessagesLimit: 50,
  70. Editor: "default",
  71. Keys: KeysConfig{
  72. Cancel: "Esc",
  73. MessagesText: MessagesTextKeysConfig{
  74. CopyContent: "Rune[c]",
  75. Reply: "Rune[r]",
  76. ReplyMention: "Rune[R]",
  77. SelectReply: "Rune[s]",
  78. SelectPrevious: "Up",
  79. SelectNext: "Down",
  80. SelectFirst: "Home",
  81. SelectLast: "End",
  82. },
  83. MessageInput: MessageInputKeysConfig{
  84. Send: "Enter",
  85. Paste: "Ctrl+V",
  86. LaunchEditor: "Ctrl+E",
  87. },
  88. },
  89. Theme: ThemeConfig{
  90. Border: true,
  91. BorderPadding: [...]int{0, 0, 1, 1},
  92. TitleColor: "default",
  93. BackgroundColor: "default",
  94. GuildsTree: GuildsTreeThemeConfig{
  95. Graphics: true,
  96. },
  97. MessagesText: MessagesTextThemeConfig{
  98. AuthorColor: "aqua",
  99. },
  100. MessageInput: MessageInputThemeConfig{},
  101. },
  102. }
  103. path = filepath.Join(path, "config.yml")
  104. _, err = os.Stat(path)
  105. if os.IsNotExist(err) {
  106. f, err := os.Create(path)
  107. if err != nil {
  108. return nil, err
  109. }
  110. defer f.Close()
  111. err = yaml.NewEncoder(f).Encode(c)
  112. if err != nil {
  113. return nil, err
  114. }
  115. } else {
  116. f, err := os.Open(path)
  117. if err != nil {
  118. return nil, err
  119. }
  120. defer f.Close()
  121. err = yaml.NewDecoder(f).Decode(&c)
  122. if err != nil {
  123. return nil, err
  124. }
  125. }
  126. return &c, nil
  127. }