From dd12773db8d97d0c07be9bb17d9db71f04678162 Mon Sep 17 00:00:00 2001 From: oir Date: Sat, 14 Dec 2024 11:11:08 -0500 Subject: [PATCH] Support dimmed text Thanks @oir --- ansidecoder/ansidecoder.go | 6 ++++ ansitosvg/ansisvg.go | 1 + cli/testdata/dim_bightblack.ansi | 1 + cli/testdata/dim_bightblack.svg | 24 +++++++++++++ .../dim_intense_background_invert.ansi | 1 + .../dim_intense_background_invert.svg | 35 +++++++++++++++++++ cli/testdata/dim_uv.ansi | 2 ++ cli/testdata/dim_uv.svg | 26 ++++++++++++++ svgscreen/svgscreen.go | 9 ++++- svgscreen/template.svg | 5 +++ 10 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 cli/testdata/dim_bightblack.ansi create mode 100644 cli/testdata/dim_bightblack.svg create mode 100644 cli/testdata/dim_intense_background_invert.ansi create mode 100644 cli/testdata/dim_intense_background_invert.svg create mode 100644 cli/testdata/dim_uv.ansi create mode 100644 cli/testdata/dim_uv.svg diff --git a/ansidecoder/ansidecoder.go b/ansidecoder/ansidecoder.go index af64b31..03c95d5 100644 --- a/ansidecoder/ansidecoder.go +++ b/ansidecoder/ansidecoder.go @@ -32,6 +32,7 @@ func (cr codeRange) Is(c int) bool { var sgrReset = codeRange{0, 0} var sgrIncreaseIntensity = codeRange{1, 1} +var sgrDim = codeRange{2, 2} var sgrNormal = codeRange{22, 22} var sgrForeground = codeRange{30, 37} var sgrForegroundBright = codeRange{90, 97} @@ -78,6 +79,7 @@ type Decoder struct { Background Color Underline bool Intensity bool + Dim bool Invert bool Italic bool Strikethrough bool @@ -218,6 +220,7 @@ func (d *Decoder) ReadRune() (r rune, size int, err error) { d.Background = Color{N: -1} d.Underline = false d.Intensity = false + d.Dim = false d.Invert = false d.Italic = false d.Strikethrough = false @@ -225,6 +228,9 @@ func (d *Decoder) ReadRune() (r rune, size int, err error) { d.Intensity = true case sgrNormal.Is(n): d.Intensity = false + d.Dim = false + case sgrDim.Is(n): + d.Dim = true case sgrForeground.Is(n): d.Foreground = Color{N: n - 30} case sgrForegroundBright.Is(n): diff --git a/ansitosvg/ansisvg.go b/ansitosvg/ansisvg.go index 7cc3686..bb60cc2 100644 --- a/ansitosvg/ansisvg.go +++ b/ansitosvg/ansisvg.go @@ -74,6 +74,7 @@ func Convert(r io.Reader, w io.Writer, opts Options) error { Background: ad.Background.String(), Underline: ad.Underline, Intensity: ad.Intensity, + Dim: ad.Dim, Invert: ad.Invert, Italic: ad.Italic, Strikethrough: ad.Strikethrough, diff --git a/cli/testdata/dim_bightblack.ansi b/cli/testdata/dim_bightblack.ansi new file mode 100644 index 0000000..332bf7f --- /dev/null +++ b/cli/testdata/dim_bightblack.ansi @@ -0,0 +1 @@ +hello dim brightblack diff --git a/cli/testdata/dim_bightblack.svg b/cli/testdata/dim_bightblack.svg new file mode 100644 index 0000000..d77aa53 --- /dev/null +++ b/cli/testdata/dim_bightblack.svg @@ -0,0 +1,24 @@ + + + +hello dim brightblack + diff --git a/cli/testdata/dim_intense_background_invert.ansi b/cli/testdata/dim_intense_background_invert.ansi new file mode 100644 index 0000000..0f60b5a --- /dev/null +++ b/cli/testdata/dim_intense_background_invert.ansi @@ -0,0 +1 @@ +hello dim intense background inverted world diff --git a/cli/testdata/dim_intense_background_invert.svg b/cli/testdata/dim_intense_background_invert.svg new file mode 100644 index 0000000..60a4e36 --- /dev/null +++ b/cli/testdata/dim_intense_background_invert.svg @@ -0,0 +1,35 @@ + + + + + + + +hello dim intense background inverted world + diff --git a/cli/testdata/dim_uv.ansi b/cli/testdata/dim_uv.ansi new file mode 100644 index 0000000..6745004 --- /dev/null +++ b/cli/testdata/dim_uv.ansi @@ -0,0 +1,2 @@ +Using Python 3.11.10 environment at: env3.11 +Audited 1 package in 2ms diff --git a/cli/testdata/dim_uv.svg b/cli/testdata/dim_uv.svg new file mode 100644 index 0000000..80df639 --- /dev/null +++ b/cli/testdata/dim_uv.svg @@ -0,0 +1,26 @@ + + + +Using Python 3.11.10 environment at: env3.11 +Audited 1 package in 2ms + diff --git a/svgscreen/svgscreen.go b/svgscreen/svgscreen.go index a2f31df..ecac540 100644 --- a/svgscreen/svgscreen.go +++ b/svgscreen/svgscreen.go @@ -5,11 +5,12 @@ import ( _ "embed" "encoding/base64" "fmt" - "github.com/wader/ansisvg/svgscreen/xydim" "html/template" "io" "strconv" "strings" + + "github.com/wader/ansisvg/svgscreen/xydim" ) //go:embed template.svg @@ -22,6 +23,7 @@ type Char struct { Background string Underline bool Intensity bool + Dim bool Invert bool Italic bool Strikethrough bool @@ -69,6 +71,7 @@ type SvgDom struct { Italic bool Underline bool Strikethrough bool + Dim bool } } @@ -148,6 +151,10 @@ func (s *Screen) charToFgText(c Char) textSpan { classes = append(classes, "bold") s.Dom.ClassesUsed.Bold = true } + if c.Dim { + classes = append(classes, "dim") + s.Dom.ClassesUsed.Dim = true + } if c.Italic { classes = append(classes, "italic") s.Dom.ClassesUsed.Italic = true diff --git a/svgscreen/template.svg b/svgscreen/template.svg index cec9911..1348a77 100644 --- a/svgscreen/template.svg +++ b/svgscreen/template.svg @@ -53,6 +53,11 @@ whitespace only tspan:s preventing underline to be properly rendered. text-decoration: line-through; } {{- end}} +{{- if $.Dom.ClassesUsed.Dim}} + .dim { + opacity: 0.5; + } +{{- end}} {{- if anyColorUsed $.Background.ANSIUsed}} {{- end}}