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 bd3952e commit 9936326
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"
"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 {

Check failure on line 111 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if ti != nil` has complex nested blocks (complexity: 7) (nestif)

Check failure on line 111 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if ti != nil` has complex nested blocks (complexity: 7) (nestif)
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 {

Check failure on line 123 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x1000000, in <condition> detected (gomnd)

Check failure on line 123 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x1000000, in <condition> detected (gomnd)
setProfile(TrueColor)
} else if colors >= 0x100 {

Check failure on line 125 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x100, in <condition> detected (gomnd)

Check failure on line 125 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x100, in <condition> detected (gomnd)
setProfile(ANSI256)
} else if colors >= 0x10 {

Check failure on line 127 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x10, in <condition> detected (gomnd)

Check failure on line 127 in env.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0x10, in <condition> detected (gomnd)
setProfile(ANSI)
}
}
}

return
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -8,11 +8,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 9936326

Please sign in to comment.