mod.go 2.7 KB

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