config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. LaunchEditor string `yaml:"launch_editor"`
  22. }
  23. KeysConfig struct {
  24. Cancel string `yaml:"cancel"`
  25. MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
  26. MessageInput MessageInputKeysConfig `yaml:"message_input"`
  27. }
  28. )
  29. type (
  30. GuildsTreeThemeConfig struct {
  31. Graphics bool `yaml:"graphics"`
  32. }
  33. MessagesTextThemeConfig struct {
  34. AuthorColor string `yaml:"author_color"`
  35. }
  36. MessageInputThemeConfig struct{}
  37. ThemeConfig struct {
  38. Border bool `yaml:"border"`
  39. BorderPadding [4]int `yaml:"border_padding,flow"`
  40. TitleColor string `yaml:"title_color"`
  41. BackgroundColor string `yaml:"background_color"`
  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. Editor string `yaml:"editor"`
  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. err = os.MkdirAll(path, os.ModePerm)
  62. if err != nil {
  63. return nil, err
  64. }
  65. c := Config{
  66. Mouse: true,
  67. Timestamps: false,
  68. MessagesLimit: 50,
  69. Editor: "default",
  70. Keys: KeysConfig{
  71. Cancel: "Esc",
  72. MessagesText: MessagesTextKeysConfig{
  73. CopyContent: "Rune[c]",
  74. Reply: "Rune[r]",
  75. ReplyMention: "Rune[R]",
  76. SelectReply: "Rune[s]",
  77. SelectPrevious: "Up",
  78. SelectNext: "Down",
  79. SelectFirst: "Home",
  80. SelectLast: "End",
  81. },
  82. MessageInput: MessageInputKeysConfig{
  83. Send: "Enter",
  84. LaunchEditor: "Ctrl+E",
  85. },
  86. },
  87. Theme: ThemeConfig{
  88. Border: true,
  89. BorderPadding: [...]int{0, 0, 1, 1},
  90. TitleColor: "default",
  91. BackgroundColor: "default",
  92. GuildsTree: GuildsTreeThemeConfig{
  93. Graphics: true,
  94. },
  95. MessagesText: MessagesTextThemeConfig{
  96. AuthorColor: "aqua",
  97. },
  98. MessageInput: MessageInputThemeConfig{},
  99. },
  100. }
  101. path = filepath.Join(path, "config.yml")
  102. _, err = os.Stat(path)
  103. if os.IsNotExist(err) {
  104. f, err := os.Create(path)
  105. if err != nil {
  106. return nil, err
  107. }
  108. defer f.Close()
  109. err = yaml.NewEncoder(f).Encode(c)
  110. if 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. err = yaml.NewDecoder(f).Decode(&c)
  120. if err != nil {
  121. return nil, err
  122. }
  123. }
  124. return &c, nil
  125. }