shimmer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package util
  2. import (
  3. "math"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/charmbracelet/lipgloss/v2"
  8. "github.com/charmbracelet/lipgloss/v2/compat"
  9. "github.com/sst/opencode/internal/styles"
  10. )
  11. var shimmerStart = time.Now()
  12. // Shimmer renders text with a moving foreground highlight.
  13. // bg is the background color, dim is the base text color, bright is the highlight color.
  14. func Shimmer(s string, bg compat.AdaptiveColor, _ compat.AdaptiveColor, _ compat.AdaptiveColor) string {
  15. if s == "" {
  16. return ""
  17. }
  18. runes := []rune(s)
  19. n := len(runes)
  20. if n == 0 {
  21. return s
  22. }
  23. pad := 10
  24. period := float64(n + pad*2)
  25. sweep := 2.5
  26. elapsed := time.Since(shimmerStart).Seconds()
  27. pos := (math.Mod(elapsed, sweep) / sweep) * period
  28. half := 4.0
  29. type seg struct {
  30. useHex bool
  31. hex string
  32. bold bool
  33. faint bool
  34. text string
  35. }
  36. var segs []seg
  37. useHex := hasTrueColor()
  38. for i, r := range runes {
  39. ip := float64(i + pad)
  40. dist := math.Abs(ip - pos)
  41. t := 0.0
  42. if dist <= half {
  43. x := math.Pi * (dist / half)
  44. t = 0.5 * (1.0 + math.Cos(x))
  45. }
  46. // Cosine brightness: base + amp*t (quantized for grouping)
  47. base := 0.55
  48. amp := 0.45
  49. brightness := base
  50. if t > 0 {
  51. brightness = base + amp*t
  52. }
  53. lvl := int(math.Round(brightness * 255.0))
  54. if !useHex {
  55. step := 24 // ~11 steps across range for non-truecolor
  56. lvl = int(math.Round(float64(lvl)/float64(step))) * step
  57. }
  58. bold := lvl >= 208
  59. faint := lvl <= 128
  60. // truecolor if possible; else fallback to modifiers only
  61. hex := ""
  62. if useHex {
  63. if lvl < 0 {
  64. lvl = 0
  65. }
  66. if lvl > 255 {
  67. lvl = 255
  68. }
  69. hex = rgbHex(lvl, lvl, lvl)
  70. }
  71. if len(segs) == 0 {
  72. segs = append(segs, seg{useHex: useHex, hex: hex, bold: bold, faint: faint, text: string(r)})
  73. } else {
  74. last := &segs[len(segs)-1]
  75. if last.useHex == useHex && last.hex == hex && last.bold == bold && last.faint == faint {
  76. last.text += string(r)
  77. } else {
  78. segs = append(segs, seg{useHex: useHex, hex: hex, bold: bold, faint: faint, text: string(r)})
  79. }
  80. }
  81. }
  82. var b strings.Builder
  83. for _, g := range segs {
  84. st := styles.NewStyle().Background(bg)
  85. if g.useHex && g.hex != "" {
  86. c := compat.AdaptiveColor{Dark: lipgloss.Color(g.hex), Light: lipgloss.Color(g.hex)}
  87. st = st.Foreground(c)
  88. }
  89. if g.bold {
  90. st = st.Bold(true)
  91. }
  92. if g.faint {
  93. st = st.Faint(true)
  94. }
  95. b.WriteString(st.Render(g.text))
  96. }
  97. return b.String()
  98. }
  99. func hasTrueColor() bool {
  100. c := strings.ToLower(os.Getenv("COLORTERM"))
  101. return strings.Contains(c, "truecolor") || strings.Contains(c, "24bit")
  102. }
  103. func rgbHex(r, g, b int) string {
  104. if r < 0 {
  105. r = 0
  106. }
  107. if r > 255 {
  108. r = 255
  109. }
  110. if g < 0 {
  111. g = 0
  112. }
  113. if g > 255 {
  114. g = 255
  115. }
  116. if b < 0 {
  117. b = 0
  118. }
  119. if b > 255 {
  120. b = 255
  121. }
  122. return "#" + hex2(r) + hex2(g) + hex2(b)
  123. }
  124. func hex2(v int) string {
  125. const digits = "0123456789abcdef"
  126. return string([]byte{digits[(v>>4)&0xF], digits[v&0xF]})
  127. }