config.go 2.9 KB

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