style.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2022 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcell
  15. // Style represents a complete text style, including both foreground color,
  16. // background color, and additional attributes such as "bold" or "underline".
  17. //
  18. // Note that not all terminals can display all colors or attributes, and
  19. // many might have specific incompatibilities between specific attributes
  20. // and color combinations.
  21. //
  22. // To use Style, just declare a variable of its type.
  23. type Style struct {
  24. fg Color
  25. bg Color
  26. attrs AttrMask
  27. url string
  28. urlId string
  29. }
  30. // StyleDefault represents a default style, based upon the context.
  31. // It is the zero value.
  32. var StyleDefault Style
  33. // styleInvalid is just an arbitrary invalid style used internally.
  34. var styleInvalid = Style{attrs: AttrInvalid}
  35. // Foreground returns a new style based on s, with the foreground color set
  36. // as requested. ColorDefault can be used to select the global default.
  37. func (s Style) Foreground(c Color) Style {
  38. return Style{
  39. fg: c,
  40. bg: s.bg,
  41. attrs: s.attrs,
  42. url: s.url,
  43. urlId: s.urlId,
  44. }
  45. }
  46. // Background returns a new style based on s, with the background color set
  47. // as requested. ColorDefault can be used to select the global default.
  48. func (s Style) Background(c Color) Style {
  49. return Style{
  50. fg: s.fg,
  51. bg: c,
  52. attrs: s.attrs,
  53. url: s.url,
  54. urlId: s.urlId,
  55. }
  56. }
  57. // Decompose breaks a style up, returning the foreground, background,
  58. // and other attributes. The URL if set is not included.
  59. func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
  60. return s.fg, s.bg, s.attrs
  61. }
  62. func (s Style) setAttrs(attrs AttrMask, on bool) Style {
  63. if on {
  64. return Style{
  65. fg: s.fg,
  66. bg: s.bg,
  67. attrs: s.attrs | attrs,
  68. url: s.url,
  69. urlId: s.urlId,
  70. }
  71. }
  72. return Style{
  73. fg: s.fg,
  74. bg: s.bg,
  75. attrs: s.attrs &^ attrs,
  76. url: s.url,
  77. urlId: s.urlId,
  78. }
  79. }
  80. // Normal returns the style with all attributes disabled.
  81. func (s Style) Normal() Style {
  82. return Style{
  83. fg: s.fg,
  84. bg: s.bg,
  85. }
  86. }
  87. // Bold returns a new style based on s, with the bold attribute set
  88. // as requested.
  89. func (s Style) Bold(on bool) Style {
  90. return s.setAttrs(AttrBold, on)
  91. }
  92. // Blink returns a new style based on s, with the blink attribute set
  93. // as requested.
  94. func (s Style) Blink(on bool) Style {
  95. return s.setAttrs(AttrBlink, on)
  96. }
  97. // Dim returns a new style based on s, with the dim attribute set
  98. // as requested.
  99. func (s Style) Dim(on bool) Style {
  100. return s.setAttrs(AttrDim, on)
  101. }
  102. // Italic returns a new style based on s, with the italic attribute set
  103. // as requested.
  104. func (s Style) Italic(on bool) Style {
  105. return s.setAttrs(AttrItalic, on)
  106. }
  107. // Reverse returns a new style based on s, with the reverse attribute set
  108. // as requested. (Reverse usually changes the foreground and background
  109. // colors.)
  110. func (s Style) Reverse(on bool) Style {
  111. return s.setAttrs(AttrReverse, on)
  112. }
  113. // Underline returns a new style based on s, with the underline attribute set
  114. // as requested.
  115. func (s Style) Underline(on bool) Style {
  116. return s.setAttrs(AttrUnderline, on)
  117. }
  118. // StrikeThrough sets strikethrough mode.
  119. func (s Style) StrikeThrough(on bool) Style {
  120. return s.setAttrs(AttrStrikeThrough, on)
  121. }
  122. // Attributes returns a new style based on s, with its attributes set as
  123. // specified.
  124. func (s Style) Attributes(attrs AttrMask) Style {
  125. return Style{
  126. fg: s.fg,
  127. bg: s.bg,
  128. attrs: attrs,
  129. url: s.url,
  130. urlId: s.urlId,
  131. }
  132. }
  133. // Url returns a style with the Url set. If the provided Url is not empty,
  134. // and the terminal supports it, text will typically be marked up as a clickable
  135. // link to that Url. If the Url is empty, then this mode is turned off.
  136. func (s Style) Url(url string) Style {
  137. return Style{
  138. fg: s.fg,
  139. bg: s.bg,
  140. attrs: s.attrs,
  141. url: url,
  142. urlId: s.urlId,
  143. }
  144. }
  145. // UrlId returns a style with the UrlId set. If the provided UrlId is not empty,
  146. // any marked up Url with this style will be given the UrlId also. If the
  147. // terminal supports it, any text with the same UrlId will be grouped as if it
  148. // were one Url, even if it spans multiple lines.
  149. func (s Style) UrlId(id string) Style {
  150. return Style{
  151. fg: s.fg,
  152. bg: s.bg,
  153. attrs: s.attrs,
  154. url: s.url,
  155. urlId: "id=" + id,
  156. }
  157. }