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. SendMessage string `yaml:"send_message"`
  19. OpenExternalEditor string `yaml:"open_external_editor"`
  20. PasteClipboard string `yaml:"paste_clipboard"`
  21. }
  22. type KeysConfig struct {
  23. MessagesView MessagesViewKeysConfig `yaml:"messages_view"`
  24. InputView InputViewKeysConfig `yaml:"input_view"`
  25. }
  26. type ThemeConfig struct {
  27. Background string `yaml:"background"`
  28. Border string `yaml:"border"`
  29. Title string `yaml:"title"`
  30. }
  31. type Config struct {
  32. // Whether the mouse is usable or not.
  33. Mouse bool `yaml:"mouse"`
  34. // The maximum number of messages to fetch and display. Its value must not be lesser than 1 and greater than 100.
  35. MessagesLimit uint `yaml:"messages_limit"`
  36. // Whether to display the timestamps of the messages beside the displayed message or not.
  37. Timestamps bool `yaml:"timestamps"`
  38. // The timezone of the timestamps. Learn more: https://pkg.go.dev/time#LoadLocation
  39. Timezone string `yaml:"timezone"`
  40. // A textual representation of the time value formatted according to the layout defined by its value. Learn more: https://pkg.go.dev/time#Layout
  41. TimeFormat string `yaml:"time_format"`
  42. // Keybindings
  43. Keys KeysConfig `yaml:"keys"`
  44. // Theme
  45. Theme ThemeConfig `yaml:"theme"`
  46. }
  47. func New() *Config {
  48. return &Config{
  49. Mouse: true,
  50. MessagesLimit: 50,
  51. Timestamps: false,
  52. Timezone: "Local",
  53. TimeFormat: time.Kitchen,
  54. Keys: KeysConfig{
  55. MessagesView: MessagesViewKeysConfig{
  56. OpenActionsView: "Rune[a]",
  57. SelectPreviousMessage: "Up",
  58. SelectNextMessage: "Down",
  59. SelectFirstMessage: "Home",
  60. SelectLastMessage: "End",
  61. },
  62. InputView: InputViewKeysConfig{
  63. SendMessage: "Enter",
  64. OpenExternalEditor: "Ctrl+E",
  65. PasteClipboard: "Ctrl+V",
  66. },
  67. },
  68. Theme: ThemeConfig{
  69. Background: "default",
  70. Border: "white",
  71. Title: "white",
  72. },
  73. }
  74. }
  75. func (cfg *Config) Load() error {
  76. path, _ := os.UserConfigDir()
  77. // Create the configuration directory if it does not exist already.
  78. path = filepath.Join(path, Name)
  79. err := os.MkdirAll(path, os.ModePerm)
  80. if err != nil {
  81. return err
  82. }
  83. path = filepath.Join(path, "config.yml")
  84. // Open the configuration file with create and read-write flag.
  85. f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
  86. if err != nil {
  87. return err
  88. }
  89. defer f.Close()
  90. fi, err := f.Stat()
  91. if err != nil {
  92. return err
  93. }
  94. // 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.
  95. if fi.Size() == 0 {
  96. return yaml.NewEncoder(f).Encode(cfg)
  97. }
  98. return yaml.NewDecoder(f).Decode(&cfg)
  99. }