config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. c := Config{
  41. Mouse: true,
  42. Notifications: true,
  43. UserAgent: "" +
  44. "Mozilla/5.0 (X11; Linux x86_64) " +
  45. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  46. "Chrome/92.0.4515.131 Safari/537.36",
  47. GetMessagesLimit: 50,
  48. Theme: tview.Styles,
  49. Keybindings: keybindings{
  50. GuildsTreeViewFocus: "Alt+Rune[1]",
  51. MessagesTextViewFocus: "Alt+Rune[2]",
  52. MessagesTextViewSelectPrevious: "Up",
  53. MessagesTextViewSelectNext: "Down",
  54. MessagesTextViewSelectFirst: "Home",
  55. MessagesTextViewSelectLast: "End",
  56. MessagesTextViewReplySelected: "r",
  57. MessagesTextViewMentionReplySelected: "R",
  58. MessageInputFieldFocus: "Alt+Rune[3]",
  59. },
  60. }
  61. d, err := json.MarshalIndent(c, "", "\t")
  62. if err != nil {
  63. panic(err)
  64. }
  65. _, err = f.Write(d)
  66. if err != nil {
  67. panic(err)
  68. }
  69. f.Sync()
  70. }
  71. d, err := os.ReadFile(configPath)
  72. if err != nil {
  73. panic(err)
  74. }
  75. var c Config
  76. if err = json.Unmarshal(d, &c); err != nil {
  77. panic(err)
  78. }
  79. return &c
  80. }