add colorsystem detection

This commit is contained in:
Daylin Morgan 2024-09-26 16:32:55 -05:00
parent 5673b92863
commit c2bcfd1f73
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 20 additions and 4 deletions

View file

@ -6,6 +6,8 @@ import ./[styles, colors]
type
BbMode* = enum
On, NoColor, Off
ColorSystem = enum
TrueColor, EightBit, Standard, None
proc checkColorSupport(): BbMode =
when defined(bbansiOff):
@ -13,14 +15,28 @@ proc checkColorSupport(): BbMode =
when defined(bbansiNoColor):
return NoColor
else:
if os.getEnv("HWYLTERM_FORCE_COLOR") != "":
if getEnv("HWYLTERM_FORCE_COLOR") != "":
return On
if os.getEnv("NO_COLOR") != "":
elif getEnv("NO_COLOR") != "":
return NoColor
if not isatty(stdout):
elif (getEnv("TERM") in ["dumb", "unknown"]) or not isatty(stdout):
return Off
proc checkColorSystem(): ColorSystem =
let colorterm = getEnv("COLORTERM").strip().toLowerAscii()
if colorterm in ["truecolor", "24bit"]:
return TrueColor
let term = getEnv("TERM", "").strip().toLowerAscii()
let colors = term.split("-")[^1]
return
case colors:
of "kitty": EightBit
of "256color": EightBit
of "16color": Standard
else: Standard
let bbMode* = checkColorSupport()
let colorSystem* = checkColorSystem()
func firstCapital(s: string): string = s.toLowerAscii().capitalizeAscii()
func normalizeStyle(style: string): string = style.replace("_","").capitalizeAscii()

View file

@ -16,6 +16,6 @@
- [ ] add support for 256 and truecolors
- [ ] add support for rgb colors
- [ ] modify 256 colors w/parser changes to be `"[color(9)]red"` instead of `[9]red`
- [ ] improve color detection [ref](https://github.com/Textualize/rich/blob/4101991898ee7a09fe1706daca24af5e1e054862/rich/console.py#L791)
- [x] improve color detection [ref](https://github.com/Textualize/rich/blob/4101991898ee7a09fe1706daca24af5e1e054862/rich/console.py#L791)
<!-- generated with <3 by daylinmorgan/todo -->