theme.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "": sw.Style = sw.Underline(tcell.UnderlineStyleNone)
  60. case "solid": sw.Style = sw.Underline(tcell.UnderlineStyleSolid)
  61. case "double": sw.Style = sw.Underline(tcell.UnderlineStyleDouble)
  62. case "curly": sw.Style = sw.Underline(tcell.UnderlineStyleCurly)
  63. case "dotted": sw.Style = sw.Underline(tcell.UnderlineStyleDotted)
  64. case "dashed": sw.Style = sw.Underline(tcell.UnderlineStyleDashed)
  65. }
  66. }
  67. case "underline_color":
  68. if s, ok := val.(string); ok {
  69. sw.Style = sw.Underline(tcell.GetColor(s))
  70. }
  71. }
  72. }
  73. return nil
  74. }
  75. func (sw *StyleWrapper) parseAttr(s string) {
  76. switch s {
  77. case "underline":
  78. sw.Style = sw.Underline(true)
  79. case "bold":
  80. sw.Style = sw.Bold(true)
  81. case "blink":
  82. sw.Style = sw.Blink(true)
  83. case "reverse":
  84. sw.Style = sw.Reverse(true)
  85. case "dim":
  86. sw.Style = sw.Dim(true)
  87. case "italic":
  88. sw.Style = sw.Italic(true)
  89. case "strikethrough":
  90. sw.Style = sw.StrikeThrough(true)
  91. }
  92. }
  93. type BorderSetWrapper struct{ tview.BorderSet }
  94. func (bw *BorderSetWrapper) UnmarshalTOML(val any) error {
  95. s, ok := val.(string)
  96. if !ok {
  97. return errInvalidType
  98. }
  99. switch s {
  100. case "hidden":
  101. bw.BorderSet = tview.BorderSetHidden()
  102. case "plain":
  103. bw.BorderSet = tview.BorderSetPlain()
  104. case "round":
  105. bw.BorderSet = tview.BorderSetRound()
  106. case "thick":
  107. bw.BorderSet = tview.BorderSetThick()
  108. case "double":
  109. bw.BorderSet = tview.BorderSetDouble()
  110. }
  111. return nil
  112. }
  113. type GlyphSetWrapper struct{ tview.GlyphSet }
  114. func (gw *GlyphSetWrapper) UnmarshalTOML(val any) error {
  115. s, ok := val.(string)
  116. if !ok {
  117. return errInvalidType
  118. }
  119. switch s {
  120. case "minimal":
  121. gw.GlyphSet = tview.MinimalGlyphSet()
  122. case "box_drawing", "boxdrawing", "box":
  123. gw.GlyphSet = tview.BoxDrawingGlyphSet()
  124. case "unicode":
  125. gw.GlyphSet = tview.UnicodeGlyphSet()
  126. }
  127. return nil
  128. }
  129. type ScrollBarVisibilityWrapper struct{ tview.ScrollBarVisibility }
  130. func (vw *ScrollBarVisibilityWrapper) UnmarshalTOML(val any) error {
  131. s, ok := val.(string)
  132. if !ok {
  133. return errInvalidType
  134. }
  135. switch s {
  136. case "automatic", "auto":
  137. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAutomatic
  138. case "always":
  139. vw.ScrollBarVisibility = tview.ScrollBarVisibilityAlways
  140. case "never", "hidden", "off":
  141. vw.ScrollBarVisibility = tview.ScrollBarVisibilityNever
  142. }
  143. return nil
  144. }
  145. type HelpTheme struct {
  146. ShortKeyStyle StyleWrapper `toml:"short_key_style"`
  147. ShortDescStyle StyleWrapper `toml:"short_desc_style"`
  148. FullKeyStyle StyleWrapper `toml:"full_key_style"`
  149. FullDescStyle StyleWrapper `toml:"full_desc_style"`
  150. }
  151. type (
  152. ThemeStyle struct {
  153. NormalStyle StyleWrapper `toml:"normal_style"`
  154. ActiveStyle StyleWrapper `toml:"active_style"`
  155. }
  156. TitleTheme struct {
  157. ThemeStyle
  158. Alignment AlignmentWrapper `toml:"alignment"`
  159. }
  160. FooterTheme struct {
  161. ThemeStyle
  162. Alignment AlignmentWrapper `toml:"alignment"`
  163. }
  164. BorderTheme struct {
  165. ThemeStyle
  166. Enabled bool `toml:"enabled"`
  167. Padding [4]int `toml:"padding"`
  168. NormalSet BorderSetWrapper `toml:"normal_set"`
  169. ActiveSet BorderSetWrapper `toml:"active_set"`
  170. }
  171. GuildsTreeTheme struct {
  172. AutoExpandFolders bool `toml:"auto_expand_folders"`
  173. Graphics bool `toml:"graphics"`
  174. GraphicsColor string `toml:"graphics_color"`
  175. }
  176. MessagesListTheme struct {
  177. ReplyIndicator string `toml:"reply_indicator"`
  178. ForwardedIndicator string `toml:"forwarded_indicator"`
  179. AuthorStyle StyleWrapper `toml:"author_style"`
  180. MentionStyle StyleWrapper `toml:"mention_style"`
  181. EmojiStyle StyleWrapper `toml:"emoji_style"`
  182. URLStyle StyleWrapper `toml:"url_style"`
  183. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  184. MessageStyle StyleWrapper `toml:"message_style"`
  185. SelectedMessageStyle StyleWrapper `toml:"selected_message_style"`
  186. }
  187. MentionsListTheme struct {
  188. MinWidth uint `toml:"min_width"`
  189. MaxHeight uint `toml:"max_height"`
  190. }
  191. DialogTheme struct {
  192. Style StyleWrapper `toml:"style"`
  193. BackgroundStyle StyleWrapper `toml:"background_style"`
  194. }
  195. ScrollBarTheme struct {
  196. Visibility ScrollBarVisibilityWrapper `toml:"visibility"`
  197. GlyphSet GlyphSetWrapper `toml:"glyph_set"`
  198. TrackStyle StyleWrapper `toml:"track_style"`
  199. ThumbStyle StyleWrapper `toml:"thumb_style"`
  200. }
  201. Theme struct {
  202. Title TitleTheme `toml:"title"`
  203. Footer FooterTheme `toml:"footer"`
  204. Border BorderTheme `toml:"border"`
  205. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  206. ScrollBar ScrollBarTheme `toml:"scroll_bar"`
  207. MessagesList MessagesListTheme `toml:"messages_list"`
  208. MentionsList MentionsListTheme `toml:"mentions_list"`
  209. Dialog DialogTheme `toml:"dialog"`
  210. Help HelpTheme `toml:"help"`
  211. }
  212. )