config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "gopkg.in/yaml.v3"
  6. )
  7. const name = "discordo"
  8. type CommonThemeConfig struct {
  9. Border bool `yaml:"border"`
  10. BorderPadding [4]int `yaml:"border_padding,flow"`
  11. }
  12. type GuildsTreeThemeConfig struct {
  13. CommonThemeConfig `yaml:",inline"`
  14. Graphics bool `yaml:"graphics"`
  15. }
  16. type MessagesTextThemeConfig struct {
  17. CommonThemeConfig `yaml:",inline"`
  18. }
  19. type MessageInputThemeConfig struct {
  20. CommonThemeConfig `yaml:",inline"`
  21. }
  22. type ThemeConfig struct {
  23. GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
  24. MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
  25. MessageInput MessageInputThemeConfig `yaml:"message_input"`
  26. }
  27. type Config struct {
  28. Mouse bool `yaml:"mouse"`
  29. MessagesLimit uint `yaml:"messages_limit"`
  30. Timestamps bool `yaml:"timestamps"`
  31. Theme ThemeConfig `yaml:"theme"`
  32. }
  33. func newConfig() (*Config, error) {
  34. path, err := os.UserConfigDir()
  35. if err != nil {
  36. return nil, err
  37. }
  38. path = filepath.Join(path, name)
  39. if err = os.MkdirAll(path, os.ModePerm); err != nil {
  40. return nil, err
  41. }
  42. common := CommonThemeConfig{
  43. Border: true,
  44. BorderPadding: [...]int{0, 0, 1, 1},
  45. }
  46. c := Config{
  47. Mouse: true,
  48. MessagesLimit: 50,
  49. Theme: ThemeConfig{
  50. GuildsTree: GuildsTreeThemeConfig{
  51. CommonThemeConfig: common,
  52. Graphics: true,
  53. },
  54. MessagesText: MessagesTextThemeConfig{
  55. CommonThemeConfig: common,
  56. },
  57. MessageInput: MessageInputThemeConfig{
  58. CommonThemeConfig: common,
  59. },
  60. },
  61. }
  62. path = filepath.Join(path, "config.yml")
  63. if _, err = os.Stat(path); os.IsNotExist(err) {
  64. f, err := os.Create(path)
  65. if err != nil {
  66. return nil, err
  67. }
  68. defer f.Close()
  69. e := yaml.NewEncoder(f)
  70. if err = e.Encode(c); err != nil {
  71. return nil, err
  72. }
  73. } else {
  74. f, err := os.Open(path)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer f.Close()
  79. if err = yaml.NewDecoder(f).Decode(&c); err != nil {
  80. return nil, err
  81. }
  82. }
  83. return &c, nil
  84. }