mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 00:47:15 -04:00
Construct one combined true-color ANSI sequence for foreground and background colors
This commit is contained in:
+37
-17
@@ -31,38 +31,57 @@ func color(c *string) (uint8, error) {
|
|||||||
return uint8(i), err
|
return uint8(i), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func colorSeq(c *string) (string, error) {
|
func colorSeq(fg *string, bg *string) (string, error) {
|
||||||
if c == nil || len(*c) == 0 {
|
fc := ""
|
||||||
|
bc := ""
|
||||||
|
if fg != nil {
|
||||||
|
fc = *fg
|
||||||
|
}
|
||||||
|
if bg != nil {
|
||||||
|
bc = *bg
|
||||||
|
}
|
||||||
|
|
||||||
|
fs := ""
|
||||||
|
bs := ""
|
||||||
|
f, err := colorful.Hex(fc)
|
||||||
|
if err == nil {
|
||||||
|
fs = fmt.Sprintf("38;2;%d;%d;%d", uint8(f.R*255), uint8(f.G*255), uint8(f.B*255))
|
||||||
|
}
|
||||||
|
b, err := colorful.Hex(bc)
|
||||||
|
if err == nil {
|
||||||
|
bs = fmt.Sprintf("48;2;%d;%d;%d", uint8(b.R*255), uint8(b.G*255), uint8(b.B*255))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fs) > 0 || len(bs) > 0 {
|
||||||
|
seq := "\x1b[" + fs
|
||||||
|
if len(fs) > 0 {
|
||||||
|
seq += ";"
|
||||||
|
}
|
||||||
|
return seq + bs + "m", nil
|
||||||
|
}
|
||||||
|
|
||||||
return "", errors.New("Invalid color")
|
return "", errors.New("Invalid color")
|
||||||
}
|
}
|
||||||
|
|
||||||
col, err := colorful.Hex(*c)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%d;%d;%dm", uint8(col.R*255), uint8(col.G*255), uint8(col.B*255)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func renderText(w io.Writer, rules ElementStyle, s string) {
|
func renderText(w io.Writer, rules ElementStyle, s string) {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
truecolor := os.Getenv("COLORTERM") == "truecolor"
|
||||||
// FIXME: ugly true-color ANSI support hack
|
// FIXME: ugly true-color ANSI support hack
|
||||||
if os.Getenv("COLORTERM") == "truecolor" {
|
if truecolor {
|
||||||
bg, err := colorSeq(rules.BackgroundColor)
|
seq, err := colorSeq(rules.Color, rules.BackgroundColor)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s = "\x1b[48;2;" + bg + s
|
s = seq + s
|
||||||
}
|
} else {
|
||||||
fg, err := colorSeq(rules.Color)
|
truecolor = false
|
||||||
if err == nil {
|
|
||||||
s = "\x1b[38;2;" + fg + s
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out := aurora.Reset(s)
|
out := aurora.Reset(s)
|
||||||
|
|
||||||
|
if !truecolor {
|
||||||
if rules.Color != nil {
|
if rules.Color != nil {
|
||||||
i, err := color(rules.Color)
|
i, err := color(rules.Color)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -75,6 +94,7 @@ func renderText(w io.Writer, rules ElementStyle, s string) {
|
|||||||
out = out.BgIndex(i)
|
out = out.BgIndex(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if rules.Underline != nil && *rules.Underline {
|
if rules.Underline != nil && *rules.Underline {
|
||||||
out = out.Underline()
|
out = out.Underline()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user