theme.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package config
  2. import (
  3. "errors"
  4. "github.com/ayn2op/tview"
  5. "github.com/gdamore/tcell/v2"
  6. )
  7. var (
  8. errInvalidType = errors.New("invalid type")
  9. )
  10. type BorderSetWrapper struct{ tview.BorderSet }
  11. func (bw *BorderSetWrapper) UnmarshalTOML(val any) error {
  12. s, ok := val.(string)
  13. if !ok {
  14. return errInvalidType
  15. }
  16. switch s {
  17. case "plain":
  18. bw.BorderSet = tview.BorderSetPlain()
  19. case "round":
  20. bw.BorderSet = tview.BorderSetRound()
  21. case "thick":
  22. bw.BorderSet = tview.BorderSetThick()
  23. case "double":
  24. bw.BorderSet = tview.BorderSetDouble()
  25. }
  26. return nil
  27. }
  28. type AlignmentWrapper struct{ tview.Alignment }
  29. func (aw *AlignmentWrapper) UnmarshalTOML(v any) error {
  30. s, ok := v.(string)
  31. if !ok {
  32. return errInvalidType
  33. }
  34. switch s {
  35. case "left":
  36. aw.Alignment = tview.AlignmentLeft
  37. case "center":
  38. aw.Alignment = tview.AlignmentCenter
  39. case "right":
  40. aw.Alignment = tview.AlignmentRight
  41. }
  42. return nil
  43. }
  44. type StyleWrapper struct{ tcell.Style }
  45. func NewStyleWrapper(style tcell.Style) StyleWrapper {
  46. return StyleWrapper{Style: style}
  47. }
  48. func (sw *StyleWrapper) UnmarshalTOML(v any) error {
  49. m, ok := v.(map[string]any)
  50. if !ok {
  51. return errInvalidType
  52. }
  53. for key, val := range m {
  54. s, ok := val.(string)
  55. if !ok {
  56. continue
  57. }
  58. switch key {
  59. case "foreground":
  60. color := tcell.GetColor(s)
  61. sw.Style = sw.Foreground(color)
  62. case "background":
  63. color := tcell.GetColor(s)
  64. sw.Style = sw.Background(color)
  65. }
  66. }
  67. return nil
  68. }
  69. type (
  70. ThemeStyle struct {
  71. NormalStyle StyleWrapper `toml:"normal_style"`
  72. ActiveStyle StyleWrapper `toml:"active_style"`
  73. }
  74. TitleTheme struct {
  75. ThemeStyle
  76. Alignment AlignmentWrapper `toml:"alignment"`
  77. }
  78. BorderTheme struct {
  79. ThemeStyle
  80. Enabled bool `toml:"enabled"`
  81. Padding [4]int `toml:"padding"`
  82. Set BorderSetWrapper `toml:"set"`
  83. }
  84. GuildsTreeTheme struct {
  85. AutoExpandFolders bool `toml:"auto_expand_folders"`
  86. Graphics bool `toml:"graphics"`
  87. GraphicsColor string `toml:"graphics_color"`
  88. PrivateChannelStyle StyleWrapper `toml:"private_channel_style"`
  89. GuildStyle StyleWrapper `toml:"guild_style"`
  90. ChannelStyle StyleWrapper `toml:"channel_style"`
  91. }
  92. MessagesTextTheme struct {
  93. ShowNicknames bool `toml:"show_user_nicks"`
  94. ShowUsernameColors bool `toml:"show_user_colors"`
  95. ReplyIndicator string `toml:"reply_indicator"`
  96. ForwardedIndicator string `toml:"forwarded_indicator"`
  97. AuthorStyle StyleWrapper `toml:"author_style"`
  98. MentionStyle StyleWrapper `toml:"mention_style"`
  99. EmojiStyle StyleWrapper `toml:"emoji_style"`
  100. URLStyle StyleWrapper `toml:"url_style"`
  101. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  102. }
  103. AutocompleteTheme struct {
  104. ShowNicknames bool `toml:"show_user_nicks"`
  105. ShowUsernameColors bool `toml:"show_user_colors"`
  106. MinWidth uint `toml:"min_width"`
  107. MaxHeight uint `toml:"max_height"`
  108. }
  109. Theme struct {
  110. BackgroundColor string `toml:"background_color"`
  111. Title TitleTheme `toml:"title"`
  112. Border BorderTheme `toml:"border"`
  113. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  114. MessagesText MessagesTextTheme `toml:"messages_text"`
  115. Autocomplete AutocompleteTheme `toml:"autocomplete"`
  116. }
  117. )