| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package config
- import (
- "log"
- "os"
- "path/filepath"
- "runtime"
- "github.com/BurntSushi/toml"
- )
- type IdentifyConfig struct {
- UserAgent string `toml:"user_agent"`
- Os string `toml:"os"`
- Browser string `toml:"browser"`
- }
- type KeysConfig struct {
- ToggleGuildsTree string `toml:"toggle_guilds_tree"`
- ToggleChannelsTree string `toml:"toggle_channels_tree"`
- ToggleMessagesTextView string `toml:"toggle_messages_text_view"`
- ToggleMessageInput string `toml:"toggle_message_input"`
- OpenMessageActionsList string `toml:"open_message_actions_list"`
- OpenExternalEditor string `toml:"open_external_editor"`
- SelectPreviousMessage string `toml:"select_previous_message"`
- SelectNextMessage string `toml:"select_next_message"`
- SelectFirstMessage string `toml:"select_first_message"`
- SelectLastMessage string `toml:"select_last_message"`
- }
- type ThemeConfig struct {
- Background string `toml:"background"`
- Border string `toml:"border"`
- Title string `toml:"title"`
- }
- type Config struct {
- Mouse bool `toml:"mouse"`
- Timestamps bool `toml:"timestamps"`
- MessagesLimit uint `toml:"messages_limit"`
- Timezone string `toml:"timezone"`
- AttachmentDownloadsDir string `toml:"attachment_downloads_dir"`
- Identify IdentifyConfig `toml:"identify"`
- Theme ThemeConfig `toml:"theme"`
- Keys KeysConfig `toml:"keys"`
- }
- func New() *Config {
- return &Config{
- Mouse: true,
- Timestamps: false,
- MessagesLimit: 50,
- Timezone: "Local",
- AttachmentDownloadsDir: UserDownloadsDir(),
- Identify: IdentifyConfig{
- UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
- Os: "Linux",
- Browser: "Chrome",
- },
- Theme: ThemeConfig{
- Background: "black",
- Border: "white",
- Title: "white",
- },
- Keys: KeysConfig{
- ToggleGuildsTree: "Rune[g]",
- ToggleChannelsTree: "Rune[c]",
- ToggleMessagesTextView: "Rune[m]",
- ToggleMessageInput: "Rune[i]",
- OpenMessageActionsList: "Rune[a]",
- OpenExternalEditor: "Ctrl+E",
- SelectPreviousMessage: "Up",
- SelectNextMessage: "Down",
- SelectFirstMessage: "Home",
- SelectLastMessage: "End",
- },
- }
- }
- func (c *Config) Load(path string) error {
- // Create directories that do not exist and are mentioned in the path recursively.
- err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
- if err != nil {
- return err
- }
- // If the configuration file does not exist already, create a new file; otherwise, open the existing file with read-write flag.
- f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm)
- if err != nil {
- return err
- }
- defer f.Close()
- fi, err := f.Stat()
- if err != nil {
- return err
- }
- // If the file is empty (the size of the file is zero), write the default configuration to the file.
- if fi.Size() == 0 {
- return toml.NewEncoder(f).Encode(c)
- }
- _, err = toml.NewDecoder(f).Decode(&c)
- return err
- }
- func DefaultPath() string {
- path, err := os.UserConfigDir()
- if err != nil {
- log.Fatal(err)
- }
- path += "/discordo/config.toml"
- return path
- }
- func UserDownloadsDir() string {
- // We try to set the download folder location to the default Downloads folder
- var dlloc string
- if runtime.GOOS == "windows" {
- h, _ := os.UserHomeDir()
- dlloc = h + "\\Downloads"
- } else if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
- h, _ := os.UserHomeDir()
- dlloc = h + "/Downloads"
- } else {
- dlloc = os.TempDir() // Very lame fallback, I know
- }
- return dlloc
- }
|