Skip to content

Commit 2fdcd71

Browse files
committed
Better ANSI Color Support Detection
1 parent ff58bcf commit 2fdcd71

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

tools/hxcpp/BuildTool.hx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,8 +1603,11 @@ class BuildTool
16031603
}
16041604

16051605

1606-
if (defines.exists("HXCPP_NO_COLOUR") || defines.exists("HXCPP_NO_COLOR"))
1606+
if (Sys.getEnv("HXCPP_COLOUR") != null || Sys.getEnv("HXCPP_COLOR") != null)
1607+
Log.colorSupported = !(defines.exists("HXCPP_NO_COLOUR") || defines.exists("HXCPP_NO_COLOR"));
1608+
else if (defines.exists("HXCPP_NO_COLOUR") || defines.exists("HXCPP_NO_COLOR"))
16071609
Log.colorSupported = false;
1610+
16081611
Log.verbose = defines.exists("HXCPP_VERBOSE");
16091612
Log.showSetup = defines.exists("HXCPP_LOG_SETUP");
16101613
exitOnThreadError = defines.exists("HXCPP_EXIT_ON_ERROR");

tools/hxcpp/Log.hx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,29 @@ class Log
135135
{
136136
if (colorSupported == null)
137137
{
138-
if (!BuildTool.isWindows)
139-
{
140-
var result = -1;
141-
try
142-
{
143-
var process = new Process ("tput", [ "colors" ]);
144-
result = process.exitCode ();
145-
process.close ();
146-
}
147-
catch (e:Dynamic) {};
138+
var term = Sys.getEnv("TERM");
148139

149-
colorSupported = (result == 0);
140+
if (term == "dumb")
141+
{
142+
colorSupported = false;
150143
}
151144
else
152145
{
153-
colorSupported = (Sys.getEnv("TERM") == "xterm" || Sys.getEnv("ANSICON") != null);
146+
if (colorSupported != true && term != null)
147+
{
148+
colorSupported = ~/(?i)-256(color)?$/.match(term)
149+
|| ~/(?i)^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/.match(term);
150+
}
151+
152+
if (colorSupported != true)
153+
{
154+
colorSupported = Sys.getEnv("TERM_PROGRAM") == "iTerm.app"
155+
|| Sys.getEnv("TERM_PROGRAM") == "Apple_Terminal"
156+
|| Sys.getEnv("COLORTERM") != null
157+
|| Sys.getEnv("ANSICON") != null
158+
|| Sys.getEnv("ConEmuANSI") != null
159+
|| Sys.getEnv("WT_SESSION") != null;
160+
}
154161
}
155162
}
156163

@@ -160,8 +167,7 @@ class Log
160167
}
161168
else
162169
{
163-
var colorCodes:EReg = ~/\x1b\[[^m]+m/g;
164-
return colorCodes.replace(output, "");
170+
return ~/\x1b\[[0-9;]*m/g.replace(output, "");
165171
}
166172
}
167173

0 commit comments

Comments
 (0)