From 21ff27725b69bc947c82f4f2ebc0c88453f9db0c Mon Sep 17 00:00:00 2001 From: Dan Shick Date: Wed, 7 Jan 2026 00:34:43 -0500 Subject: [PATCH] fix(svg.go): don't drop explicit colors when settings a foregroundColorOverride this change keeps all custom color classes in place when setting a foregroundColorOverride and chooses instead to style all vt10x.DefaultFG colored text uniformly using a text element selector. the DefaultFG color is set to the result of GetColor(vt10x.DefaultFG) which gives ANSI color LightGrey. this is the same as the previous implicit behavior regardless of whether or not that was intended. if a foregroundColorOverride is set, that color is used instead. this better matches the behavior of the backgroundColorOverride which still respects explicit CSI SGR signals while changing the DefaultBG where it is encountered. --- internal/svg/svg.go | 24 ++++++++++++------- internal/svg/testdata/TestExportOutput.golden | 12 +++++----- .../testdata/TestExportOutputNoWindow.golden | 12 +++++----- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/internal/svg/svg.go b/internal/svg/svg.go index 26d4efa..8410924 100644 --- a/internal/svg/svg.go +++ b/internal/svg/svg.go @@ -94,11 +94,12 @@ func parseCast(c *Canvas) { } func (c *Canvas) getColors(cell vt10x.Glyph) { - fg := color.GetColor(cell.FG) - - if _, ok := c.colors[fg]; !ok { - c.colors[fg] = c.id.String() - c.id.Next() + if cell.FG != vt10x.DefaultFG { + fg := color.GetColor(cell.FG) + if _, ok := c.colors[fg]; !ok { + c.colors[fg] = c.id.String() + c.id.Next() + } } if cell.BG != vt10x.DefaultBG { @@ -158,10 +159,11 @@ func (c *Canvas) addStyles() { styles += css.Block{Selector: ".dim", Rules: css.Rules{"opacity": "0.5"}}.String() // If custom colors have been provided, use them instead if foregroundColorOverride != "" { - styles += fmt.Sprintf(".a{fill:%s}", foregroundColorOverride) + styles += css.Block{Selector: "text", Rules: css.Rules{"fill": foregroundColorOverride}}.String() } else { - styles += colors.String() + styles += css.Block{Selector: "text", Rules: css.Rules{"fill": color.GetColor(vt10x.DefaultFG)}}.String() } + styles += colors.String() c.Style("text/css", styles) } @@ -222,7 +224,8 @@ func (c *Canvas) renderRow(term vt10x.Terminal, row int) { } if strings.TrimSpace(frame) != "" { - c.Text(lastColummn*colWidth, row*rowHeight, frame, fmt.Sprintf(`class="%s"`, c.colors[color.GetColor(lastColor)])) + colorClass := c.buildClassString(lastColor, false, false, false, false) + c.Text(lastColummn*colWidth, row*rowHeight, frame, colorClass) } } @@ -235,7 +238,10 @@ func (c *Canvas) cellAttributesChanged( } func (c *Canvas) buildClassString(fgColor vt10x.Color, bold, italic, underline, dim bool) string { - class := c.colors[color.GetColor(fgColor)] + class := "" + if fgColor != vt10x.DefaultFG { + class = c.colors[color.GetColor(fgColor)] + } if bold { class += " bold" } diff --git a/internal/svg/testdata/TestExportOutput.golden b/internal/svg/testdata/TestExportOutput.golden index 58875e7..426aed3 100644 --- a/internal/svg/testdata/TestExportOutput.golden +++ b/internal/svg/testdata/TestExportOutput.golden @@ -11,23 +11,23 @@ -h +h -he +he -hel +hel -hell +hell -hello +hello diff --git a/internal/svg/testdata/TestExportOutputNoWindow.golden b/internal/svg/testdata/TestExportOutputNoWindow.golden index 1d0751a..c7535a6 100644 --- a/internal/svg/testdata/TestExportOutputNoWindow.golden +++ b/internal/svg/testdata/TestExportOutputNoWindow.golden @@ -8,23 +8,23 @@ -h +h -he +he -hel +hel -hell +hell -hello +hello