config.go 3.8 KB

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