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 c0fd30f commit 8b73e82
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"
)

// DetectColorProfile returns the color profile based on the terminal output,
Expand Down Expand Up @@ -133,6 +134,29 @@ func envColorProfile(env map[string]string) (p Profile) {
setProfile(ANSI)
}

ti, _ := terminfo.Load(term)
if ti != nil {

Check failure on line 138 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 138 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 138 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 150 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 150 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 152 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 152 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 154 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 154 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 8b73e82

Please sign in to comment.