Skip to content

Commit cab337d

Browse files
committed
Console: added detectTerminal() as reusable TTY check
Extracted the TTY detection out of detectColors() so it can be used independently to auto-disable features that only make sense in an interactive terminal (progress indicators, line-rewriting output). detectColors() now builds on top of detectTerminal() and adds the NO_COLOR/FORCE_COLOR environment logic on top.
1 parent 09b2538 commit cab337d

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/CommandLine/Console.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,27 @@ public function color(?string $color, ?string $s = null): string
6060
/**
6161
* Detects whether the terminal supports ANSI colors.
6262
* Returns false when NO_COLOR is set, or when not running in a CLI TTY.
63-
* On Windows, checks VT100 support via sapi_windows_vt100_support().
63+
* FORCE_COLOR overrides the TTY check.
6464
*/
6565
public static function detectColors(): bool
6666
{
6767
return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
6868
&& getenv('NO_COLOR') === false // https://no-color.org
69-
&& (getenv('FORCE_COLOR')
70-
|| (function_exists('sapi_windows_vt100_support')
71-
? sapi_windows_vt100_support(STDOUT)
72-
: @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
73-
);
69+
&& (getenv('FORCE_COLOR') || self::detectTerminal());
70+
}
71+
72+
73+
/**
74+
* Detects whether the output is an interactive terminal.
75+
* Useful for auto-disabling features that only make sense in a TTY
76+
* (progress indicators, line-rewriting output, interactive prompts).
77+
* On Windows, checks VT100 support via sapi_windows_vt100_support().
78+
*/
79+
public static function detectTerminal(): bool
80+
{
81+
return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
82+
&& (function_exists('sapi_windows_vt100_support')
83+
? sapi_windows_vt100_support(STDOUT)
84+
: @stream_isatty(STDOUT)); // @ may trigger error 'cannot cast a filtered stream on this system'
7485
}
7586
}

0 commit comments

Comments
 (0)