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: 2016-08-19 08:28 UTC
From: mlocati at gmail dot com Assigned:
Status: Open Package: Output Control
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jan 26 09:01:24 2020 UTC