mod.go 2.6 KB

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