config.go 3.0 KB

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