config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package config
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/BurntSushi/toml"
  8. )
  9. type IdentifyConfig struct {
  10. UserAgent string `toml:"user_agent"`
  11. Os string `toml:"os"`
  12. Browser string `toml:"browser"`
  13. }
  14. type KeysConfig struct {
  15. ToggleGuildsList string `toml:"toggle_guilds_list"`
  16. ToggleChannelsTreeView string `toml:"toggle_channels_tree_view"`
  17. ToggleMessagesTextView string `toml:"toggle_messages_text_view"`
  18. ToggleMessageInputField string `toml:"toggle_message_input_field"`
  19. OpenMessageActionsList string `toml:"open_message_actions_list"`
  20. OpenExternalEditor string `toml:"open_external_editor"`
  21. SelectPreviousMessage string `toml:"select_previous_message"`
  22. SelectNextMessage string `toml:"select_next_message"`
  23. SelectFirstMessage string `toml:"select_first_message"`
  24. SelectLastMessage string `toml:"select_last_message"`
  25. }
  26. type ThemeConfig struct {
  27. Background string `toml:"background"`
  28. Border string `toml:"border"`
  29. Title string `toml:"title"`
  30. }
  31. type Config struct {
  32. Mouse bool `toml:"mouse"`
  33. Timestamps bool `toml:"timestamps"`
  34. MessagesLimit int `toml:"messages_limit"`
  35. AttachmentDownloadsDir string `toml:"attachment_downloads_dir"`
  36. Identify IdentifyConfig `toml:"identify"`
  37. Theme ThemeConfig `toml:"theme"`
  38. Keys KeysConfig `toml:"keys"`
  39. }
  40. func New() *Config {
  41. return &Config{
  42. Mouse: true,
  43. Timestamps: false,
  44. MessagesLimit: 50,
  45. AttachmentDownloadsDir: UserDownloadsDir(),
  46. Identify: IdentifyConfig{
  47. UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
  48. Os: "Linux",
  49. Browser: "Chrome",
  50. },
  51. Theme: ThemeConfig{
  52. Background: "black",
  53. Border: "white",
  54. Title: "white",
  55. },
  56. Keys: KeysConfig{
  57. ToggleGuildsList: "Rune[g]",
  58. ToggleChannelsTreeView: "Rune[c]",
  59. ToggleMessagesTextView: "Rune[m]",
  60. ToggleMessageInputField: "Rune[i]",
  61. OpenMessageActionsList: "Rune[a]",
  62. OpenExternalEditor: "Ctrl+E",
  63. SelectPreviousMessage: "Up",
  64. SelectNextMessage: "Down",
  65. SelectFirstMessage: "Home",
  66. SelectLastMessage: "End",
  67. },
  68. }
  69. }
  70. func (c *Config) Load(path string) error {
  71. // Create directories that do not exist and are mentioned in the path recursively.
  72. err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
  73. if err != nil {
  74. return err
  75. }
  76. // If the configuration file does not exist already, create a new file; otherwwise, open the existing file with read-write flag.
  77. f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm)
  78. if err != nil {
  79. return err
  80. }
  81. defer f.Close()
  82. fi, err := f.Stat()
  83. if err != nil {
  84. return err
  85. }
  86. // If the file is empty (the size of the file is zero), write the dedfault configuration to the file.
  87. if fi.Size() == 0 {
  88. return toml.NewEncoder(f).Encode(c)
  89. }
  90. _, err = toml.NewDecoder(f).Decode(&c)
  91. return err
  92. }
  93. func DefaultPath() string {
  94. path, err := os.UserConfigDir()
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. path += "/discordo/config.toml"
  99. return path
  100. }
  101. func UserDownloadsDir() string {
  102. // We try to set the download folder location to the default Downloads folder
  103. var dlloc string
  104. if runtime.GOOS == "windows" {
  105. h, _ := os.UserHomeDir()
  106. dlloc = h + "\\Downloads"
  107. } else if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
  108. h, _ := os.UserHomeDir()
  109. dlloc = h + "/Downloads"
  110. } else {
  111. dlloc = os.TempDir() // Very lame fallback, I know
  112. }
  113. return dlloc
  114. }