config.go 2.1 KB

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