config.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "unicode/utf8"
  10. "github.com/BurntSushi/toml"
  11. "github.com/ayn2op/discordo/internal/consts"
  12. "github.com/diamondburned/arikawa/v3/discord"
  13. )
  14. const fileName = "config.toml"
  15. type (
  16. Timestamps struct {
  17. Enabled bool `toml:"enabled"`
  18. Format string `toml:"format"`
  19. }
  20. DateSeparator struct {
  21. Enabled bool `toml:"enabled"`
  22. Format string `toml:"format"`
  23. Character string `toml:"character"`
  24. }
  25. Notifications struct {
  26. Enabled bool `toml:"enabled"`
  27. Duration int `toml:"duration"`
  28. Sound Sound `toml:"sound"`
  29. }
  30. Sound struct {
  31. Enabled bool `toml:"enabled"`
  32. OnlyOnPing bool `toml:"only_on_ping"`
  33. }
  34. TypingIndicator struct {
  35. Send bool `toml:"send"`
  36. Receive bool `toml:"receive"`
  37. }
  38. Icons struct {
  39. GuildCategory string `toml:"guild_category"`
  40. GuildText string `toml:"guild_text"`
  41. GuildVoice string `toml:"guild_voice"`
  42. GuildStageVoice string `toml:"guild_stage_voice"`
  43. GuildAnnouncementThread string `toml:"guild_announcement_thread"`
  44. GuildPublicThread string `toml:"guild_public_thread"`
  45. GuildPrivateThread string `toml:"guild_private_thread"`
  46. GuildAnnouncement string `toml:"guild_announcement"`
  47. GuildForum string `toml:"guild_forum"`
  48. GuildStore string `toml:"guild_store"`
  49. }
  50. PickerConfig struct {
  51. Width int `toml:"width"`
  52. Height int `toml:"height"`
  53. }
  54. MarkdownConfig struct {
  55. Enabled bool `toml:"enabled"`
  56. Theme string `toml:"theme"`
  57. }
  58. HelpConfig struct {
  59. CompactModifiers bool `toml:"compact_modifiers"`
  60. Padding [2]int `toml:"padding"`
  61. Separator string `toml:"separator"`
  62. }
  63. SidebarMarkersConfig struct {
  64. Expanded string `toml:"expanded"`
  65. Collapsed string `toml:"collapsed"`
  66. Leaf string `toml:"leaf"`
  67. }
  68. SidebarConfig struct {
  69. Markers SidebarMarkersConfig `toml:"markers"`
  70. }
  71. Config struct {
  72. AutoFocus bool `toml:"auto_focus"`
  73. Mouse bool `toml:"mouse"`
  74. Editor string `toml:"editor"`
  75. Status discord.Status `toml:"status"`
  76. HideBlockedUsers bool `toml:"hide_blocked_users"`
  77. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  78. ImageViewer string `toml:"image_viewer"`
  79. ImageSaveDir string `toml:"image_save_dir"`
  80. // Use 0 to disable
  81. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  82. MessagesLimit uint8 `toml:"messages_limit"`
  83. Markdown MarkdownConfig `toml:"markdown"`
  84. Help HelpConfig `toml:"help"`
  85. Picker PickerConfig `toml:"picker"`
  86. Timestamps Timestamps `toml:"timestamps"`
  87. DateSeparator DateSeparator `toml:"date_separator"`
  88. Notifications Notifications `toml:"notifications"`
  89. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  90. Sidebar SidebarConfig `toml:"sidebar"`
  91. Icons Icons `toml:"icons"`
  92. Keybinds Keybinds `toml:"keybinds"`
  93. Theme Theme `toml:"theme"`
  94. }
  95. )
  96. //go:embed config.toml
  97. var defaultCfg []byte
  98. func DefaultPath() string {
  99. path, err := os.UserConfigDir()
  100. if err != nil {
  101. slog.Info(
  102. "user config dir cannot be determined; falling back to the current dir",
  103. "err", err,
  104. )
  105. path = "."
  106. }
  107. return filepath.Join(path, consts.Name, fileName)
  108. }
  109. // Load reads the configuration file and parses it.
  110. func Load(path string) (*Config, error) {
  111. cfg := Config{
  112. Keybinds: defaultKeybinds(),
  113. }
  114. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  115. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  116. }
  117. file, err := os.Open(path)
  118. if os.IsNotExist(err) {
  119. slog.Info(
  120. "config file does not exist, falling back to the default config",
  121. "path",
  122. path,
  123. "err",
  124. err,
  125. )
  126. } else {
  127. if err != nil {
  128. return nil, fmt.Errorf("failed to open config file: %w", err)
  129. }
  130. defer file.Close()
  131. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  132. return nil, fmt.Errorf("failed to decode config: %w", err)
  133. }
  134. }
  135. applyDefaults(&cfg)
  136. return &cfg, nil
  137. }
  138. func applyDefaults(cfg *Config) {
  139. if cfg.Editor == "default" {
  140. cfg.Editor = os.Getenv("EDITOR")
  141. }
  142. if cfg.Status == "default" {
  143. cfg.Status = discord.UnknownStatus
  144. }
  145. if cfg.ImageViewer == "default" {
  146. cfg.ImageViewer = ""
  147. }
  148. if strings.HasPrefix(cfg.ImageSaveDir, "~/") {
  149. if home, err := os.UserHomeDir(); err == nil {
  150. cfg.ImageSaveDir = filepath.Join(home, cfg.ImageSaveDir[2:])
  151. }
  152. }
  153. if cfg.DateSeparator.Format == "" {
  154. cfg.DateSeparator.Format = "January 2, 2006"
  155. }
  156. if cfg.DateSeparator.Character == "" {
  157. cfg.DateSeparator.Character = "─"
  158. return
  159. }
  160. r, _ := utf8.DecodeRuneInString(cfg.DateSeparator.Character)
  161. if r == utf8.RuneError {
  162. cfg.DateSeparator.Character = "─"
  163. return
  164. }
  165. cfg.DateSeparator.Character = string(r)
  166. }