config.go 2.8 KB

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