config.go 3.6 KB

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