php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72896 Add a way to determine if output is redirected
Submitted: 2016-08-19 08:26 UTC Modified: 2020-01-27 14:45 UTC
From: mlocati at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Output Control
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
 [2016-08-19 08:26 UTC] mlocati at gmail dot com
Description:
------------
Strictly related to https://bugs.php.net/bug.php?id=72768

If we enable color support for Windows, scripts may want to know if the output is redirected to file.

For instance, we may print colored text if the standard output is not redirected to file, but we may want to avoid ANSI control sequences if the output is redirected to a file.

I wrote a sample C program that does this check: https://gist.github.com/mlocati/21a9233ac83f7d3d7837535bc109b3b7

For posix systems it's already possible by using the posix_isatty PHP function (see what's doing Symfony for instance: https://github.com/symfony/symfony/blob/2909e4b04d04653286b8d63b19e11a40157497b5/src/Symfony/Component/Console/Output/StreamOutput.php#L101 )

Test script:
---------------
<?php

if (SomeWayToDetermineIfStdoutIsRedirectedToFile) {
   echo "Plain text";
} else {
   echo \033[101;93m Colored text \033[0m\n"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-19 08:28 UTC] mlocati at gmail dot com
-Summary: Add a way if output is redirected +Summary: Add a way to determine if output is redirected
 [2016-08-19 08:28 UTC] mlocati at gmail dot com
(Fixed the title of this bug report)
 [2020-01-27 11:28 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2020-01-27 11:28 UTC] cmb@php.net
I think one can use sapi_windows_vt100_support()[1] for that
purpose.

[1] <https://www.php.net/manual/en/function.sapi-windows-vt100-support.php>
 [2020-01-27 13:52 UTC] mlocati at gmail dot com
-Status: Feedback +Status: Assigned
 [2020-01-27 13:52 UTC] mlocati at gmail dot com
Yep, this "bug" has been fixed by https://github.com/php/php-src/pull/2103
 [2020-01-27 14:45 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-01-27 14:45 UTC] cmb@php.net
Ah, nice! :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 12:01:29 2024 UTC