theme.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package config
  2. import (
  3. "errors"
  4. "github.com/ayn2op/tview"
  5. "github.com/gdamore/tcell/v3"
  6. )
  7. var errInvalidType = errors.New("invalid type")
  8. type AlignmentWrapper struct{ tview.Alignment }
  9. func (aw *AlignmentWrapper) UnmarshalTOML(v any) error {
  10. s, ok := v.(string)
  11. if !ok {
  12. return errInvalidType
  13. }
  14. switch s {
  15. case "left":
  16. aw.Alignment = tview.AlignmentLeft
  17. case "center":
  18. aw.Alignment = tview.AlignmentCenter
  19. case "right":
  20. aw.Alignment = tview.AlignmentRight
  21. }
  22. return nil
  23. }
  24. type StyleWrapper struct{ tcell.Style }
  25. func NewStyleWrapper(style tcell.Style) StyleWrapper {
  26. return StyleWrapper{Style: style}
  27. }
  28. func (sw *StyleWrapper) UnmarshalTOML(v any) error {
  29. m, ok := v.(map[string]any)
  30. if !ok {
  31. return errInvalidType
  32. }
  33. for key, val := range m {
  34. switch key {
  35. case "foreground":
  36. s, ok := val.(string)
  37. if !ok {
  38. continue
  39. }
  40. color := tcell.GetColor(s)
  41. sw.Style = sw.Foreground(color)
  42. case "background":
  43. s, ok := val.(string)
  44. if !ok {
  45. continue
  46. }
  47. color := tcell.GetColor(s)
  48. sw.Style = sw.Background(color)
  49. case "attributes":
  50. var attrs tcell.AttrMask
  51. switch val := val.(type) {
  52. case string:
  53. attrs |= stringToAttrMask(val)
  54. case []any:
  55. for _, attr := range val {
  56. s, ok := attr.(string)
  57. if !ok {
  58. continue
  59. }
  60. attrs |= stringToAttrMask(s)
  61. }
  62. }
  63. sw.Style = sw.Attributes(attrs)
  64. }
  65. }
  66. return nil
  67. }
  68. type BorderSetWrapper struct{ tview.BorderSet }
  69. func (bw *BorderSetWrapper) UnmarshalTOML(val any) error {
  70. s, ok := val.(string)
  71. if !ok {
  72. return errInvalidType
  73. }
  74. switch s {
  75. case "hidden":
  76. bw.BorderSet = tview.BorderSetHidden()
  77. case "plain":
  78. bw.BorderSet = tview.BorderSetPlain()
  79. case "round":
  80. bw.BorderSet = tview.BorderSetRound()
  81. case "thick":
  82. bw.BorderSet = tview.BorderSetThick()
  83. case "double":
  84. bw.BorderSet = tview.BorderSetDouble()
  85. }
  86. return nil
  87. }
  88. type GlyphSetWrapper struct{ tview.GlyphSet }
  89. func (gw *GlyphSetWrapper) UnmarshalTOML(val any) error {
  90. s, ok := val.(string)
  91. if !ok {
  92. return errInvalidType
  93. }
  94. switch s {
  95. case "minimal":
  96. gw.GlyphSet = tview.MinimalGlyphSet()
  97. case "box_drawing", "boxdrawing", "box":
  98. gw.GlyphSet = tview.BoxDrawingGlyphSet()
  99. case "unicode":
  100. gw.GlyphSet = tview.UnicodeGlyphSet()
  101. }
  102. return nil
  103. }
  104. type ScrollBarVisibilityWrapper struct{ tview.ScrollBarVisibility }
  105. func (vw *ScrollBarVisibilityWrapper) UnmarshalTOML(val any) error {
  106. s, ok := val.(string)
  107. if !ok {
  108. return errInvalidType
  109. }
  110. switch s {
  111. case "automatic", "auto":
  112. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAutomatic
  113. case "always":
  114. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAlways
  115. case "never", "hidden", "off":
  116. vw.ScrollBarVisibility = tview.ScrollBarVisibilityNever
  117. }
  118. return nil
  119. }
  120. type HelpTheme struct {
  121. ShortKeyStyle StyleWrapper `toml:"short_key_style"`
  122. ShortDescStyle StyleWrapper `toml:"short_desc_style"`
  123. FullKeyStyle StyleWrapper `toml:"full_key_style"`
  124. FullDescStyle StyleWrapper `toml:"full_desc_style"`
  125. }
  126. type (
  127. ThemeStyle struct {
  128. NormalStyle StyleWrapper `toml:"normal_style"`
  129. ActiveStyle StyleWrapper `toml:"active_style"`
  130. }
  131. TitleTheme struct {
  132. ThemeStyle
  133. Alignment AlignmentWrapper `toml:"alignment"`
  134. }
  135. FooterTheme struct {
  136. ThemeStyle
  137. Alignment AlignmentWrapper `toml:"alignment"`
  138. }
  139. BorderTheme struct {
  140. ThemeStyle
  141. Enabled bool `toml:"enabled"`
  142. Padding [4]int `toml:"padding"`
  143. NormalSet BorderSetWrapper `toml:"normal_set"`
  144. ActiveSet BorderSetWrapper `toml:"active_set"`
  145. }
  146. GuildsTreeTheme struct {
  147. AutoExpandFolders bool `toml:"auto_expand_folders"`
  148. Graphics bool `toml:"graphics"`
  149. GraphicsColor string `toml:"graphics_color"`
  150. }
  151. MessagesListTheme struct {
  152. ReplyIndicator string `toml:"reply_indicator"`
  153. ForwardedIndicator string `toml:"forwarded_indicator"`
  154. AuthorStyle StyleWrapper `toml:"author_style"`
  155. MentionStyle StyleWrapper `toml:"mention_style"`
  156. EmojiStyle StyleWrapper `toml:"emoji_style"`
  157. URLStyle StyleWrapper `toml:"url_style"`
  158. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  159. MessageStyle StyleWrapper `toml:"message_style"`
  160. SelectedMessageStyle StyleWrapper `toml:"selected_message_style"`
  161. }
  162. MentionsListTheme struct {
  163. MinWidth uint `toml:"min_width"`
  164. MaxHeight uint `toml:"max_height"`
  165. }
  166. DialogTheme struct {
  167. Style StyleWrapper `toml:"style"`
  168. BackgroundStyle StyleWrapper `toml:"background_style"`
  169. }
  170. ScrollBarTheme struct {
  171. Visibility ScrollBarVisibilityWrapper `toml:"visibility"`
  172. GlyphSet GlyphSetWrapper `toml:"glyph_set"`
  173. TrackStyle StyleWrapper `toml:"track_style"`
  174. ThumbStyle StyleWrapper `toml:"thumb_style"`
  175. }
  176. Theme struct {
  177. Title TitleTheme `toml:"title"`
  178. Footer FooterTheme `toml:"footer"`
  179. Border BorderTheme `toml:"border"`
  180. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  181. ScrollBar ScrollBarTheme `toml:"scroll_bar"`
  182. MessagesList MessagesListTheme `toml:"messages_list"`
  183. MentionsList MentionsListTheme `toml:"mentions_list"`
  184. Dialog DialogTheme `toml:"dialog"`
  185. Help HelpTheme `toml:"help"`
  186. }
  187. )
  188. func stringToAttrMask(s string) tcell.AttrMask {
  189. switch s {
  190. case "bold":
  191. return tcell.AttrBold
  192. case "blink":
  193. return tcell.AttrBlink
  194. case "reverse":
  195. return tcell.AttrReverse
  196. case "dim":
  197. return tcell.AttrDim
  198. case "italic":
  199. return tcell.AttrItalic
  200. case "strikethrough":
  201. return tcell.AttrStrikeThrough
  202. default:
  203. return tcell.AttrNone
  204. }
  205. }