about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-27 16:13:41 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-27 16:15:31 +0100
commit52c415762fda7b9369ed4cf88783a6639574e3ea (patch)
treec90c3dc079027154e55a8843286125c6b3b0426e
parentf2462f43427f56bb8452f7ee9dd0c93c7ed1fc46 (diff)
downloadchawan-52c415762fda7b9369ed4cf88783a6639574e3ea.tar.gz
term: improve color detection
some terminals (alacritty) don't support XTGETTCAP and don't even
respond with ANSI color support to DA1, so just fall back to termcap.
-rw-r--r--src/local/term.nim21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/local/term.nim b/src/local/term.nim
index 64898702..29a0a68b 100644
--- a/src/local/term.nim
+++ b/src/local/term.nim
@@ -43,10 +43,14 @@ type
     vi # make cursor invisible
     ve # reset cursor to normal
 
+  TermcapCapNumeric = enum
+    Co # color?
+
   Termcap = ref object
     bp: array[1024, uint8]
     funcstr: array[256, uint8]
     caps: array[TermcapCap, cstring]
+    numCaps: array[TermcapCapNumeric, cint]
 
   Terminal* = ref TerminalObj
   TerminalObj = object
@@ -646,6 +650,8 @@ when termcap_found:
       term.tc = tc
       for id in TermcapCap:
         tc.caps[id] = tgetstr(cstring($id), cast[ptr cstring](addr tc.funcstr))
+      for id in TermcapCapNumeric:
+        tc.numCaps[id] = tgetnum(cstring($id))
     else:
       raise newException(Defect, "Failed to load termcap description for terminal " & term.tname)
 
@@ -844,15 +850,20 @@ proc detectTermAttributes(term: Terminal, windowOnly: bool): TermStartResult =
     term.loadTermcap()
     if term.tc != nil:
       term.smcup = term.hascap ti
-      if term.hascap(ZH):
+      if term.colormode < cmEightBit and term.tc.numCaps[Co] == 256:
+        # due to termcap limitations, 256 is the highest possible number here
+        term.colormode = cmEightBit
+      elif term.colormode < cmANSI and term.tc.numCaps[Co] >= 8:
+        term.colormode = cmANSI
+      if term.hascap ZH:
         term.formatmode.incl(ffItalic)
-      if term.hascap(us):
+      if term.hascap us:
         term.formatmode.incl(ffUnderline)
-      if term.hascap(md):
+      if term.hascap md:
         term.formatmode.incl(ffBold)
-      if term.hascap(mr):
+      if term.hascap mr:
         term.formatmode.incl(ffReverse)
-      if term.hascap(mb):
+      if term.hascap mb:
         term.formatmode.incl(ffBlink)
   else:
     term.smcup = true