renderer.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package lipgloss
  2. import (
  3. "io"
  4. "github.com/muesli/termenv"
  5. )
  6. // We're manually creating the struct here to avoid initializing the output and
  7. // query the terminal multiple times.
  8. var renderer = &Renderer{
  9. output: termenv.DefaultOutput(),
  10. }
  11. // Renderer is a lipgloss terminal renderer.
  12. type Renderer struct {
  13. output *termenv.Output
  14. hasDarkBackground *bool
  15. }
  16. // RendererOption is a function that can be used to configure a [Renderer].
  17. type RendererOption func(r *Renderer)
  18. // DefaultRenderer returns the default renderer.
  19. func DefaultRenderer() *Renderer {
  20. return renderer
  21. }
  22. // SetDefaultRenderer sets the default global renderer.
  23. func SetDefaultRenderer(r *Renderer) {
  24. renderer = r
  25. }
  26. // NewRenderer creates a new Renderer.
  27. //
  28. // w will be used to determine the terminal's color capabilities.
  29. func NewRenderer(w io.Writer, opts ...termenv.OutputOption) *Renderer {
  30. r := &Renderer{
  31. output: termenv.NewOutput(w, opts...),
  32. }
  33. return r
  34. }
  35. // Output returns the termenv output.
  36. func (r *Renderer) Output() *termenv.Output {
  37. return r.output
  38. }
  39. // SetOutput sets the termenv output.
  40. func (r *Renderer) SetOutput(o *termenv.Output) {
  41. r.output = o
  42. }
  43. // ColorProfile returns the detected termenv color profile.
  44. func (r *Renderer) ColorProfile() termenv.Profile {
  45. return r.output.Profile
  46. }
  47. // ColorProfile returns the detected termenv color profile.
  48. func ColorProfile() termenv.Profile {
  49. return renderer.ColorProfile()
  50. }
  51. // SetColorProfile sets the color profile on the renderer. This function exists
  52. // mostly for testing purposes so that you can assure you're testing against
  53. // a specific profile.
  54. //
  55. // Outside of testing you likely won't want to use this function as the color
  56. // profile will detect and cache the terminal's color capabilities and choose
  57. // the best available profile.
  58. //
  59. // Available color profiles are:
  60. //
  61. // termenv.Ascii // no color, 1-bit
  62. // termenv.ANSI //16 colors, 4-bit
  63. // termenv.ANSI256 // 256 colors, 8-bit
  64. // termenv.TrueColor // 16,777,216 colors, 24-bit
  65. //
  66. // This function is thread-safe.
  67. func (r *Renderer) SetColorProfile(p termenv.Profile) {
  68. r.output.Profile = p
  69. }
  70. // SetColorProfile sets the color profile on the default renderer. This
  71. // function exists mostly for testing purposes so that you can assure you're
  72. // testing against a specific profile.
  73. //
  74. // Outside of testing you likely won't want to use this function as the color
  75. // profile will detect and cache the terminal's color capabilities and choose
  76. // the best available profile.
  77. //
  78. // Available color profiles are:
  79. //
  80. // termenv.Ascii // no color, 1-bit
  81. // termenv.ANSI //16 colors, 4-bit
  82. // termenv.ANSI256 // 256 colors, 8-bit
  83. // termenv.TrueColor // 16,777,216 colors, 24-bit
  84. //
  85. // This function is thread-safe.
  86. func SetColorProfile(p termenv.Profile) {
  87. renderer.SetColorProfile(p)
  88. }
  89. // HasDarkBackground returns whether or not the terminal has a dark background.
  90. func HasDarkBackground() bool {
  91. return renderer.HasDarkBackground()
  92. }
  93. // HasDarkBackground returns whether or not the renderer will render to a dark
  94. // background. A dark background can either be auto-detected, or set explicitly
  95. // on the renderer.
  96. func (r *Renderer) HasDarkBackground() bool {
  97. if r.hasDarkBackground != nil {
  98. return *r.hasDarkBackground
  99. }
  100. return r.output.HasDarkBackground()
  101. }
  102. // SetHasDarkBackground sets the background color detection value for the
  103. // default renderer. This function exists mostly for testing purposes so that
  104. // you can assure you're testing against a specific background color setting.
  105. //
  106. // Outside of testing you likely won't want to use this function as the
  107. // backgrounds value will be automatically detected and cached against the
  108. // terminal's current background color setting.
  109. //
  110. // This function is thread-safe.
  111. func SetHasDarkBackground(b bool) {
  112. renderer.SetHasDarkBackground(b)
  113. }
  114. // SetHasDarkBackground sets the background color detection value on the
  115. // renderer. This function exists mostly for testing purposes so that you can
  116. // assure you're testing against a specific background color setting.
  117. //
  118. // Outside of testing you likely won't want to use this function as the
  119. // backgrounds value will be automatically detected and cached against the
  120. // terminal's current background color setting.
  121. //
  122. // This function is thread-safe.
  123. func (r *Renderer) SetHasDarkBackground(b bool) {
  124. r.hasDarkBackground = &b
  125. }