config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package main
  2. import (
  3. "os"
  4. "github.com/BurntSushi/toml"
  5. "github.com/rivo/tview"
  6. )
  7. const userAgent = "" +
  8. "Mozilla/5.0 (X11; Linux x86_64) " +
  9. "AppleWebKit/537.36 (KHTML, like Gecko) " +
  10. "Chrome/92.0.4515.131 Safari/537.36"
  11. type keybindingsChannelsTree struct {
  12. Focus string
  13. }
  14. type keybindingsMessagesView struct {
  15. Focus string
  16. SelectPrevious string
  17. SelectNext string
  18. SelectFirst string
  19. SelectLast string
  20. Reply string
  21. ReplyMention string
  22. }
  23. type keybindingsMessageInputField struct {
  24. Focus string
  25. }
  26. type keybindings struct {
  27. ChannelsTree keybindingsChannelsTree
  28. MessagesView keybindingsMessagesView
  29. MessageInputField keybindingsMessageInputField
  30. }
  31. type themeBackground struct {
  32. // Main background color for primitives.
  33. Primitive string
  34. // Background color for contrasting elements.
  35. Contrast string
  36. // Background color for even more contrasting elements.
  37. MoreContrast string
  38. }
  39. type themeText struct {
  40. // Primary text.
  41. Primary string
  42. // Secondary text (e.g. labels).
  43. Secondary string
  44. // Tertiary text (e.g. subtitles, notes).
  45. Tertiary string
  46. // Text on primary-colored backgrounds.
  47. Inverse string
  48. // Secondary text on ContrastBackgroundColor-colored backgrounds.
  49. ContrastSecondary string
  50. }
  51. type theme struct {
  52. // Box borders.
  53. Border string
  54. // Box titles.
  55. Title string
  56. // Graphics.
  57. Graphics string
  58. Background themeBackground
  59. Text themeText
  60. }
  61. type borders struct {
  62. Horizontal rune
  63. Vertical rune
  64. TopLeft rune
  65. TopRight rune
  66. BottomLeft rune
  67. BottomRight rune
  68. LeftT rune
  69. RightT rune
  70. TopT rune
  71. BottomT rune
  72. Cross rune
  73. HorizontalFocus rune
  74. VerticalFocus rune
  75. TopLeftFocus rune
  76. TopRightFocus rune
  77. BottomLeftFocus rune
  78. BottomRightFocus rune
  79. }
  80. type config struct {
  81. Token string
  82. UserAgent string
  83. Mouse bool
  84. Notifications bool
  85. GetMessagesLimit int
  86. Theme theme
  87. Keybindings keybindings
  88. Borders borders
  89. }
  90. func loadConfig() *config {
  91. configPath, err := os.UserConfigDir()
  92. if err != nil {
  93. panic(err)
  94. }
  95. configPath += "/discordo.toml"
  96. var c config
  97. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  98. f, err := os.Create(configPath)
  99. if err != nil {
  100. panic(err)
  101. }
  102. defer f.Close()
  103. c.Mouse = true
  104. c.Notifications = true
  105. c.UserAgent = userAgent
  106. c.GetMessagesLimit = 50
  107. c.Theme = theme{
  108. Border: "white",
  109. Title: "white",
  110. Graphics: "white",
  111. Background: themeBackground{
  112. Primitive: "black",
  113. Contrast: "blue",
  114. MoreContrast: "green",
  115. },
  116. Text: themeText{
  117. Primary: "white",
  118. Secondary: "yellow",
  119. Tertiary: "green",
  120. Inverse: "blue",
  121. ContrastSecondary: "darkcyan",
  122. },
  123. }
  124. c.Keybindings = keybindings{
  125. ChannelsTree: keybindingsChannelsTree{
  126. Focus: "Alt+Rune[1]",
  127. },
  128. MessagesView: keybindingsMessagesView{
  129. Focus: "Alt+Rune[2]",
  130. SelectPrevious: "Up",
  131. SelectNext: "Down",
  132. SelectFirst: "Home",
  133. SelectLast: "End",
  134. Reply: "Rune[r]",
  135. ReplyMention: "Rune[R]",
  136. },
  137. MessageInputField: keybindingsMessageInputField{
  138. Focus: "Alt+Rune[3]",
  139. },
  140. }
  141. c.Borders = tview.Borders
  142. err = toml.NewEncoder(f).Encode(c)
  143. if err != nil {
  144. panic(err)
  145. }
  146. } else {
  147. _, err = toml.DecodeFile(configPath, &c)
  148. if err != nil {
  149. panic(err)
  150. }
  151. }
  152. return &c
  153. }