theme.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package config
  2. import (
  3. "errors"
  4. "github.com/ayn2op/tview"
  5. "github.com/gdamore/tcell/v2"
  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 (
  69. ThemeStyle struct {
  70. NormalStyle StyleWrapper `toml:"normal_style"`
  71. ActiveStyle StyleWrapper `toml:"active_style"`
  72. }
  73. GuildsTreeTheme struct {
  74. AutoExpandFolders bool `toml:"auto_expand_folders"`
  75. Graphics bool `toml:"graphics"`
  76. GraphicsColor string `toml:"graphics_color"`
  77. PrivateChannelStyle StyleWrapper `toml:"private_channel_style"`
  78. GuildStyle StyleWrapper `toml:"guild_style"`
  79. ChannelStyle StyleWrapper `toml:"channel_style"`
  80. }
  81. MessagesListTheme struct {
  82. ReplyIndicator string `toml:"reply_indicator"`
  83. ForwardedIndicator string `toml:"forwarded_indicator"`
  84. AuthorStyle StyleWrapper `toml:"author_style"`
  85. MentionStyle StyleWrapper `toml:"mention_style"`
  86. EmojiStyle StyleWrapper `toml:"emoji_style"`
  87. URLStyle StyleWrapper `toml:"url_style"`
  88. AttachmentStyle StyleWrapper `toml:"attachment_style"`
  89. }
  90. MentionsListTheme struct {
  91. MinWidth uint `toml:"min_width"`
  92. MaxHeight uint `toml:"max_height"`
  93. }
  94. Theme struct {
  95. BackgroundColor string `toml:"background_color"`
  96. Title TitleTheme `toml:"title"`
  97. Border BorderTheme `toml:"border"`
  98. GuildsTree GuildsTreeTheme `toml:"guilds_tree"`
  99. MessagesList MessagesListTheme `toml:"messages_list"`
  100. MentionsList MentionsListTheme `toml:"mentions_list"`
  101. }
  102. )
  103. func stringToAttrMask(s string) tcell.AttrMask {
  104. switch s {
  105. case "bold":
  106. return tcell.AttrBold
  107. case "blink":
  108. return tcell.AttrBlink
  109. case "reverse":
  110. return tcell.AttrReverse
  111. case "underline":
  112. return tcell.AttrUnderline
  113. case "dim":
  114. return tcell.AttrDim
  115. case "italic":
  116. return tcell.AttrItalic
  117. case "strikethrough":
  118. return tcell.AttrStrikeThrough
  119. default:
  120. return tcell.AttrNone
  121. }
  122. }