config.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. type Config struct {
  19. Token string
  20. Mouse bool
  21. Notifications bool
  22. UserAgent string
  23. GetMessagesLimit int
  24. Theme tview.Theme
  25. Keybindings keybindings
  26. }
  27. func LoadConfig() *Config {
  28. u, err := os.UserHomeDir()
  29. if err != nil {
  30. panic(err)
  31. }
  32. configPath := u + "/.config/discordo/config.json"
  33. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  34. f, err := os.Create(configPath)
  35. if err != nil {
  36. panic(err)
  37. }
  38. c := Config{
  39. Mouse: true,
  40. Notifications: true,
  41. UserAgent: "" +
  42. "Mozilla/5.0 (X11; Linux x86_64) " +
  43. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  44. "Chrome/92.0.4515.131 Safari/537.36",
  45. GetMessagesLimit: 50,
  46. Theme: tview.Styles,
  47. Keybindings: keybindings{
  48. GuildsTreeViewFocus: "Alt+Rune[1]",
  49. MessagesTextViewFocus: "Alt+Rune[2]",
  50. MessagesTextViewSelectPrevious: "Up",
  51. MessagesTextViewSelectNext: "Down",
  52. MessagesTextViewSelectFirst: "Home",
  53. MessagesTextViewSelectLast: "End",
  54. MessagesTextViewReplySelected: "r",
  55. MessagesTextViewMentionReplySelected: "R",
  56. MessageInputFieldFocus: "Alt+Rune[3]",
  57. },
  58. }
  59. d, err := json.MarshalIndent(c, "", "\t")
  60. if err != nil {
  61. panic(err)
  62. }
  63. _, err = f.Write(d)
  64. if err != nil {
  65. panic(err)
  66. }
  67. f.Sync()
  68. }
  69. d, err := os.ReadFile(configPath)
  70. if err != nil {
  71. panic(err)
  72. }
  73. var c Config
  74. if err = json.Unmarshal(d, &c); err != nil {
  75. panic(err)
  76. }
  77. return &c
  78. }