config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. var conf *config
  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 theme struct {
  19. PrimitiveBackgroundColor string // Main background color for primitives.
  20. ContrastBackgroundColor string // Background color for contrasting elements.
  21. MoreContrastBackgroundColor string // Background color for even more contrasting elements.
  22. BorderColor string // Box borders.
  23. TitleColor string // Box titles.
  24. GraphicsColor string // Graphics.
  25. PrimaryTextColor string // Primary text.
  26. SecondaryTextColor string // Secondary text (e.g. labels).
  27. TertiaryTextColor string // Tertiary text (e.g. subtitles, notes).
  28. InverseTextColor string // Text on primary-colored backgrounds.
  29. ContrastSecondaryTextColor string // Secondary text on ContrastBackgroundColor-colored backgrounds.
  30. }
  31. type config struct {
  32. Token string
  33. Mouse bool
  34. Notifications bool
  35. UserAgent string
  36. GetMessagesLimit int
  37. Theme theme
  38. Keybindings keybindings
  39. }
  40. func loadConfig() *config {
  41. u, err := os.UserHomeDir()
  42. if err != nil {
  43. panic(err)
  44. }
  45. configPath := u + "/.config/discordo/config.json"
  46. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  47. err = os.MkdirAll(u+"/.config/discordo", 0700)
  48. if err != nil {
  49. panic(err)
  50. }
  51. f, err := os.Create(configPath)
  52. if err != nil {
  53. panic(err)
  54. }
  55. defer f.Close()
  56. c := config{
  57. Mouse: true,
  58. Notifications: true,
  59. UserAgent: "" +
  60. "Mozilla/5.0 (X11; Linux x86_64) " +
  61. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  62. "Chrome/92.0.4515.131 Safari/537.36",
  63. GetMessagesLimit: 50,
  64. Theme: theme{
  65. PrimitiveBackgroundColor: "black",
  66. ContrastBackgroundColor: "blue",
  67. MoreContrastBackgroundColor: "green",
  68. BorderColor: "white",
  69. TitleColor: "white",
  70. GraphicsColor: "white",
  71. PrimaryTextColor: "white",
  72. SecondaryTextColor: "yellow",
  73. TertiaryTextColor: "green",
  74. InverseTextColor: "blue",
  75. ContrastSecondaryTextColor: "darkcyan",
  76. },
  77. Keybindings: keybindings{
  78. GuildsTreeViewFocus: "Alt+Rune[1]",
  79. MessagesTextViewFocus: "Alt+Rune[2]",
  80. MessagesTextViewSelectPrevious: "Up",
  81. MessagesTextViewSelectNext: "Down",
  82. MessagesTextViewSelectFirst: "Home",
  83. MessagesTextViewSelectLast: "End",
  84. MessagesTextViewReplySelected: "Rune[r]",
  85. MessagesTextViewMentionReplySelected: "Rune[R]",
  86. MessageInputFieldFocus: "Alt+Rune[3]",
  87. },
  88. }
  89. d, err := json.MarshalIndent(c, "", "\t")
  90. if err != nil {
  91. panic(err)
  92. }
  93. _, err = f.Write(d)
  94. if err != nil {
  95. panic(err)
  96. }
  97. f.Sync()
  98. return &c
  99. }
  100. d, err := os.ReadFile(configPath)
  101. if err != nil {
  102. panic(err)
  103. }
  104. var c config
  105. if err = json.Unmarshal(d, &c); err != nil {
  106. panic(err)
  107. }
  108. return &c
  109. }