mod.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. OpenEditor string `json:"open_editor"`
  20. SelectPreviousMessage string `json:"selectPreviousMessage"`
  21. SelectNextMessage string `json:"selectNextMessage"`
  22. SelectFirstMessage string `json:"selectFirstMessage"`
  23. SelectLastMessage string `json:"selectLastMessage"`
  24. }
  25. type Config struct {
  26. Keybindings KeybindingsConfig `json:"keybindings"`
  27. General GeneralConfig `json:"general"`
  28. }
  29. func Load() Config {
  30. configPath, err := os.UserConfigDir()
  31. if err != nil {
  32. panic(err)
  33. }
  34. configPath += "/discordo.toml"
  35. c := Config{}
  36. // If the configuration file does not exist, create and write the default configuration to the file.
  37. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  38. f, err := os.Create(configPath)
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer f.Close()
  43. c = newDefaultConfig()
  44. err = toml.NewEncoder(f).Encode(c)
  45. if err != nil {
  46. panic(err)
  47. }
  48. } else {
  49. _, err = toml.DecodeFile(configPath, &c)
  50. if err != nil {
  51. panic(err)
  52. }
  53. }
  54. return c
  55. }
  56. func newDefaultConfig() Config {
  57. return Config{
  58. General: GeneralConfig{
  59. UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
  60. FetchMessagesLimit: 50,
  61. Mouse: true,
  62. Notifications: true,
  63. Timestamps: false,
  64. },
  65. Keybindings: KeybindingsConfig{
  66. FocusGuildsList: "Alt+Rune[g]",
  67. FocusChannelsTreeView: "Alt+Rune[t]",
  68. FocusMessagesTextView: "Alt+Rune[m]",
  69. FocusMessageInputField: "Alt+Rune[i]",
  70. FocusMessageActionsList: "Alt+Rune[a]",
  71. OpenEditor: "Alt+Rune[e]",
  72. SelectPreviousMessage: "Up",
  73. SelectNextMessage: "Down",
  74. SelectFirstMessage: "Home",
  75. SelectLastMessage: "End",
  76. },
  77. }
  78. }