style.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package termenv
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/mattn/go-runewidth"
  6. )
  7. // Sequence definitions.
  8. const (
  9. ResetSeq = "0"
  10. BoldSeq = "1"
  11. FaintSeq = "2"
  12. ItalicSeq = "3"
  13. UnderlineSeq = "4"
  14. BlinkSeq = "5"
  15. ReverseSeq = "7"
  16. CrossOutSeq = "9"
  17. OverlineSeq = "53"
  18. )
  19. // Style is a string that various rendering styles can be applied to.
  20. type Style struct {
  21. profile Profile
  22. string
  23. styles []string
  24. }
  25. // String returns a new Style.
  26. func String(s ...string) Style {
  27. return Style{
  28. profile: ANSI,
  29. string: strings.Join(s, " "),
  30. }
  31. }
  32. func (t Style) String() string {
  33. return t.Styled(t.string)
  34. }
  35. // Styled renders s with all applied styles.
  36. func (t Style) Styled(s string) string {
  37. if t.profile == Ascii {
  38. return s
  39. }
  40. if len(t.styles) == 0 {
  41. return s
  42. }
  43. seq := strings.Join(t.styles, ";")
  44. if seq == "" {
  45. return s
  46. }
  47. return fmt.Sprintf("%s%sm%s%sm", CSI, seq, s, CSI+ResetSeq)
  48. }
  49. // Foreground sets a foreground color.
  50. func (t Style) Foreground(c Color) Style {
  51. if c != nil {
  52. t.styles = append(t.styles, c.Sequence(false))
  53. }
  54. return t
  55. }
  56. // Background sets a background color.
  57. func (t Style) Background(c Color) Style {
  58. if c != nil {
  59. t.styles = append(t.styles, c.Sequence(true))
  60. }
  61. return t
  62. }
  63. // Bold enables bold rendering.
  64. func (t Style) Bold() Style {
  65. t.styles = append(t.styles, BoldSeq)
  66. return t
  67. }
  68. // Faint enables faint rendering.
  69. func (t Style) Faint() Style {
  70. t.styles = append(t.styles, FaintSeq)
  71. return t
  72. }
  73. // Italic enables italic rendering.
  74. func (t Style) Italic() Style {
  75. t.styles = append(t.styles, ItalicSeq)
  76. return t
  77. }
  78. // Underline enables underline rendering.
  79. func (t Style) Underline() Style {
  80. t.styles = append(t.styles, UnderlineSeq)
  81. return t
  82. }
  83. // Overline enables overline rendering.
  84. func (t Style) Overline() Style {
  85. t.styles = append(t.styles, OverlineSeq)
  86. return t
  87. }
  88. // Blink enables blink mode.
  89. func (t Style) Blink() Style {
  90. t.styles = append(t.styles, BlinkSeq)
  91. return t
  92. }
  93. // Reverse enables reverse color mode.
  94. func (t Style) Reverse() Style {
  95. t.styles = append(t.styles, ReverseSeq)
  96. return t
  97. }
  98. // CrossOut enables crossed-out rendering.
  99. func (t Style) CrossOut() Style {
  100. t.styles = append(t.styles, CrossOutSeq)
  101. return t
  102. }
  103. // Width returns the width required to print all runes in Style.
  104. func (t Style) Width() int {
  105. return runewidth.StringWidth(t.string)
  106. }