config.go 2.6 KB

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