theme.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Reset on new styles
  34. sw.Style = tcell.StyleDefault
  35. for key, val := range m {
  36. switch key {
  37. case "foreground":
  38. if s, ok := val.(string); ok {
  39. sw.Style = sw.Foreground(tcell.GetColor(s))
  40. }
  41. case "background":
  42. if s, ok := val.(string); ok {
  43. sw.Style = sw.Background(tcell.GetColor(s))
  44. }
  45. case "attributes":
  46. switch val := val.(type) {
  47. case string:
  48. sw.parseAttr(val)
  49. case []any:
  50. for _, attr := range val {
  51. if s, ok := attr.(string); ok {
  52. sw.parseAttr(s)
  53. }
  54. }
  55. }
  56. case "underline":
  57. if s, ok := val.(string); ok {
  58. switch s {
  59. case "":
  60. sw.Style = sw.Underline(tcell.UnderlineStyleNone)
  61. case "solid":
  62. sw.Style = sw.Underline(tcell.UnderlineStyleSolid)
  63. case "double":
  64. sw.Style = sw.Underline(tcell.UnderlineStyleDouble)
  65. case "curly":
  66. sw.Style = sw.Underline(tcell.UnderlineStyleCurly)
  67. case "dotted":
  68. sw.Style = sw.Underline(tcell.UnderlineStyleDotted)
  69. case "dashed":
  70. sw.Style = sw.Underline(tcell.UnderlineStyleDashed)
  71. }
  72. }
  73. case "underline_color":
  74. if s, ok := val.(string); ok {
  75. sw.Style = sw.Underline(tcell.GetColor(s))
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func (sw *StyleWrapper) parseAttr(s string) {
  82. switch s {
  83. case "underline":
  84. sw.Style = sw.Underline(true)
  85. case "bold":
  86. sw.Style = sw.Bold(true)
  87. case "blink":
  88. sw.Style = sw.Blink(true)
  89. case "reverse":
  90. sw.Style = sw.Reverse(true)
  91. case "dim":
  92. sw.Style = sw.Dim(true)
  93. case "italic":
  94. sw.Style = sw.Italic(true)
  95. case "strikethrough":
  96. sw.Style = sw.StrikeThrough(true)
  97. }
  98. }
  99. type BorderSetWrapper struct{ tview.BorderSet }
  100. func (bw *BorderSetWrapper) UnmarshalTOML(val any) error {
  101. s, ok := val.(string)
  102. if !ok {
  103. return errInvalidType
  104. }
  105. switch s {
  106. case "hidden":
  107. bw.BorderSet = tview.BorderSetHidden()
  108. case "plain":
  109. bw.BorderSet = tview.BorderSetPlain()
  110. case "round":
  111. bw.BorderSet = tview.BorderSetRound()
  112. case "thick":
  113. bw.BorderSet = tview.BorderSetThick()
  114. case "double":
  115. bw.BorderSet = tview.BorderSetDouble()
  116. }
  117. return nil
  118. }
  119. type GlyphSetWrapper struct{ tview.GlyphSet }
  120. func (gw *GlyphSetWrapper) UnmarshalTOML(val any) error {
  121. s, ok := val.(string)
  122. if !ok {
  123. return errInvalidType
  124. }
  125. switch s {
  126. case "minimal":
  127. gw.GlyphSet = tview.MinimalGlyphSet()
  128. case "box_drawing", "boxdrawing", "box":
  129. gw.GlyphSet = tview.BoxDrawingGlyphSet()
  130. case "unicode":
  131. gw.GlyphSet = tview.UnicodeGlyphSet()
  132. }
  133. return nil
  134. }
  135. type ScrollBarVisibilityWrapper struct{ tview.ScrollBarVisibility }
  136. func (vw *ScrollBarVisibilityWrapper) UnmarshalTOML(val any) error {
  137. s, ok := val.(string)
  138. if !ok {
  139. return errInvalidType
  140. }
  141. switch s {
  142. case "automatic", "auto":
  143. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAutomatic
  144. case "always":
  145. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAlways
  146. case "never", "hidden", "off":
  147. vw.ScrollBarVisibility = tview.ScrollBarVisibilityNever
  148. }
  149. return nil
  150. }
  151. type HelpTheme struct {
  152. ShortKeyStyle StyleWrapper `toml:"short_key_style"`
  153. ShortDescStyle StyleWrapper `toml:"short_desc_style"`
  154. FullKeyStyle StyleWrapper `toml:"full_key_style"`
  155. FullDescStyle StyleWrapper `toml:"full_desc_style"`
  156. }
  157. type (
  158. ThemeStyle struct {
  159. NormalStyle StyleWrapper `toml:"normal_style"`
  160. ActiveStyle StyleWrapper `toml:"active_style"`
  161. }
  162. TitleTheme struct {
  163. ThemeStyle
  164. Alignment AlignmentWrapper `toml:"alignment"`
  165. }
  166. FooterTheme struct {
  167. ThemeStyle
  168. Alignment AlignmentWrapper `toml:"alignment"`
  169. }
  170. BorderTheme struct {
  171. ThemeStyle
  172. Enabled bool `toml:"enabled"`
  173. Padding [4]int `toml:"padding"`
  174. NormalSet BorderSetWrapper `toml:"normal_set"`
  175. ActiveSet BorderSetWrapper `toml:"active_set"`
  176. }
  177. GuildsTreeTheme struct {
  178. AutoExpandFolders bool `toml:"auto_expand_folders"`
  179. Graphics bool `toml:"graphics"`
  180. GraphicsColor string `toml:"graphics_color"`
  181. Indents GuildsTreeIndents `toml:"indents"`
  182. }
  183. GuildsTreeIndents struct {
  184. Guild int `toml:"guild"`
  185. Category int `toml:"category"`
  186. Channel int `toml:"channel"`
  187. Forum int `toml:"forum"`
  188. GroupDM int `toml:"group_dm"`
  189. DM int `toml:"dm"`
  190. }
  191. MessagesListTheme struct {
  192. ReplyIndicator string `toml:"reply_indicator"`
  193. ForwardedIndicator string `toml:"forwarded_indicator"`
  194. AuthorStyle StyleWrapper `toml:"author_style"`
  195. MentionStyle StyleWrapper `toml:"mention_style"`
  196. EmojiStyle StyleWrapper `toml:"emoji_style"`
  197. URLStyle StyleWrapper `toml:"url_style"`
  198. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  199. MessageStyle StyleWrapper `toml:"message_style"`
  200. SelectedMessageStyle StyleWrapper `toml:"selected_message_style"`
  201. Embeds MessagesListEmbedsTheme `toml:"embeds"`
  202. }
  203. MessagesListEmbedsTheme struct {
  204. ProviderStyle StyleWrapper `toml:"provider_style"`
  205. AuthorStyle StyleWrapper `toml:"author_style"`
  206. TitleStyle StyleWrapper `toml:"title_style"`
  207. DescriptionStyle StyleWrapper `toml:"description_style"`
  208. FieldNameStyle StyleWrapper `toml:"field_name_style"`
  209. FieldValueStyle StyleWrapper `toml:"field_value_style"`
  210. FooterStyle StyleWrapper `toml:"footer_style"`
  211. URLStyle StyleWrapper `toml:"url_style"`
  212. }
  213. MentionsListTheme struct {
  214. MinWidth uint `toml:"min_width"`
  215. MaxHeight uint `toml:"max_height"`
  216. }
  217. DialogTheme struct {
  218. Style StyleWrapper `toml:"style"`
  219. BackgroundStyle StyleWrapper `toml:"background_style"`
  220. }
  221. ScrollBarTheme struct {
  222. Visibility ScrollBarVisibilityWrapper `toml:"visibility"`
  223. GlyphSet GlyphSetWrapper `toml:"glyph_set"`
  224. TrackStyle StyleWrapper `toml:"track_style"`
  225. ThumbStyle StyleWrapper `toml:"thumb_style"`
  226. }
  227. Theme struct {
  228. Title TitleTheme `toml:"title"`
  229. Footer FooterTheme `toml:"footer"`
  230. Border BorderTheme `toml:"border"`
  231. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  232. ScrollBar ScrollBarTheme `toml:"scroll_bar"`
  233. MessagesList MessagesListTheme `toml:"messages_list"`
  234. MentionsList MentionsListTheme `toml:"mentions_list"`
  235. Dialog DialogTheme `toml:"dialog"`
  236. Help HelpTheme `toml:"help"`
  237. }
  238. )