config.go 3.0 KB

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