mod.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package config
  2. import (
  3. "os"
  4. "github.com/BurntSushi/toml"
  5. )
  6. type GeneralConfig struct {
  7. UserAgent string `json:"userAgent"`
  8. FetchMessagesLimit int `json:"fetchMessagesLimit"`
  9. Mouse bool `json:"mouse"`
  10. Notifications bool `json:"notifications"`
  11. Timestamps bool `json:"timestamps"`
  12. }
  13. type KeybindingsConfig struct {
  14. FocusGuildsList string `json:"focusGuildsList"`
  15. FocusChannelsTreeView string `json:"focusChannelsTreeView"`
  16. FocusMessagesTextView string `json:"focusMessagesTextView"`
  17. FocusMessageInputField string `json:"focusMessageInputField"`
  18. FocusMessageActionsList string `json:"focusMessageActionsList"`
  19. SelectPreviousMessage string `json:"selectPreviousMessage"`
  20. SelectNextMessage string `json:"selectNextMessage"`
  21. SelectFirstMessage string `json:"selectFirstMessage"`
  22. SelectLastMessage string `json:"selectLastMessage"`
  23. }
  24. type Config struct {
  25. Keybindings KeybindingsConfig `json:"keybindings"`
  26. General GeneralConfig `json:"general"`
  27. }
  28. func Load() Config {
  29. configPath, err := os.UserConfigDir()
  30. if err != nil {
  31. panic(err)
  32. }
  33. configPath += "/discordo.toml"
  34. c := Config{}
  35. // If the configuration file does not exist, create and write the default configuration to the file.
  36. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  37. f, err := os.Create(configPath)
  38. if err != nil {
  39. panic(err)
  40. }
  41. defer f.Close()
  42. c = newDefaultConfig()
  43. err = toml.NewEncoder(f).Encode(c)
  44. if err != nil {
  45. panic(err)
  46. }
  47. } else {
  48. _, err = toml.DecodeFile(configPath, &c)
  49. if err != nil {
  50. panic(err)
  51. }
  52. }
  53. return c
  54. }
  55. func newDefaultConfig() Config {
  56. return Config{
  57. General: GeneralConfig{
  58. UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
  59. FetchMessagesLimit: 50,
  60. Mouse: true,
  61. Notifications: true,
  62. Timestamps: false,
  63. },
  64. Keybindings: KeybindingsConfig{
  65. FocusGuildsList: "Alt+Rune[g]",
  66. FocusChannelsTreeView: "Alt+Rune[t]",
  67. FocusMessagesTextView: "Alt+Rune[m]",
  68. FocusMessageInputField: "Alt+Rune[i]",
  69. FocusMessageActionsList: "Alt+Rune[a]",
  70. SelectPreviousMessage: "Up",
  71. SelectNextMessage: "Down",
  72. SelectFirstMessage: "Home",
  73. SelectLastMessage: "End",
  74. },
  75. }
  76. }