config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "os"
  4. "github.com/BurntSushi/toml"
  5. )
  6. var conf *config
  7. type keybindingsChannelsTree struct {
  8. Focus string `toml:"focus"`
  9. }
  10. type keybindingsMessagesTextView struct {
  11. Focus string `toml:"focus"`
  12. SelectPrevious string `toml:"select_previous"`
  13. SelectNext string `toml:"select_next"`
  14. SelectFirst string `toml:"select_first"`
  15. SelectLast string `toml:"select_last"`
  16. Reply string `toml:"reply"`
  17. ReplyMention string `toml:"reply_mention"`
  18. }
  19. type keybindingsMessageInputField struct {
  20. Focus string `toml:"focus"`
  21. }
  22. type keybindings struct {
  23. ChannelsTree keybindingsChannelsTree `toml:"channels_tree"`
  24. MessagesTextView keybindingsMessagesTextView `toml:"messages_textview"`
  25. MessageInputField keybindingsMessageInputField `toml:"message_inputfield"`
  26. }
  27. type themeBackground struct {
  28. // Main background color for primitives.
  29. Primitive string `toml:"primitive"`
  30. // Background color for contrasting elements.
  31. Contrast string `toml:"contrast"`
  32. // Background color for even more contrasting elements.
  33. MoreContrast string `toml:"more_contrast"`
  34. }
  35. type themeText struct {
  36. // Primary text.
  37. Primary string `toml:"primary"`
  38. // Secondary text (e.g. labels).
  39. Secondary string `toml:"secondary"`
  40. // Tertiary text (e.g. subtitles, notes).
  41. Tertiary string `toml:"tertiary"`
  42. // Text on primary-colored backgrounds.
  43. Inverse string `toml:"inverse"`
  44. // Secondary text on ContrastBackgroundColor-colored backgrounds.
  45. ContrastSecondary string `toml:"contrast_secondary"`
  46. }
  47. type theme struct {
  48. // Box borders.
  49. Border string `toml:"border"`
  50. // Box titles.
  51. Title string `toml:"title"`
  52. // Graphics.
  53. Graphics string `toml:"graphics"`
  54. Background themeBackground `toml:"background"`
  55. Text themeText `toml:"text"`
  56. }
  57. type config struct {
  58. Token string `toml:"token"`
  59. Mouse bool `toml:"mouse"`
  60. Notifications bool `toml:"notifications"`
  61. UserAgent string `toml:"user_agent"`
  62. GetMessagesLimit int `toml:"get_messages_limit"`
  63. Theme theme `toml:"theme"`
  64. Keybindings keybindings `toml:"keybindings"`
  65. }
  66. func loadConfig() *config {
  67. u, err := os.UserHomeDir()
  68. if err != nil {
  69. panic(err)
  70. }
  71. configPath := u + "/.config/discordo.toml"
  72. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  73. f, err := os.Create(configPath)
  74. if err != nil {
  75. panic(err)
  76. }
  77. defer f.Close()
  78. c := config{
  79. Mouse: true,
  80. Notifications: true,
  81. UserAgent: "" +
  82. "Mozilla/5.0 (X11; Linux x86_64) " +
  83. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  84. "Chrome/92.0.4515.131 Safari/537.36",
  85. GetMessagesLimit: 50,
  86. Theme: theme{
  87. Border: "white",
  88. Title: "white",
  89. Graphics: "white",
  90. Background: themeBackground{
  91. Primitive: "black",
  92. Contrast: "blue",
  93. MoreContrast: "green",
  94. },
  95. Text: themeText{
  96. Primary: "white",
  97. Secondary: "yellow",
  98. Tertiary: "green",
  99. Inverse: "blue",
  100. ContrastSecondary: "darkcyan",
  101. },
  102. },
  103. Keybindings: keybindings{
  104. ChannelsTree: keybindingsChannelsTree{
  105. Focus: "Alt+Rune[1]",
  106. },
  107. MessagesTextView: keybindingsMessagesTextView{
  108. Focus: "Alt+Rune[2]",
  109. SelectPrevious: "Up",
  110. SelectNext: "Down",
  111. SelectFirst: "Home",
  112. SelectLast: "End",
  113. Reply: "Rune[r]",
  114. ReplyMention: "Rune[R]",
  115. },
  116. MessageInputField: keybindingsMessageInputField{
  117. Focus: "Alt+Rune[3]",
  118. },
  119. },
  120. }
  121. err = toml.NewEncoder(f).Encode(c)
  122. if err != nil {
  123. panic(err)
  124. }
  125. return &c
  126. }
  127. var c config
  128. _, err = toml.DecodeFile(configPath, &c)
  129. if err != nil {
  130. panic(err)
  131. }
  132. return &c
  133. }