config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. HorizontalFocus rune
  39. VerticalFocus rune
  40. TopLeftFocus rune
  41. TopRightFocus rune
  42. BottomLeftFocus rune
  43. BottomRightFocus rune
  44. }
  45. type Config struct {
  46. Token string `toml:"token"`
  47. UserAgent string `toml:"user_agent"`
  48. Mouse bool `toml:"mouse"`
  49. Notifications bool `toml:"notifications"`
  50. GetMessagesLimit int `toml:"get_messages_limit"`
  51. Theme theme `toml:"theme"`
  52. Keybindings keybindings `toml:"keybindings"`
  53. Borders borders `toml:"borders"`
  54. }
  55. // NewConfig loads the configuration file, if the configuration file exists or creates a new one if not, and returns it.
  56. func NewConfig() *Config {
  57. configPath, err := os.UserConfigDir()
  58. if err != nil {
  59. panic(err)
  60. }
  61. configPath += "/discordo.toml"
  62. var c Config
  63. if _, err = os.Stat(configPath); os.IsNotExist(err) {
  64. f, err := os.Create(configPath)
  65. if err != nil {
  66. panic(err)
  67. }
  68. defer f.Close()
  69. c = Config{
  70. Mouse: true,
  71. Notifications: true,
  72. UserAgent: userAgent,
  73. GetMessagesLimit: 50,
  74. Borders: borders{
  75. Horizontal: 0,
  76. Vertical: 0,
  77. TopLeft: 0,
  78. TopRight: 0,
  79. BottomLeft: 0,
  80. BottomRight: 0,
  81. HorizontalFocus: tview.BoxDrawingsLightHorizontal,
  82. VerticalFocus: tview.BoxDrawingsLightVertical,
  83. TopLeftFocus: tview.BoxDrawingsLightDownAndRight,
  84. TopRightFocus: tview.BoxDrawingsLightDownAndLeft,
  85. BottomLeftFocus: tview.BoxDrawingsLightUpAndRight,
  86. BottomRightFocus: tview.BoxDrawingsLightUpAndLeft,
  87. },
  88. Theme: theme{
  89. Background: "black",
  90. Border: "white",
  91. Title: "white",
  92. Graphics: "white",
  93. Text: "white",
  94. },
  95. Keybindings: keybindings{
  96. FocusChannelsTree: []string{"Alt+Left"},
  97. FocusMessagesView: []string{"Alt+Right"},
  98. FocusMessageInputField: []string{"Alt+Down"},
  99. SelectPreviousMessage: []string{"Up"},
  100. SelectNextMessage: []string{"Down"},
  101. SelectFirstMessage: []string{"Home"},
  102. SelectLastMessage: []string{"End"},
  103. ReplySelectedMessage: []string{"Rune[r]"},
  104. MentionReplySelectedMessage: []string{"Rune[R]"},
  105. CopySelectedMessage: []string{"Rune[c]"},
  106. SelectMessageReference: []string{"Rune[m]"},
  107. },
  108. }
  109. err = toml.NewEncoder(f).Encode(c)
  110. if err != nil {
  111. panic(err)
  112. }
  113. } else {
  114. _, err = toml.DecodeFile(configPath, &c)
  115. if err != nil {
  116. panic(err)
  117. }
  118. }
  119. return &c
  120. }