config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package config
  2. import (
  3. _ "embed"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "gopkg.in/yaml.v3"
  8. )
  9. const Name = "discordo"
  10. type MessagesViewKeysConfig struct {
  11. OpenActionsView string `yaml:"open_actions_view"`
  12. SelectPreviousMessage string `yaml:"select_previous_message"`
  13. SelectNextMessage string `yaml:"select_next_message"`
  14. SelectFirstMessage string `yaml:"select_first_message"`
  15. SelectLastMessage string `yaml:"select_last_message"`
  16. }
  17. type InputViewKeysConfig struct {
  18. OpenExternalEditor string `yaml:"open_external_editor"`
  19. PasteClipboard string `yaml:"paste_clipboard"`
  20. }
  21. type KeysConfig struct {
  22. MessagesView MessagesViewKeysConfig `yaml:"messages_view"`
  23. InputView InputViewKeysConfig `yaml:"input_view"`
  24. }
  25. type ThemeConfig struct {
  26. Background string `yaml:"background"`
  27. Border string `yaml:"border"`
  28. Title string `yaml:"title"`
  29. }
  30. type Config struct {
  31. // Whether to send desktop notification when a new message is sent or not.
  32. Notifications bool `yaml:"notifications"`
  33. // Whether the mouse is usable or not.
  34. Mouse bool `yaml:"mouse"`
  35. // The maximum number of messages to fetch and display. Its value must not be lesser than 1 and greater than 100.
  36. MessagesLimit uint `yaml:"messages_limit"`
  37. // Whether to display the timestamps of the messages beside the displayed message or not.
  38. Timestamps bool `yaml:"timestamps"`
  39. // The timezone of the timestamps. Learn more: https://pkg.go.dev/time#LoadLocation
  40. Timezone string `yaml:"timezone"`
  41. // A textual representation of the time value formatted according to the layout defined by its value. Learn more: https://pkg.go.dev/time#Layout
  42. TimeFormat string `yaml:"time_format"`
  43. // Keybindings
  44. Keys KeysConfig `yaml:"keys"`
  45. // Theme
  46. Theme ThemeConfig `yaml:"theme"`
  47. }
  48. func New() *Config {
  49. return &Config{
  50. Notifications: true,
  51. Mouse: true,
  52. MessagesLimit: 50,
  53. Timestamps: false,
  54. Timezone: "Local",
  55. TimeFormat: time.Kitchen,
  56. Keys: KeysConfig{
  57. MessagesView: MessagesViewKeysConfig{
  58. OpenActionsView: "Rune[a]",
  59. SelectPreviousMessage: "Up",
  60. SelectNextMessage: "Down",
  61. SelectFirstMessage: "Home",
  62. SelectLastMessage: "End",
  63. },
  64. },
  65. Theme: ThemeConfig{
  66. Background: "default",
  67. Border: "white",
  68. Title: "white",
  69. },
  70. }
  71. }
  72. func (cfg *Config) Load(path string) error {
  73. // Open the existing configuration file with read-only flag.
  74. f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
  75. if err != nil {
  76. return err
  77. }
  78. defer f.Close()
  79. fi, err := f.Stat()
  80. if err != nil {
  81. return err
  82. }
  83. // If the configuration file is empty (the size of the file is zero; a new configuration file was created), write the default configuration to the file.
  84. if fi.Size() == 0 {
  85. return yaml.NewEncoder(f).Encode(cfg)
  86. }
  87. return yaml.NewDecoder(f).Decode(&cfg)
  88. }
  89. // DefaultConfigPath returns the default configuration file path.
  90. func DefaultConfigPath() string {
  91. path, _ := os.UserConfigDir()
  92. return filepath.Join(path, Name+".yml")
  93. }
  94. // DefaultLogPath returns the default log file path.
  95. func DefaultLogPath() string {
  96. path, _ := os.UserCacheDir()
  97. return filepath.Join(path, Name+".log")
  98. }