@@ -28,8 +28,8 @@ var (
2828 hlCacheMu sync.RWMutex
2929 )
3030
31-func cachedHighlight(text, language, theme string) string {
32- cacheKey := language + ":" + theme + ":" + text
31+func cachedHighlight(text, language, theme, bgHex string) string {
32+ cacheKey := language + ":" + theme + ":" + bgHex + ":" + text
3333 hlCacheMu.RLock()
3434 if v, ok := hlCache[cacheKey]; ok {
3535 hlCacheMu.RUnlock()
@@ -55,7 +55,13 @@ func cachedHighlight(text, language, theme string) string {
5555 if err := fmtr.Format(&buf, sty, it); err != nil {
5656 return text
5757 }
58- result := strings.TrimRight(buf.String(), "\n")
58+ raw := strings.TrimRight(buf.String(), "\n")
59+
60+ // Chroma's terminal16m formatter skips bg on tokens that inherit from
61+ // the Background entry, and every \x1b[0m reset clears bg. Fix by
62+ // replacing resets with reset+bg so the background is always active.
63+ bgSeq := hexToBgSeq(bgHex)
64+ result := bgSeq + strings.ReplaceAll(raw, "\x1b[0m", "\x1b[0m"+bgSeq)
5965
6066 hlCacheMu.Lock()
6167 if len(hlCache) > 2000 {
@@ -66,6 +72,33 @@ func cachedHighlight(text, language, theme string) string {
6672 return result
6773 }
6874
75+// hexToBgSeq converts "#rrggbb" to an ANSI 24-bit background escape sequence.
76+func hexToBgSeq(hex string) string {
77+ if len(hex) != 7 || hex[0] != '#' {
78+ return ""
79+ }
80+ r := hexByte(hex[1], hex[2])
81+ g := hexByte(hex[3], hex[4])
82+ b := hexByte(hex[5], hex[6])
83+ return fmt.Sprintf("\x1b[48;2;%d;%d;%dm", r, g, b)
84+}
85+
86+func hexByte(hi, lo byte) int {
87+ return hexNibble(hi)<<4 | hexNibble(lo)
88+}
89+
90+func hexNibble(c byte) int {
91+ switch {
92+ case c >= '0' && c <= '9':
93+ return int(c - '0')
94+ case c >= 'a' && c <= 'f':
95+ return int(c-'a') + 10
96+ case c >= 'A' && c <= 'F':
97+ return int(c-'A') + 10
98+ }
99+ return 0
100+}
101+
69102 // themeBg extracts the background hex color from a Chroma style.
70103 // Returns "" if no background is set.
71104 func themeBg(theme string) string {
@@ -396,15 +429,20 @@ func (m *Model) expandedColToBufferCol(bufRow, expandedCol int) int {
396429 return len(line)
397430 }
398431
399-// bgForRender returns the background style. Extracts from syntax theme if
400-// available, falls back to BgColor.
401-func (m *Model) bgForRender() lipgloss.Style {
432+// bgHexForRender returns the background hex color. Prefers the syntax theme
433+// background, falls back to BgColor.
434+func (m *Model) bgHexForRender() string {
402435 if m.Language != "" && m.SyntaxTheme != "" {
403436 if hex := themeBg(m.SyntaxTheme); hex != "" {
404- return lipgloss.NewStyle().Background(lipgloss.Color(hex))
437+ return hex
405438 }
406439 }
407- return lipgloss.NewStyle().Background(m.BgColor)
440+ return string(m.BgColor)
441+}
442+
443+// bgForRender returns the background as a lipgloss style.
444+func (m *Model) bgForRender() lipgloss.Style {
445+ return lipgloss.NewStyle().Background(lipgloss.Color(m.bgHexForRender()))
408446 }
409447
410448 // ---------------------------------------------------------------------------
@@ -735,7 +773,8 @@ func (m Model) View() string {
735773 }
736774
737775 tw := m.textWidth()
738- bg := m.bgForRender()
776+ bgHex := m.bgHexForRender()
777+ bg := lipgloss.NewStyle().Background(lipgloss.Color(bgHex))
739778 lineNumSty := m.LineNumStyle.Background(bg.GetBackground())
740779
741780 // Build a flat list of visual rows from visible buffer lines.
@@ -768,7 +807,7 @@ func (m Model) View() string {
768807 // Highlight the full line once for all its segments.
769808 var fullHL string
770809 if hasSyntax {
771- fullHL = cachedHighlight(lineStr, m.Language, m.SyntaxTheme)
810+ fullHL = cachedHighlight(lineStr, m.Language, m.SyntaxTheme, bgHex)
772811 }
773812
774813 firstSub := 0