config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Config struct {
  48. AutoFocus bool `toml:"auto_focus"`
  49. Mouse bool `toml:"mouse"`
  50. Editor string `toml:"editor"`
  51. Status discord.Status `toml:"status"`
  52. Markdown bool `toml:"markdown"`
  53. HideBlockedUsers bool `toml:"hide_blocked_users"`
  54. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  55. // Use 0 to disable
  56. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  57. MessagesLimit uint8 `toml:"messages_limit"`
  58. Picker PickerConfig `toml:"picker"`
  59. Timestamps Timestamps `toml:"timestamps"`
  60. Notifications Notifications `toml:"notifications"`
  61. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  62. Icons Icons `toml:"icons"`
  63. Keybinds Keybinds `toml:"keybinds"`
  64. Theme Theme `toml:"theme"`
  65. }
  66. )
  67. //go:embed config.toml
  68. var defaultCfg []byte
  69. func DefaultPath() string {
  70. path, err := os.UserConfigDir()
  71. if err != nil {
  72. slog.Info(
  73. "user config dir cannot be determined; falling back to the current dir",
  74. "err", err,
  75. )
  76. path = "."
  77. }
  78. return filepath.Join(path, consts.Name, fileName)
  79. }
  80. // Load reads the configuration file and parses it.
  81. func Load(path string) (*Config, error) {
  82. var cfg Config
  83. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  84. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  85. }
  86. file, err := os.Open(path)
  87. if os.IsNotExist(err) {
  88. slog.Info(
  89. "config file does not exist, falling back to the default config",
  90. "path",
  91. path,
  92. "err",
  93. err,
  94. )
  95. } else {
  96. if err != nil {
  97. return nil, fmt.Errorf("failed to open config file: %w", err)
  98. }
  99. defer file.Close()
  100. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  101. return nil, fmt.Errorf("failed to decode config: %w", err)
  102. }
  103. }
  104. applyDefaults(&cfg)
  105. return &cfg, nil
  106. }
  107. func applyDefaults(cfg *Config) {
  108. if cfg.Editor == "default" {
  109. cfg.Editor = os.Getenv("EDITOR")
  110. }
  111. if cfg.Status == "default" {
  112. cfg.Status = discord.UnknownStatus
  113. }
  114. }