config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 (
  11. MessagesTextKeysConfig struct {
  12. LaunchActions string `yaml:"launch_actions"`
  13. SelectPrevious string `yaml:"select_previous"`
  14. SelectNext string `yaml:"select_next"`
  15. SelectFirst string `yaml:"select_first"`
  16. SelectLast string `yaml:"select_last"`
  17. }
  18. MessageInputKeysConfig struct {
  19. Send string `yaml:"send"`
  20. Paste string `yaml:"paste"`
  21. LaunchEditor string `yaml:"launch_editor"`
  22. }
  23. KeysConfig struct {
  24. MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
  25. MessageInput MessageInputKeysConfig `yaml:"message_input"`
  26. }
  27. )
  28. type ThemeConfig struct {
  29. Background string `yaml:"background"`
  30. Border string `yaml:"border"`
  31. Title string `yaml:"title"`
  32. }
  33. type Config struct {
  34. // Whether the mouse is usable or not.
  35. Mouse bool `yaml:"mouse"`
  36. // The maximum number of messages to fetch and display. Its value must not be lesser than 1 and greater than 100.
  37. MessagesLimit uint `yaml:"messages_limit"`
  38. // Whether to display the timestamps of the messages beside the displayed message or not.
  39. Timestamps bool `yaml:"timestamps"`
  40. // The timezone of the timestamps. Learn more: https://pkg.go.dev/time#LoadLocation
  41. Timezone string `yaml:"timezone"`
  42. // A textual representation of the time value formatted according to the layout defined by its value. Learn more: https://pkg.go.dev/time#Layout
  43. TimeFormat string `yaml:"time_format"`
  44. Keys KeysConfig `yaml:"keys"`
  45. Theme ThemeConfig `yaml:"theme"`
  46. }
  47. func New() (*Config, error) {
  48. path, err := os.UserConfigDir()
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Create the configuration directory if it does not exist already.
  53. path = filepath.Join(path, Name)
  54. err = os.MkdirAll(path, os.ModePerm)
  55. if err != nil {
  56. return nil, err
  57. }
  58. c := def()
  59. path = filepath.Join(path, "config.yml")
  60. _, err = os.Stat(path)
  61. // If the configuration file does not exist, create a new one and write the default configuration to it.
  62. if os.IsNotExist(err) {
  63. f, err := os.Create(path)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer f.Close()
  68. err = yaml.NewEncoder(f).Encode(c)
  69. if err != nil {
  70. return nil, err
  71. }
  72. } else {
  73. f, err := os.Open(path)
  74. if err != nil {
  75. return nil, err
  76. }
  77. defer f.Close()
  78. err = yaml.NewDecoder(f).Decode(&c)
  79. if err != nil {
  80. return nil, err
  81. }
  82. }
  83. return &c, nil
  84. }
  85. func def() Config {
  86. return Config{
  87. Mouse: true,
  88. MessagesLimit: 50,
  89. Timestamps: false,
  90. Timezone: "Local",
  91. TimeFormat: time.Kitchen,
  92. Keys: KeysConfig{
  93. MessagesText: MessagesTextKeysConfig{
  94. LaunchActions: "Rune[a]",
  95. SelectPrevious: "Up",
  96. SelectNext: "Down",
  97. SelectFirst: "Home",
  98. SelectLast: "End",
  99. },
  100. MessageInput: MessageInputKeysConfig{
  101. Send: "Enter",
  102. Paste: "Ctrl+V",
  103. LaunchEditor: "Ctrl+E",
  104. },
  105. },
  106. Theme: ThemeConfig{
  107. Background: "default",
  108. Border: "white",
  109. Title: "white",
  110. },
  111. }
  112. }
  113. // LogDirPath returns the path of the log directory.
  114. func LogDirPath() string {
  115. path, _ := os.UserCacheDir()
  116. return filepath.Join(path, Name)
  117. }