Skip to content

Commit b3718bc

Browse files
committed
[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@gmail.com>
1 parent a5d618b commit b3718bc

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

llvm/lib/Support/Unix/Process.inc

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,23 @@ extern "C" int tigetnum(char *capname);
331331
static ManagedStatic<std::mutex> TermColorMutex;
332332
#endif
333333

334+
bool checkTerminalEnvironmentForColors() {
335+
if (const char *TermStr = std::getenv("TERM")) {
336+
return StringSwitch<bool>(TermStr)
337+
.Case("ansi", true)
338+
.Case("cygwin", true)
339+
.Case("linux", true)
340+
.StartsWith("screen", true)
341+
.StartsWith("xterm", true)
342+
.StartsWith("vt100", true)
343+
.StartsWith("rxvt", true)
344+
.EndsWith("color", true)
345+
.Default(false);
346+
}
347+
348+
return false;
349+
}
350+
334351
static bool terminalHasColors(int fd) {
335352
#ifdef LLVM_ENABLE_TERMINFO
336353
// First, acquire a global lock because these C routines are thread hostile.
@@ -356,35 +373,21 @@ static bool terminalHasColors(int fd) {
356373
//
357374
// The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if
358375
// the terminfo says that no colors are supported.
359-
bool HasColors = tigetnum(const_cast<char *>("colors")) > 0;
376+
int colors_ti = tigetnum(const_cast<char *>("colors"));
377+
bool HasColors = colors_ti >= 0 ? colors_ti : checkTerminalEnvironmentForColors();
360378

361379
// Now extract the structure allocated by setupterm and free its memory
362380
// through a really silly dance.
363381
struct term *termp = set_curterm(previous_term);
364382
(void)del_curterm(termp); // Drop any errors here.
365383

366384
// Return true if we found a color capabilities for the current terminal.
367-
if (HasColors)
368-
return true;
385+
return HasColors;
369386
#else
370387
// When the terminfo database is not available, check if the current terminal
371388
// is one of terminals that are known to support ANSI color escape codes.
372-
if (const char *TermStr = std::getenv("TERM")) {
373-
return StringSwitch<bool>(TermStr)
374-
.Case("ansi", true)
375-
.Case("cygwin", true)
376-
.Case("linux", true)
377-
.StartsWith("screen", true)
378-
.StartsWith("xterm", true)
379-
.StartsWith("vt100", true)
380-
.StartsWith("rxvt", true)
381-
.EndsWith("color", true)
382-
.Default(false);
383-
}
389+
return checkTerminalEnvironmentForColors();
384390
#endif
385-
386-
// Otherwise, be conservative.
387-
return false;
388391
}
389392

390393
bool Process::FileDescriptorHasColors(int fd) {

0 commit comments

Comments
 (0)