theme.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. switch key {
  55. case "foreground":
  56. s, ok := val.(string)
  57. if !ok {
  58. continue
  59. }
  60. color := tcell.GetColor(s)
  61. sw.Style = sw.Foreground(color)
  62. case "background":
  63. s, ok := val.(string)
  64. if !ok {
  65. continue
  66. }
  67. color := tcell.GetColor(s)
  68. sw.Style = sw.Background(color)
  69. case "attributes":
  70. var attrs tcell.AttrMask
  71. switch val := val.(type) {
  72. case string:
  73. attrs |= stringToAttrMask(val)
  74. case []any:
  75. for _, attr := range val {
  76. s, ok := attr.(string)
  77. if !ok {
  78. continue
  79. }
  80. attrs |= stringToAttrMask(s)
  81. }
  82. }
  83. sw.Style = sw.Attributes(attrs)
  84. }
  85. }
  86. return nil
  87. }
  88. type (
  89. ThemeStyle struct {
  90. NormalStyle StyleWrapper `toml:"normal_style"`
  91. ActiveStyle StyleWrapper `toml:"active_style"`
  92. }
  93. TitleTheme struct {
  94. ThemeStyle
  95. Alignment AlignmentWrapper `toml:"alignment"`
  96. }
  97. BorderTheme struct {
  98. ThemeStyle
  99. Enabled bool `toml:"enabled"`
  100. Padding [4]int `toml:"padding"`
  101. Set BorderSetWrapper `toml:"set"`
  102. }
  103. GuildsTreeTheme struct {
  104. AutoExpandFolders bool `toml:"auto_expand_folders"`
  105. Graphics bool `toml:"graphics"`
  106. GraphicsColor string `toml:"graphics_color"`
  107. PrivateChannelStyle StyleWrapper `toml:"private_channel_style"`
  108. GuildStyle StyleWrapper `toml:"guild_style"`
  109. ChannelStyle StyleWrapper `toml:"channel_style"`
  110. }
  111. MessagesListTheme struct {
  112. ReplyIndicator string `toml:"reply_indicator"`
  113. ForwardedIndicator string `toml:"forwarded_indicator"`
  114. AuthorStyle StyleWrapper `toml:"author_style"`
  115. MentionStyle StyleWrapper `toml:"mention_style"`
  116. EmojiStyle StyleWrapper `toml:"emoji_style"`
  117. URLStyle StyleWrapper `toml:"url_style"`
  118. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  119. }
  120. MentionsListTheme struct {
  121. MinWidth uint `toml:"min_width"`
  122. MaxHeight uint `toml:"max_height"`
  123. }
  124. Theme struct {
  125. BackgroundColor string `toml:"background_color"`
  126. Title TitleTheme `toml:"title"`
  127. Border BorderTheme `toml:"border"`
  128. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  129. MessagesList MessagesListTheme `toml:"messages_list"`
  130. MentionsList MentionsListTheme `toml:"mentions_list"`
  131. }
  132. )
  133. func stringToAttrMask(s string) tcell.AttrMask {
  134. switch s {
  135. case "bold":
  136. return tcell.AttrBold
  137. case "blink":
  138. return tcell.AttrBlink
  139. case "reverse":
  140. return tcell.AttrReverse
  141. case "underline":
  142. return tcell.AttrUnderline
  143. case "dim":
  144. return tcell.AttrDim
  145. case "italic":
  146. return tcell.AttrItalic
  147. case "strikethrough":
  148. return tcell.AttrStrikeThrough
  149. default:
  150. return tcell.AttrNone
  151. }
  152. }