config.go 3.7 KB

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