config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "github.com/BurntSushi/toml"
  9. "github.com/ayn2op/discordo/internal/consts"
  10. "github.com/diamondburned/arikawa/v3/discord"
  11. )
  12. const fileName = "config.toml"
  13. type (
  14. Timestamps struct {
  15. Enabled bool `toml:"enabled"`
  16. Format string `toml:"format"`
  17. }
  18. Notifications struct {
  19. Enabled bool `toml:"enabled"`
  20. Duration int `toml:"duration"`
  21. Sound Sound `toml:"sound"`
  22. }
  23. Sound struct {
  24. Enabled bool `toml:"enabled"`
  25. OnlyOnPing bool `toml:"only_on_ping"`
  26. }
  27. TypingIndicator struct {
  28. Send bool `toml:"send"`
  29. Receive bool `toml:"receive"`
  30. }
  31. Icons struct {
  32. GuildCategory string `toml:"guild_category"`
  33. GuildText string `toml:"guild_text"`
  34. GuildVoice string `toml:"guild_voice"`
  35. GuildStageVoice string `toml:"guild_stage_voice"`
  36. GuildAnnouncementThread string `toml:"guild_announcement_thread"`
  37. GuildPublicThread string `toml:"guild_public_thread"`
  38. GuildPrivateThread string `toml:"guild_private_thread"`
  39. GuildAnnouncement string `toml:"guild_announcement"`
  40. GuildForum string `toml:"guild_forum"`
  41. GuildStore string `toml:"guild_store"`
  42. }
  43. PickerConfig struct {
  44. Width int `toml:"width"`
  45. Height int `toml:"height"`
  46. }
  47. MarkdownConfig struct {
  48. Enabled bool `toml:"enabled"`
  49. Theme string `toml:"theme"`
  50. }
  51. Config struct {
  52. AutoFocus bool `toml:"auto_focus"`
  53. Mouse bool `toml:"mouse"`
  54. Editor string `toml:"editor"`
  55. Status discord.Status `toml:"status"`
  56. HideBlockedUsers bool `toml:"hide_blocked_users"`
  57. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  58. // Use 0 to disable
  59. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  60. MessagesLimit uint8 `toml:"messages_limit"`
  61. Markdown MarkdownConfig `toml:"markdown"`
  62. Picker PickerConfig `toml:"picker"`
  63. Timestamps Timestamps `toml:"timestamps"`
  64. Notifications Notifications `toml:"notifications"`
  65. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  66. Icons Icons `toml:"icons"`
  67. Keybinds Keybinds `toml:"keybinds"`
  68. Theme Theme `toml:"theme"`
  69. }
  70. )
  71. //go:embed config.toml
  72. var defaultCfg []byte
  73. func DefaultPath() string {
  74. path, err := os.UserConfigDir()
  75. if err != nil {
  76. slog.Info(
  77. "user config dir cannot be determined; falling back to the current dir",
  78. "err", err,
  79. )
  80. path = "."
  81. }
  82. return filepath.Join(path, consts.Name, fileName)
  83. }
  84. // Load reads the configuration file and parses it.
  85. func Load(path string) (*Config, error) {
  86. var cfg Config
  87. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  88. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  89. }
  90. file, err := os.Open(path)
  91. if os.IsNotExist(err) {
  92. slog.Info(
  93. "config file does not exist, falling back to the default config",
  94. "path",
  95. path,
  96. "err",
  97. err,
  98. )
  99. } else {
  100. if err != nil {
  101. return nil, fmt.Errorf("failed to open config file: %w", err)
  102. }
  103. defer file.Close()
  104. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  105. return nil, fmt.Errorf("failed to decode config: %w", err)
  106. }
  107. }
  108. applyDefaults(&cfg)
  109. return &cfg, nil
  110. }
  111. func applyDefaults(cfg *Config) {
  112. if cfg.Editor == "default" {
  113. cfg.Editor = os.Getenv("EDITOR")
  114. }
  115. if cfg.Status == "default" {
  116. cfg.Status = discord.UnknownStatus
  117. }
  118. }