config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/config.toml"
  72. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  73. err = os.MkdirAll(u+"/.config/discordo", 0700)
  74. if err != nil {
  75. panic(err)
  76. }
  77. f, err := os.Create(configPath)
  78. if err != nil {
  79. panic(err)
  80. }
  81. defer f.Close()
  82. c := config{
  83. Mouse: true,
  84. Notifications: true,
  85. UserAgent: "" +
  86. "Mozilla/5.0 (X11; Linux x86_64) " +
  87. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  88. "Chrome/92.0.4515.131 Safari/537.36",
  89. GetMessagesLimit: 50,
  90. Theme: theme{
  91. Border: "white",
  92. Title: "white",
  93. Graphics: "white",
  94. Background: themeBackground{
  95. Primitive: "black",
  96. Contrast: "blue",
  97. MoreContrast: "green",
  98. },
  99. Text: themeText{
  100. Primary: "white",
  101. Secondary: "yellow",
  102. Tertiary: "green",
  103. Inverse: "blue",
  104. ContrastSecondary: "darkcyan",
  105. },
  106. },
  107. Keybindings: keybindings{
  108. ChannelsTree: keybindingsChannelsTree{
  109. Focus: "Alt+Rune[1]",
  110. },
  111. MessagesTextView: keybindingsMessagesTextView{
  112. Focus: "Alt+Rune[2]",
  113. SelectPrevious: "Up",
  114. SelectNext: "Down",
  115. SelectFirst: "Home",
  116. SelectLast: "End",
  117. Reply: "Rune[r]",
  118. ReplyMention: "Rune[R]",
  119. },
  120. MessageInputField: keybindingsMessageInputField{
  121. Focus: "Alt+Rune[3]",
  122. },
  123. },
  124. }
  125. err = toml.NewEncoder(f).Encode(c)
  126. if err != nil {
  127. panic(err)
  128. }
  129. return &c
  130. }
  131. var c config
  132. _, err = toml.DecodeFile(configPath, &c)
  133. if err != nil {
  134. panic(err)
  135. }
  136. return &c
  137. }