[PATCH] D125914: [llvm/Support] Fallback to $TERM if terminfo has no "colors" capability

Med Ismail Bennani via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 11:33:16 PDT 2022


mib created this revision.
mib added a reviewer: JDevlieghere.
mib added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
mib requested review of this revision.
Herald added a subscriber: llvm-commits.

It can happen on macOS that the terminal doesn't report the "colors"
capability in the terminfo database, in which case `tigetnum` returns -1.

This doesn't mean however that the terminal doesn't supports color, it
just means that the capability is absent from the terminal description.

In that case, we should still fallback to the checking the $TERM
environment variable to see if it supports ANSI escapes codes.

Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125914

Files:
  llvm/lib/Support/Unix/Process.inc


Index: llvm/lib/Support/Unix/Process.inc
===================================================================
--- llvm/lib/Support/Unix/Process.inc
+++ llvm/lib/Support/Unix/Process.inc
@@ -331,6 +331,23 @@
 static ManagedStatic<std::mutex> TermColorMutex;
 #endif
 
+bool checkTerminalEnvironmentForColors() {
+  if (const char *TermStr = std::getenv("TERM")) {
+    return StringSwitch<bool>(TermStr)
+      .Case("ansi", true)
+      .Case("cygwin", true)
+      .Case("linux", true)
+      .StartsWith("screen", true)
+      .StartsWith("xterm", true)
+      .StartsWith("vt100", true)
+      .StartsWith("rxvt", true)
+      .EndsWith("color", true)
+      .Default(false);
+  }
+
+  return false;
+}
+
 static bool terminalHasColors(int fd) {
 #ifdef LLVM_ENABLE_TERMINFO
   // First, acquire a global lock because these C routines are thread hostile.
@@ -356,7 +373,8 @@
   //
   // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if
   // the terminfo says that no colors are supported.
-  bool HasColors = tigetnum(const_cast<char *>("colors")) > 0;
+  int colors_ti = tigetnum(const_cast<char *>("colors"));
+  bool HasColors = colors_ti >= 0 ? colors_ti : checkTerminalEnvironmentForColors();
 
   // Now extract the structure allocated by setupterm and free its memory
   // through a really silly dance.
@@ -364,27 +382,12 @@
   (void)del_curterm(termp); // Drop any errors here.
 
   // Return true if we found a color capabilities for the current terminal.
-  if (HasColors)
-    return true;
+  return HasColors;
 #else
   // When the terminfo database is not available, check if the current terminal
   // is one of terminals that are known to support ANSI color escape codes.
-  if (const char *TermStr = std::getenv("TERM")) {
-    return StringSwitch<bool>(TermStr)
-      .Case("ansi", true)
-      .Case("cygwin", true)
-      .Case("linux", true)
-      .StartsWith("screen", true)
-      .StartsWith("xterm", true)
-      .StartsWith("vt100", true)
-      .StartsWith("rxvt", true)
-      .EndsWith("color", true)
-      .Default(false);
-  }
+  return checkTerminalEnvironmentForColors();
 #endif
-
-  // Otherwise, be conservative.
-  return false;
 }
 
 bool Process::FileDescriptorHasColors(int fd) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125914.430463.patch
Type: text/x-patch
Size: 2250 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220518/c81c374b/attachment.bin>


More information about the llvm-commits mailing list