From 798a734ec37d7ee12003f32cf2b0a9668c2c351f Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 14 Mar 2024 22:10:44 -0400 Subject: [PATCH] feat: support inferring the color profile from terminfo dbs This will try to load the terminal terminfo database and infer the color profile supported by the terminal. --- env.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/env.go b/env.go index aad1ae12..bfa6e9af 100644 --- a/env.go +++ b/env.go @@ -2,6 +2,8 @@ package lipgloss import ( "strings" + + "github.com/xo/terminfo" ) // envNoColor returns true if the environment variables explicitly disable color output @@ -92,5 +94,28 @@ func (o *Renderer) detectColorProfile() (p Profile) { setProfile(ANSI) } + ti, _ := terminfo.Load(term) + if ti != nil { + extbools := ti.ExtBoolCapsShort() + if _, ok := extbools["RGB"]; ok { + setProfile(TrueColor) + } + + if _, ok := extbools["Tc"]; ok { + setProfile(TrueColor) + } + + nums := ti.NumCapsShort() + if colors, ok := nums["colors"]; ok { + if colors >= 0x1000000 { + setProfile(TrueColor) + } else if colors >= 0x100 { + setProfile(ANSI256) + } else if colors >= 0x10 { + setProfile(ANSI) + } + } + } + return }