[llvm] b3718bc - [llvm/Support] Fallback to $TERM if terminfo has no "colors" capability
Med Ismail Bennani via llvm-commits
llvm-commits at lists.llvm.org
Wed May 18 18:22:57 PDT 2022
Author: Med Ismail Bennani
Date: 2022-05-18T18:22:46-07:00
New Revision: b3718bc4672bcad45f155c6d2f9fce4ad409bb8e
URL: https://github.com/llvm/llvm-project/commit/b3718bc4672bcad45f155c6d2f9fce4ad409bb8e
DIFF: https://github.com/llvm/llvm-project/commit/b3718bc4672bcad45f155c6d2f9fce4ad409bb8e.diff
LOG: [llvm/Support] Fallback to $TERM if terminfo has no "colors" capability
It can happen on macOS that 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.
Differential Revision: https://reviews.llvm.org/D125914
Signed-off-by: Med Ismail Bennani <medismail.bennani at gmail.com>
Added:
Modified:
llvm/lib/Support/Unix/Process.inc
Removed:
################################################################################
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc
index d3d9fb7d71878..3c2d118977c52 100644
--- a/llvm/lib/Support/Unix/Process.inc
+++ b/llvm/lib/Support/Unix/Process.inc
@@ -331,6 +331,23 @@ extern "C" int tigetnum(char *capname);
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 @@ static bool terminalHasColors(int fd) {
//
// 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 @@ static bool terminalHasColors(int fd) {
(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) {
More information about the llvm-commits
mailing list