config.go 3.2 KB

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