@@ -58,7 +58,7 @@ public function __construct($output = null, $charset = null)
58
58
{
59
59
parent ::__construct ($ output , $ charset );
60
60
61
- if ('\\' === DIRECTORY_SEPARATOR && ' ON ' !== @ getenv ( ' ConEmuANSI ' ) && ' xterm ' !== @ getenv ( ' TERM ' )) {
61
+ if ('\\' === DIRECTORY_SEPARATOR && ! $ this -> isWindowsTrueColor ( )) {
62
62
// Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI
63
63
$ this ->setStyles (array (
64
64
'default ' => '31 ' ,
@@ -420,7 +420,7 @@ protected function style($style, $value, $attr = array())
420
420
protected function supportsColors ()
421
421
{
422
422
if ($ this ->outputStream !== static ::$ defaultOutput ) {
423
- return @( is_resource ( $ this ->outputStream ) && function_exists ( ' posix_isatty ' ) && posix_isatty ( $ this ->outputStream ) );
423
+ return $ this ->hasColorSupport ( $ this ->outputStream );
424
424
}
425
425
if (null !== static ::$ defaultColors ) {
426
426
return static ::$ defaultColors ;
@@ -448,23 +448,10 @@ protected function supportsColors()
448
448
}
449
449
}
450
450
451
- if ('\\' === DIRECTORY_SEPARATOR ) {
452
- static ::$ defaultColors = @(
453
- function_exists ('sapi_windows_vt100_support ' ) && sapi_windows_vt100_support ($ this ->outputStream )
454
- || '10.0.10586 ' === PHP_WINDOWS_VERSION_MAJOR .'. ' .PHP_WINDOWS_VERSION_MINOR .'. ' .PHP_WINDOWS_VERSION_BUILD
455
- || false !== getenv ('ANSICON ' )
456
- || 'ON ' === getenv ('ConEmuANSI ' )
457
- || 'xterm ' === getenv ('TERM ' )
458
- );
459
- } elseif (function_exists ('posix_isatty ' )) {
460
- $ h = stream_get_meta_data ($ this ->outputStream ) + array ('wrapper_type ' => null );
461
- $ h = 'Output ' === $ h ['stream_type ' ] && 'PHP ' === $ h ['wrapper_type ' ] ? fopen ('php://stdout ' , 'wb ' ) : $ this ->outputStream ;
462
- static ::$ defaultColors = @posix_isatty ($ h );
463
- } else {
464
- static ::$ defaultColors = false ;
465
- }
451
+ $ h = stream_get_meta_data ($ this ->outputStream ) + array ('wrapper_type ' => null );
452
+ $ h = 'Output ' === $ h ['stream_type ' ] && 'PHP ' === $ h ['wrapper_type ' ] ? fopen ('php://stdout ' , 'wb ' ) : $ this ->outputStream ;
466
453
467
- return static ::$ defaultColors ;
454
+ return static ::$ defaultColors = $ this -> hasColorSupport ( $ h ) ;
468
455
}
469
456
470
457
/**
@@ -477,4 +464,69 @@ protected function dumpLine($depth, $endOfValue = false)
477
464
}
478
465
parent ::dumpLine ($ depth );
479
466
}
467
+
468
+ /**
469
+ * Returns true if the stream supports colorization.
470
+ *
471
+ * Reference: Composer\XdebugHandler\Process::supportsColor
472
+ * https://github.com/composer/xdebug-handler
473
+ *
474
+ * @param mixed $stream A CLI output stream
475
+ *
476
+ * @return bool
477
+ */
478
+ private function hasColorSupport ($ stream )
479
+ {
480
+ if (!is_resource ($ stream ) || 'stream ' !== get_resource_type ($ stream )) {
481
+ return false ;
482
+ }
483
+
484
+ if (DIRECTORY_SEPARATOR === '\\' ) {
485
+ return (function_exists ('sapi_windows_vt100_support ' )
486
+ && @sapi_windows_vt100_support ($ stream ))
487
+ || false !== getenv ('ANSICON ' )
488
+ || 'ON ' === getenv ('ConEmuANSI ' )
489
+ || 'xterm ' === getenv ('TERM ' );
490
+ }
491
+
492
+ if (function_exists ('stream_isatty ' )) {
493
+ return @stream_isatty ($ stream );
494
+ }
495
+
496
+ if (function_exists ('posix_isatty ' )) {
497
+ return @posix_isatty ($ stream );
498
+ }
499
+
500
+ $ stat = @fstat ($ stream );
501
+ // Check if formatted mode is S_IFCHR
502
+ return $ stat ? 0020000 === ($ stat ['mode ' ] & 0170000 ) : false ;
503
+ }
504
+
505
+ /**
506
+ * Returns true if the Windows terminal supports true color.
507
+ *
508
+ * Note that this does not check an output stream, but relies on environment
509
+ * variables from known implementations, or a PHP and Windows version that
510
+ * supports true color.
511
+ *
512
+ * @return bool
513
+ */
514
+ private function isWindowsTrueColor ()
515
+ {
516
+ $ result = strval (getenv ('ANSICON_VER ' )) >= '183 '
517
+ || 'ON ' === getenv ('ConEmuANSI ' )
518
+ || 'xterm ' === getenv ('TERM ' );
519
+
520
+ if (!$ result && PHP_VERSION_ID >= 70200 ) {
521
+ $ version = sprintf (
522
+ '%s.%s.%s ' ,
523
+ PHP_WINDOWS_VERSION_MAJOR ,
524
+ PHP_WINDOWS_VERSION_MINOR ,
525
+ PHP_WINDOWS_VERSION_BUILD
526
+ );
527
+ $ result = $ version >= '10.0.15063 ' ;
528
+ }
529
+
530
+ return $ result ;
531
+ }
480
532
}
0 commit comments