mod.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package config
  2. import (
  3. "encoding/json"
  4. "os"
  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. }
  12. type KeybindingsConfig struct {
  13. FocusGuildsList []string `json:"focusGuildsList"`
  14. FocusChannelsTreeView []string `json:"focusChannelsTreeView"`
  15. FocusMessagesTextView []string `json:"focusMessagesTextView"`
  16. FocusMessageInputField []string `json:"focusMessageInputField"`
  17. FocusMessageActionsList []string `json:"focusMessageActionsList"`
  18. SelectPreviousMessage []string `json:"selectPreviousMessage"`
  19. SelectNextMessage []string `json:"selectNextMessage"`
  20. SelectFirstMessage []string `json:"selectFirstMessage"`
  21. SelectLastMessage []string `json:"selectLastMessage"`
  22. }
  23. type Config struct {
  24. Keybindings KeybindingsConfig `json:"keybindings"`
  25. General GeneralConfig `json:"general"`
  26. }
  27. func New() *Config {
  28. return &Config{
  29. General: GeneralConfig{
  30. UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
  31. FetchMessagesLimit: 50,
  32. Mouse: true,
  33. Notifications: true,
  34. },
  35. Keybindings: KeybindingsConfig{
  36. FocusGuildsList: []string{"Alt+Rune[g]"},
  37. FocusChannelsTreeView: []string{"Alt+Rune[t]"},
  38. FocusMessagesTextView: []string{"Alt+Rune[m]"},
  39. FocusMessageInputField: []string{"Alt+Rune[i]"},
  40. FocusMessageActionsList: []string{"Alt+Rune[a]"},
  41. SelectPreviousMessage: []string{"Up"},
  42. SelectNextMessage: []string{"Down"},
  43. SelectFirstMessage: []string{"Home"},
  44. SelectLastMessage: []string{"End"},
  45. },
  46. }
  47. }
  48. func (c *Config) Load() {
  49. configPath, err := os.UserConfigDir()
  50. if err != nil {
  51. panic(err)
  52. }
  53. configPath += "/discordo.json"
  54. f, err := os.OpenFile(configPath, os.O_CREATE|os.O_RDWR, os.ModePerm)
  55. if err != nil {
  56. panic(err)
  57. }
  58. fi, err := f.Stat()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // If the size of the file is zero (the file is empty), write the default configuration to the file.
  63. if fi.Size() == 0 {
  64. e := json.NewEncoder(f)
  65. e.SetIndent("", "\t")
  66. c = New()
  67. err = e.Encode(c)
  68. if err != nil {
  69. panic(err)
  70. }
  71. } else {
  72. err = json.NewDecoder(f).Decode(c)
  73. if err != nil {
  74. panic(err)
  75. }
  76. }
  77. }