From c2bcfd1f73dda97bd3e209c89e2abfe588f0977c Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 26 Sep 2024 16:32:55 -0500 Subject: [PATCH] add colorsystem detection --- src/hwylterm/bbansi/utils.nim | 22 +++++++++++++++++++--- todo.md | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/hwylterm/bbansi/utils.nim b/src/hwylterm/bbansi/utils.nim index 542c92d..7c233e9 100644 --- a/src/hwylterm/bbansi/utils.nim +++ b/src/hwylterm/bbansi/utils.nim @@ -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() diff --git a/todo.md b/todo.md index a668944..e4ab2f1 100644 --- a/todo.md +++ b/todo.md @@ -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)