Skip to content

Commit

Permalink
feat: support inferring the color profile from terminfo dbs
Browse files Browse the repository at this point in the history
This will try to load the terminal terminfo database and infer the color
profile supported by the terminal.
  • Loading branch information
aymanbagabas committed Apr 29, 2024
1 parent e1eb364 commit 4deccf6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions env.go
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/charmbracelet/x/exp/term"

Check failure on line 8 in env.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

github.com/charmbracelet/x/exp/term@v0.0.0-20240425164147-ba2a9512b05f: replacement directory ../x/exp/term does not exist

Check failure on line 8 in env.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

github.com/charmbracelet/x/exp/term@v0.0.0-20240425164147-ba2a9512b05f: replacement directory ../x/exp/term does not exist
"github.com/xo/terminfo"
)

// envNoColor returns true if the environment variables explicitly disable color output
Expand Down Expand Up @@ -106,6 +107,29 @@ func detectColorProfile(env map[string]string) (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
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -10,11 +10,11 @@ require (
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/rivo/uniseg v0.4.7
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
golang.org/x/sys v0.19.0
)

require (
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
)

0 comments on commit 4deccf6

Please sign in to comment.