theme.go 5.9 KB

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