theme.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 (
  89. ThemeStyle struct {
  90. NormalStyle StyleWrapper `toml:"normal_style"`
  91. ActiveStyle StyleWrapper `toml:"active_style"`
  92. }
  93. TitleTheme struct {
  94. ThemeStyle
  95. Alignment AlignmentWrapper `toml:"alignment"`
  96. }
  97. FooterTheme struct {
  98. ThemeStyle
  99. Alignment AlignmentWrapper `toml:"alignment"`
  100. }
  101. BorderTheme struct {
  102. ThemeStyle
  103. Enabled bool `toml:"enabled"`
  104. Padding [4]int `toml:"padding"`
  105. NormalSet BorderSetWrapper `toml:"normal_set"`
  106. ActiveSet BorderSetWrapper `toml:"active_set"`
  107. }
  108. GuildsTreeTheme struct {
  109. AutoExpandFolders bool `toml:"auto_expand_folders"`
  110. Graphics bool `toml:"graphics"`
  111. GraphicsColor string `toml:"graphics_color"`
  112. }
  113. MessagesListTheme struct {
  114. ReplyIndicator string `toml:"reply_indicator"`
  115. ForwardedIndicator string `toml:"forwarded_indicator"`
  116. AuthorStyle StyleWrapper `toml:"author_style"`
  117. MentionStyle StyleWrapper `toml:"mention_style"`
  118. EmojiStyle StyleWrapper `toml:"emoji_style"`
  119. URLStyle StyleWrapper `toml:"url_style"`
  120. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  121. }
  122. MentionsListTheme struct {
  123. MinWidth uint `toml:"min_width"`
  124. MaxHeight uint `toml:"max_height"`
  125. }
  126. Theme struct {
  127. Title TitleTheme `toml:"title"`
  128. Footer FooterTheme `toml:"footer"`
  129. Border BorderTheme `toml:"border"`
  130. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  131. MessagesList MessagesListTheme `toml:"messages_list"`
  132. MentionsList MentionsListTheme `toml:"mentions_list"`
  133. }
  134. )
  135. func stringToAttrMask(s string) tcell.AttrMask {
  136. switch s {
  137. case "bold":
  138. return tcell.AttrBold
  139. case "blink":
  140. return tcell.AttrBlink
  141. case "reverse":
  142. return tcell.AttrReverse
  143. case "dim":
  144. return tcell.AttrDim
  145. case "italic":
  146. return tcell.AttrItalic
  147. case "strikethrough":
  148. return tcell.AttrStrikeThrough
  149. default:
  150. return tcell.AttrNone
  151. }
  152. }