config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Path string // set programmatically, not from TOML
  73. AutoFocus bool `toml:"auto_focus"`
  74. Mouse bool `toml:"mouse"`
  75. Editor string `toml:"editor"`
  76. Status discord.Status `toml:"status"`
  77. HideBlockedUsers bool `toml:"hide_blocked_users"`
  78. ShowAttachmentLinks bool `toml:"show_attachment_links"`
  79. ImageViewer string `toml:"image_viewer"`
  80. ImageSaveDir string `toml:"image_save_dir"`
  81. // Use 0 to disable
  82. AutocompleteLimit uint8 `toml:"autocomplete_limit"`
  83. MessagesLimit uint8 `toml:"messages_limit"`
  84. Markdown MarkdownConfig `toml:"markdown"`
  85. Help HelpConfig `toml:"help"`
  86. Picker PickerConfig `toml:"picker"`
  87. Timestamps Timestamps `toml:"timestamps"`
  88. DateSeparator DateSeparator `toml:"date_separator"`
  89. Notifications Notifications `toml:"notifications"`
  90. TypingIndicator TypingIndicator `toml:"typing_indicator"`
  91. Sidebar SidebarConfig `toml:"sidebar"`
  92. Icons Icons `toml:"icons"`
  93. Keybinds Keybinds `toml:"keybinds"`
  94. Theme Theme `toml:"theme"`
  95. }
  96. )
  97. //go:embed config.toml
  98. var defaultCfg []byte
  99. func DefaultPath() string {
  100. path, err := os.UserConfigDir()
  101. if err != nil {
  102. slog.Info(
  103. "user config dir cannot be determined; falling back to the current dir",
  104. "err", err,
  105. )
  106. path = "."
  107. }
  108. return filepath.Join(path, consts.Name, fileName)
  109. }
  110. // Load reads the configuration file and parses it.
  111. func Load(path string) (*Config, error) {
  112. cfg := Config{
  113. Keybinds: defaultKeybinds(),
  114. }
  115. if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
  116. return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
  117. }
  118. file, err := os.Open(path)
  119. if os.IsNotExist(err) {
  120. slog.Info(
  121. "config file does not exist, falling back to the default config",
  122. "path",
  123. path,
  124. "err",
  125. err,
  126. )
  127. } else {
  128. if err != nil {
  129. return nil, fmt.Errorf("failed to open config file: %w", err)
  130. }
  131. defer file.Close()
  132. if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
  133. return nil, fmt.Errorf("failed to decode config: %w", err)
  134. }
  135. }
  136. cfg.Path = path
  137. applyDefaults(&cfg)
  138. return &cfg, nil
  139. }
  140. func applyDefaults(cfg *Config) {
  141. if cfg.Editor == "default" {
  142. if editor := os.Getenv("EDITOR"); editor != "" {
  143. cfg.Editor = editor
  144. } else {
  145. cfg.Editor = "vim"
  146. }
  147. }
  148. if cfg.Status == "default" {
  149. cfg.Status = discord.UnknownStatus
  150. }
  151. if cfg.ImageViewer == "default" {
  152. cfg.ImageViewer = ""
  153. }
  154. if strings.HasPrefix(cfg.ImageSaveDir, "~/") {
  155. if home, err := os.UserHomeDir(); err == nil {
  156. cfg.ImageSaveDir = filepath.Join(home, cfg.ImageSaveDir[2:])
  157. }
  158. }
  159. if cfg.DateSeparator.Format == "" {
  160. cfg.DateSeparator.Format = "January 2, 2006"
  161. }
  162. if cfg.DateSeparator.Character == "" {
  163. cfg.DateSeparator.Character = "─"
  164. return
  165. }
  166. r, _ := utf8.DecodeRuneInString(cfg.DateSeparator.Character)
  167. if r == utf8.RuneError {
  168. cfg.DateSeparator.Character = "─"
  169. return
  170. }
  171. cfg.DateSeparator.Character = string(r)
  172. }