| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package main
- import (
- "os"
- "path/filepath"
- "gopkg.in/yaml.v3"
- )
- const name = "discordo"
- type (
- MessagesTextKeysConfig struct {
- CopyContent string `yaml:"copy_content"`
- Reply string `yaml:"reply"`
- ReplyMention string `yaml:"reply_mention"`
- SelectReply string `yaml:"select_reply"`
- SelectPrevious string `yaml:"select_previous"`
- SelectNext string `yaml:"select_next"`
- SelectFirst string `yaml:"select_first"`
- SelectLast string `yaml:"select_last"`
- }
- MessageInputKeysConfig struct {
- Send string `yaml:"send"`
- Paste string `yaml:"paste"`
- LaunchEditor string `yaml:"launch_editor"`
- }
- KeysConfig struct {
- Cancel string `yaml:"cancel"`
- MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
- MessageInput MessageInputKeysConfig `yaml:"message_input"`
- }
- )
- type (
- GuildsTreeThemeConfig struct {
- Graphics bool `yaml:"graphics"`
- }
- MessagesTextThemeConfig struct {
- AuthorColor string `yaml:"author_color"`
- }
- MessageInputThemeConfig struct{}
- ThemeConfig struct {
- Border bool `yaml:"border"`
- BorderPadding [4]int `yaml:"border_padding,flow"`
- TitleColor string `yaml:"title_color"`
- BackgroundColor string `yaml:"background_color"`
- GuildsTree GuildsTreeThemeConfig `yaml:"guilds_tree"`
- MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
- MessageInput MessageInputThemeConfig `yaml:"message_input"`
- }
- )
- type Config struct {
- Mouse bool `yaml:"mouse"`
- MessagesLimit uint `yaml:"messages_limit"`
- Timestamps bool `yaml:"timestamps"`
- Editor string `yaml:"editor"`
- Keys KeysConfig `yaml:"keys"`
- Theme ThemeConfig `yaml:"theme"`
- }
- func newConfig() (*Config, error) {
- path, err := os.UserConfigDir()
- if err != nil {
- return nil, err
- }
- path = filepath.Join(path, name)
- err = os.MkdirAll(path, os.ModePerm)
- if err != nil {
- return nil, err
- }
- c := Config{
- Mouse: true,
- Timestamps: false,
- MessagesLimit: 50,
- Editor: "default",
- Keys: KeysConfig{
- Cancel: "Esc",
- MessagesText: MessagesTextKeysConfig{
- CopyContent: "Rune[c]",
- Reply: "Rune[r]",
- ReplyMention: "Rune[R]",
- SelectReply: "Rune[s]",
- SelectPrevious: "Up",
- SelectNext: "Down",
- SelectFirst: "Home",
- SelectLast: "End",
- },
- MessageInput: MessageInputKeysConfig{
- Send: "Enter",
- Paste: "Ctrl+V",
- LaunchEditor: "Ctrl+E",
- },
- },
- Theme: ThemeConfig{
- Border: true,
- BorderPadding: [...]int{0, 0, 1, 1},
- TitleColor: "default",
- BackgroundColor: "default",
- GuildsTree: GuildsTreeThemeConfig{
- Graphics: true,
- },
- MessagesText: MessagesTextThemeConfig{
- AuthorColor: "aqua",
- },
- MessageInput: MessageInputThemeConfig{},
- },
- }
- path = filepath.Join(path, "config.yml")
- _, err = os.Stat(path)
- if os.IsNotExist(err) {
- f, err := os.Create(path)
- if err != nil {
- return nil, err
- }
- defer f.Close()
- err = yaml.NewEncoder(f).Encode(c)
- if err != nil {
- return nil, err
- }
- } else {
- f, err := os.Open(path)
- if err != nil {
- return nil, err
- }
- defer f.Close()
- err = yaml.NewDecoder(f).Decode(&c)
- if err != nil {
- return nil, err
- }
- }
- return &c, nil
- }
|