utilities.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package styles
  2. import (
  3. "image/color"
  4. "github.com/charmbracelet/lipgloss/v2"
  5. "github.com/charmbracelet/lipgloss/v2/compat"
  6. )
  7. // IsNoColor checks if a color is the special NoColor type
  8. func IsNoColor(c color.Color) bool {
  9. _, ok := c.(lipgloss.NoColor)
  10. return ok
  11. }
  12. // Style wraps lipgloss.Style to provide a fluent API for handling "none" colors
  13. type Style struct {
  14. lipgloss.Style
  15. }
  16. // NewStyle creates a new Style with proper handling of "none" colors
  17. func NewStyle() Style {
  18. return Style{lipgloss.NewStyle()}
  19. }
  20. func (s Style) Lipgloss() lipgloss.Style {
  21. return s.Style
  22. }
  23. // Foreground sets the foreground color, handling "none" appropriately
  24. func (s Style) Foreground(c compat.AdaptiveColor) Style {
  25. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  26. return Style{s.Style.UnsetForeground()}
  27. }
  28. return Style{s.Style.Foreground(c)}
  29. }
  30. // Background sets the background color, handling "none" appropriately
  31. func (s Style) Background(c compat.AdaptiveColor) Style {
  32. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  33. return Style{s.Style.UnsetBackground()}
  34. }
  35. return Style{s.Style.Background(c)}
  36. }
  37. // BorderForeground sets the border foreground color, handling "none" appropriately
  38. func (s Style) BorderForeground(c compat.AdaptiveColor) Style {
  39. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  40. return Style{s.Style.UnsetBorderForeground()}
  41. }
  42. return Style{s.Style.BorderForeground(c)}
  43. }
  44. // BorderBackground sets the border background color, handling "none" appropriately
  45. func (s Style) BorderBackground(c compat.AdaptiveColor) Style {
  46. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  47. return Style{s.Style.UnsetBorderBackground()}
  48. }
  49. return Style{s.Style.BorderBackground(c)}
  50. }
  51. // BorderTopForeground sets the border top foreground color, handling "none" appropriately
  52. func (s Style) BorderTopForeground(c compat.AdaptiveColor) Style {
  53. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  54. return Style{s.Style.UnsetBorderTopForeground()}
  55. }
  56. return Style{s.Style.BorderTopForeground(c)}
  57. }
  58. // BorderTopBackground sets the border top background color, handling "none" appropriately
  59. func (s Style) BorderTopBackground(c compat.AdaptiveColor) Style {
  60. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  61. return Style{s.Style.UnsetBorderTopBackground()}
  62. }
  63. return Style{s.Style.BorderTopBackground(c)}
  64. }
  65. // BorderBottomForeground sets the border bottom foreground color, handling "none" appropriately
  66. func (s Style) BorderBottomForeground(c compat.AdaptiveColor) Style {
  67. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  68. return Style{s.Style.UnsetBorderBottomForeground()}
  69. }
  70. return Style{s.Style.BorderBottomForeground(c)}
  71. }
  72. // BorderBottomBackground sets the border bottom background color, handling "none" appropriately
  73. func (s Style) BorderBottomBackground(c compat.AdaptiveColor) Style {
  74. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  75. return Style{s.Style.UnsetBorderBottomBackground()}
  76. }
  77. return Style{s.Style.BorderBottomBackground(c)}
  78. }
  79. // BorderLeftForeground sets the border left foreground color, handling "none" appropriately
  80. func (s Style) BorderLeftForeground(c compat.AdaptiveColor) Style {
  81. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  82. return Style{s.Style.UnsetBorderLeftForeground()}
  83. }
  84. return Style{s.Style.BorderLeftForeground(c)}
  85. }
  86. // BorderLeftBackground sets the border left background color, handling "none" appropriately
  87. func (s Style) BorderLeftBackground(c compat.AdaptiveColor) Style {
  88. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  89. return Style{s.Style.UnsetBorderLeftBackground()}
  90. }
  91. return Style{s.Style.BorderLeftBackground(c)}
  92. }
  93. // BorderRightForeground sets the border right foreground color, handling "none" appropriately
  94. func (s Style) BorderRightForeground(c compat.AdaptiveColor) Style {
  95. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  96. return Style{s.Style.UnsetBorderRightForeground()}
  97. }
  98. return Style{s.Style.BorderRightForeground(c)}
  99. }
  100. // BorderRightBackground sets the border right background color, handling "none" appropriately
  101. func (s Style) BorderRightBackground(c compat.AdaptiveColor) Style {
  102. if IsNoColor(c.Dark) && IsNoColor(c.Light) {
  103. return Style{s.Style.UnsetBorderRightBackground()}
  104. }
  105. return Style{s.Style.BorderRightBackground(c)}
  106. }
  107. // Render applies the style to a string
  108. func (s Style) Render(str string) string {
  109. return s.Style.Render(str)
  110. }
  111. // Common lipgloss.Style method delegations for seamless usage
  112. func (s Style) Bold(v bool) Style {
  113. return Style{s.Style.Bold(v)}
  114. }
  115. func (s Style) Italic(v bool) Style {
  116. return Style{s.Style.Italic(v)}
  117. }
  118. func (s Style) Underline(v bool) Style {
  119. return Style{s.Style.Underline(v)}
  120. }
  121. func (s Style) Strikethrough(v bool) Style {
  122. return Style{s.Style.Strikethrough(v)}
  123. }
  124. func (s Style) Blink(v bool) Style {
  125. return Style{s.Style.Blink(v)}
  126. }
  127. func (s Style) Faint(v bool) Style {
  128. return Style{s.Style.Faint(v)}
  129. }
  130. func (s Style) Reverse(v bool) Style {
  131. return Style{s.Style.Reverse(v)}
  132. }
  133. func (s Style) Width(i int) Style {
  134. return Style{s.Style.Width(i)}
  135. }
  136. func (s Style) Height(i int) Style {
  137. return Style{s.Style.Height(i)}
  138. }
  139. func (s Style) Padding(i ...int) Style {
  140. return Style{s.Style.Padding(i...)}
  141. }
  142. func (s Style) PaddingTop(i int) Style {
  143. return Style{s.Style.PaddingTop(i)}
  144. }
  145. func (s Style) PaddingBottom(i int) Style {
  146. return Style{s.Style.PaddingBottom(i)}
  147. }
  148. func (s Style) PaddingLeft(i int) Style {
  149. return Style{s.Style.PaddingLeft(i)}
  150. }
  151. func (s Style) PaddingRight(i int) Style {
  152. return Style{s.Style.PaddingRight(i)}
  153. }
  154. func (s Style) Margin(i ...int) Style {
  155. return Style{s.Style.Margin(i...)}
  156. }
  157. func (s Style) MarginTop(i int) Style {
  158. return Style{s.Style.MarginTop(i)}
  159. }
  160. func (s Style) MarginBottom(i int) Style {
  161. return Style{s.Style.MarginBottom(i)}
  162. }
  163. func (s Style) MarginLeft(i int) Style {
  164. return Style{s.Style.MarginLeft(i)}
  165. }
  166. func (s Style) MarginRight(i int) Style {
  167. return Style{s.Style.MarginRight(i)}
  168. }
  169. func (s Style) Border(b lipgloss.Border, sides ...bool) Style {
  170. return Style{s.Style.Border(b, sides...)}
  171. }
  172. func (s Style) BorderStyle(b lipgloss.Border) Style {
  173. return Style{s.Style.BorderStyle(b)}
  174. }
  175. func (s Style) BorderTop(v bool) Style {
  176. return Style{s.Style.BorderTop(v)}
  177. }
  178. func (s Style) BorderBottom(v bool) Style {
  179. return Style{s.Style.BorderBottom(v)}
  180. }
  181. func (s Style) BorderLeft(v bool) Style {
  182. return Style{s.Style.BorderLeft(v)}
  183. }
  184. func (s Style) BorderRight(v bool) Style {
  185. return Style{s.Style.BorderRight(v)}
  186. }
  187. func (s Style) Align(p ...lipgloss.Position) Style {
  188. return Style{s.Style.Align(p...)}
  189. }
  190. func (s Style) AlignHorizontal(p lipgloss.Position) Style {
  191. return Style{s.Style.AlignHorizontal(p)}
  192. }
  193. func (s Style) AlignVertical(p lipgloss.Position) Style {
  194. return Style{s.Style.AlignVertical(p)}
  195. }
  196. func (s Style) Inline(v bool) Style {
  197. return Style{s.Style.Inline(v)}
  198. }
  199. func (s Style) MaxWidth(n int) Style {
  200. return Style{s.Style.MaxWidth(n)}
  201. }
  202. func (s Style) MaxHeight(n int) Style {
  203. return Style{s.Style.MaxHeight(n)}
  204. }
  205. func (s Style) TabWidth(n int) Style {
  206. return Style{s.Style.TabWidth(n)}
  207. }
  208. func (s Style) UnsetBold() Style {
  209. return Style{s.Style.UnsetBold()}
  210. }
  211. func (s Style) UnsetItalic() Style {
  212. return Style{s.Style.UnsetItalic()}
  213. }
  214. func (s Style) UnsetUnderline() Style {
  215. return Style{s.Style.UnsetUnderline()}
  216. }
  217. func (s Style) UnsetStrikethrough() Style {
  218. return Style{s.Style.UnsetStrikethrough()}
  219. }
  220. func (s Style) UnsetBlink() Style {
  221. return Style{s.Style.UnsetBlink()}
  222. }
  223. func (s Style) UnsetFaint() Style {
  224. return Style{s.Style.UnsetFaint()}
  225. }
  226. func (s Style) UnsetReverse() Style {
  227. return Style{s.Style.UnsetReverse()}
  228. }
  229. func (s Style) Copy() Style {
  230. return Style{s.Style}
  231. }
  232. func (s Style) Inherit(i Style) Style {
  233. return Style{s.Style.Inherit(i.Style)}
  234. }