Skip to content

Commit 5451877

Browse files
sunshinecottaylorr
authored andcommitted
chainlint: sidestep impoverished macOS "terminfo"
Although the macOS Terminal.app is "xterm"-compatible, its corresponding "terminfo" entries -- such as "xterm", "xterm-256color", and "xterm-new"[1] -- neglect to mention capabilities which Terminal.app actually supports (such as "dim text"). This oversight on Apple's part ends up penalizing users of "good citizen" console programs which consult "terminfo" to tailor their output based upon reported terminal capabilities (as opposed to programs which assume that the terminal supports ANSI codes). The same problem is present in other Apple "terminfo" entries, such as "nsterm"[2], with which macOS Terminal.app may be configured. Sidestep this Apple problem by imbuing get_colors() with specific knowledge of capabilities common to "xterm" and "nsterm", rather than trusting "terminfo" to report them correctly. Although hard-coding such knowledge is ugly, "xterm" support is nearly ubiquitous these days, and Git itself sets precedence by assuming support for ANSI color codes. For other terminal types, fall back to querying "terminfo" via `tput` as usual. FOOTNOTES [1] iTerm2 FAQ suggests "xterm-new": https://iterm2.com/faq.html [2] Neovim documentation recommends terminal type "nsterm" with Terminal.app: https://neovim.io/doc/user/term.html#terminfo Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 3a79a80 commit 5451877

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

t/chainlint.pl

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -653,21 +653,32 @@ package main;
653653
my %COLORS = ();
654654
sub get_colors {
655655
return \%COLORS if %COLORS;
656-
if (exists($ENV{NO_COLOR}) ||
657-
system("tput sgr0 >/dev/null 2>&1") != 0 ||
658-
system("tput bold >/dev/null 2>&1") != 0 ||
659-
system("tput rev >/dev/null 2>&1") != 0 ||
660-
system("tput setaf 1 >/dev/null 2>&1") != 0) {
656+
if (exists($ENV{NO_COLOR})) {
661657
%COLORS = @NOCOLORS;
662658
return \%COLORS;
663659
}
664-
%COLORS = (bold => `tput bold`,
665-
rev => `tput rev`,
666-
reset => `tput sgr0`,
667-
blue => `tput setaf 4`,
668-
green => `tput setaf 2`,
669-
red => `tput setaf 1`);
670-
chomp(%COLORS);
660+
if ($ENV{TERM} =~ /xterm|xterm-\d+color|xterm-new|xterm-direct|nsterm|nsterm-\d+color|nsterm-direct/) {
661+
%COLORS = (bold => "\e[1m",
662+
rev => "\e[7m",
663+
reset => "\e[0m",
664+
blue => "\e[34m",
665+
green => "\e[32m",
666+
red => "\e[31m");
667+
return \%COLORS;
668+
}
669+
if (system("tput sgr0 >/dev/null 2>&1") == 0 &&
670+
system("tput bold >/dev/null 2>&1") == 0 &&
671+
system("tput rev >/dev/null 2>&1") == 0 &&
672+
system("tput setaf 1 >/dev/null 2>&1") == 0) {
673+
%COLORS = (bold => `tput bold`,
674+
rev => `tput rev`,
675+
reset => `tput sgr0`,
676+
blue => `tput setaf 4`,
677+
green => `tput setaf 2`,
678+
red => `tput setaf 1`);
679+
return \%COLORS;
680+
}
681+
%COLORS = @NOCOLORS;
671682
return \%COLORS;
672683
}
673684

0 commit comments

Comments
 (0)