config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 keybindings struct {
  12. FocusChannelsTree string `toml:"focus_channels_tree"`
  13. FocusMessagesView string `toml:"focus_messages_view"`
  14. FocusMessageInputField string `toml:"focus_message_input_field"`
  15. SelectPreviousMessage string `toml:"select_previous_message"`
  16. SelectNextMessage string `toml:"select_next_message"`
  17. SelectFirstMessage string `toml:"select_first_message"`
  18. SelectLastMessage string `toml:"select_last_message"`
  19. ReplySelectedMessage string `toml:"reply_selected_message"`
  20. MentionReplySelectedMessage string `toml:"mention_reply_selected_message"`
  21. }
  22. type theme struct {
  23. Background string `toml:"background"`
  24. Border string `toml:"border"`
  25. Title string `toml:"title"`
  26. Graphics string `toml:"graphics"`
  27. Text string `toml:"text"`
  28. }
  29. type borders struct {
  30. Horizontal rune
  31. Vertical rune
  32. TopLeft rune
  33. TopRight rune
  34. BottomLeft rune
  35. BottomRight rune
  36. LeftT rune
  37. RightT rune
  38. TopT rune
  39. BottomT rune
  40. Cross rune
  41. HorizontalFocus rune
  42. VerticalFocus rune
  43. TopLeftFocus rune
  44. TopRightFocus rune
  45. BottomLeftFocus rune
  46. BottomRightFocus rune
  47. }
  48. type config struct {
  49. Token string `toml:"token"`
  50. UserAgent string `toml:"user_agent"`
  51. Mouse bool `toml:"mouse"`
  52. Notifications bool `toml:"notifications"`
  53. GetMessagesLimit int `toml:"get_messages_limit"`
  54. Theme theme `toml:"theme"`
  55. Keybindings keybindings `toml:"keybindings"`
  56. Borders borders `toml:"borders"`
  57. }
  58. func loadConfig() *config {
  59. configPath, err := os.UserConfigDir()
  60. if err != nil {
  61. panic(err)
  62. }
  63. configPath += "/discordo.toml"
  64. var c config
  65. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  66. f, err := os.Create(configPath)
  67. if err != nil {
  68. panic(err)
  69. }
  70. defer f.Close()
  71. c.Mouse = true
  72. c.Notifications = true
  73. c.UserAgent = userAgent
  74. c.GetMessagesLimit = 50
  75. c.Theme = theme{
  76. Background: "black",
  77. Border: "white",
  78. Title: "white",
  79. Graphics: "white",
  80. Text: "white",
  81. }
  82. c.Keybindings = keybindings{
  83. FocusChannelsTree: "Alt+Left",
  84. FocusMessagesView: "Alt+Right",
  85. FocusMessageInputField: "Alt+Down",
  86. SelectPreviousMessage: "Up",
  87. SelectNextMessage: "Down",
  88. SelectFirstMessage: "Home",
  89. SelectLastMessage: "End",
  90. ReplySelectedMessage: "Rune[r]",
  91. MentionReplySelectedMessage: "Rune[R]",
  92. }
  93. c.Borders = tview.Borders
  94. err = toml.NewEncoder(f).Encode(c)
  95. if err != nil {
  96. panic(err)
  97. }
  98. } else {
  99. _, err = toml.DecodeFile(configPath, &c)
  100. if err != nil {
  101. panic(err)
  102. }
  103. }
  104. return &c
  105. }