config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package util
  2. import (
  3. "encoding/json"
  4. "os"
  5. "github.com/rivo/tview"
  6. )
  7. type keybindings struct {
  8. GuildsTreeViewFocus string
  9. MessagesTextViewFocus string
  10. MessagesTextViewSelectPrevious string
  11. MessagesTextViewSelectNext string
  12. MessagesTextViewSelectFirst string
  13. MessagesTextViewSelectLast string
  14. MessagesTextViewReplySelected string
  15. MessagesTextViewMentionReplySelected string
  16. MessageInputFieldFocus string
  17. }
  18. // Config consists of fields that can be customized by the user using the configuration file.
  19. type Config struct {
  20. Token string
  21. Mouse bool
  22. Notifications bool
  23. UserAgent string
  24. GetMessagesLimit int
  25. Theme tview.Theme
  26. Keybindings keybindings
  27. }
  28. // LoadConfig createsa new configuration file (if does not exist) and returns the default configuration or reads the existing configuration file from the default path.
  29. func LoadConfig() *Config {
  30. u, err := os.UserHomeDir()
  31. if err != nil {
  32. panic(err)
  33. }
  34. configPath := u + "/.config/discordo/config.json"
  35. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  36. f, err := os.Create(configPath)
  37. if err != nil {
  38. panic(err)
  39. }
  40. defer f.Close()
  41. c := Config{
  42. Mouse: true,
  43. Notifications: true,
  44. UserAgent: "" +
  45. "Mozilla/5.0 (X11; Linux x86_64) " +
  46. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  47. "Chrome/92.0.4515.131 Safari/537.36",
  48. GetMessagesLimit: 50,
  49. Theme: tview.Styles,
  50. Keybindings: keybindings{
  51. GuildsTreeViewFocus: "Alt+Rune[1]",
  52. MessagesTextViewFocus: "Alt+Rune[2]",
  53. MessagesTextViewSelectPrevious: "Up",
  54. MessagesTextViewSelectNext: "Down",
  55. MessagesTextViewSelectFirst: "Home",
  56. MessagesTextViewSelectLast: "End",
  57. MessagesTextViewReplySelected: "r",
  58. MessagesTextViewMentionReplySelected: "R",
  59. MessageInputFieldFocus: "Alt+Rune[3]",
  60. },
  61. }
  62. d, err := json.MarshalIndent(c, "", "\t")
  63. if err != nil {
  64. panic(err)
  65. }
  66. _, err = f.Write(d)
  67. if err != nil {
  68. panic(err)
  69. }
  70. f.Sync()
  71. return &c
  72. }
  73. d, err := os.ReadFile(configPath)
  74. if err != nil {
  75. panic(err)
  76. }
  77. var c Config
  78. if err = json.Unmarshal(d, &c); err != nil {
  79. panic(err)
  80. }
  81. return &c
  82. }