theme.go 6.8 KB

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