mod.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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 New() *Config {
  29. return &Config{
  30. General: GeneralConfig{
  31. UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
  32. FetchMessagesLimit: 50,
  33. Mouse: true,
  34. Notifications: true,
  35. Timestamps: false,
  36. },
  37. Keybindings: KeybindingsConfig{
  38. FocusGuildsList: "Alt+Rune[g]",
  39. FocusChannelsTreeView: "Alt+Rune[t]",
  40. FocusMessagesTextView: "Alt+Rune[m]",
  41. FocusMessageInputField: "Alt+Rune[i]",
  42. FocusMessageActionsList: "Alt+Rune[a]",
  43. SelectPreviousMessage: "Up",
  44. SelectNextMessage: "Down",
  45. SelectFirstMessage: "Home",
  46. SelectLastMessage: "End",
  47. },
  48. }
  49. }
  50. func (c *Config) Load() {
  51. configPath, err := os.UserConfigDir()
  52. if err != nil {
  53. panic(err)
  54. }
  55. configPath += "/discordo.json"
  56. f, err := os.OpenFile(configPath, os.O_CREATE|os.O_RDWR, os.ModePerm)
  57. if err != nil {
  58. panic(err)
  59. }
  60. fi, err := f.Stat()
  61. if err != nil {
  62. panic(err)
  63. }
  64. // If the size of the file is zero (the file is empty), write the default configuration to the file.
  65. if fi.Size() == 0 {
  66. e := json.NewEncoder(f)
  67. e.SetIndent("", "\t")
  68. c = New()
  69. err = e.Encode(c)
  70. if err != nil {
  71. panic(err)
  72. }
  73. } else {
  74. err = json.NewDecoder(f).Decode(c)
  75. if err != nil {
  76. panic(err)
  77. }
  78. }
  79. }