From b7c85afc380fa31dd01cd90d799705ccb6db754b Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 5 Jan 2020 21:21:48 -0500 Subject: [PATCH] Detect true color support for dynamic terminfo Ref zyedidia/micro#1452 --- terminfo/dynamic/dynamic.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/terminfo/dynamic/dynamic.go b/terminfo/dynamic/dynamic.go index f4a0eaa0..87564855 100644 --- a/terminfo/dynamic/dynamic.go +++ b/terminfo/dynamic/dynamic.go @@ -24,6 +24,7 @@ package dynamic import ( "bytes" "errors" + "os" "os/exec" "regexp" "strconv" @@ -421,5 +422,33 @@ func LoadTerminfo(name string) (*terminfo.Terminfo, string, error) { t.SetFgBg = fg + ";" + bg } + addtruecolor := false + switch os.Getenv("COLORTERM") { + case "truecolor", "24bit", "24-bit": + addtruecolor = true + } + switch os.Getenv("TCELL_TRUECOLOR") { + case "": + case "disable": + addtruecolor = false + default: + addtruecolor = true + } + + // If the user has requested 24-bit color with $COLORTERM, then + // amend the value (unless already present). This means we don't + // need to have a value present. + if addtruecolor && + t.SetFgBgRGB == "" && + t.SetFgRGB == "" && + t.SetBgRGB == "" { + + // Supply vanilla ISO 8613-6:1994 24-bit color sequences. + t.SetFgRGB = "\x1b[38;2;%p1%d;%p2%d;%p3%dm" + t.SetBgRGB = "\x1b[48;2;%p1%d;%p2%d;%p3%dm" + t.SetFgBgRGB = "\x1b[38;2;%p1%d;%p2%d;%p3%d;" + + "48;2;%p4%d;%p5%d;%p6%dm" + } + return t, tc.desc, nil }