Skip to content

Respect NO_COLOR and FORCE_COLOR, and detect non-TTY output #47

Description

@HafizMMoaz

supportsAnsi() returns true unconditionally on anything that is not Windows (src/CLI.php:57-72). It never asks whether STDOUT is actually a terminal, so escape sequences end up in redirected output:

php app list > commands.txt     # commands.txt contains \033[0;32m ...
php app list | grep deploy      # colors leak into the pipe

The Windows branch already does the right thing by asking sapi_windows_vt100_support(), so the behaviour is inconsistent between platforms.

Two additions worth making:

  1. Check stream_isatty(STDOUT) on all platforms and disable ANSI when it is false. The function is already used for STDIN in masked().
  2. Honour the NO_COLOR convention (https://no-color.org), which is what CI systems and users set, plus FORCE_COLOR as the escape hatch for when output is piped on purpose.

Rough precedence: explicit setAnsi() call, then FORCE_COLOR, then NO_COLOR, then the TTY check, then the current per-platform detection.

Note that supportsAnsi() carries #[Pure] while already calling getenv(), which is not pure. Worth dropping the attribute here (and on getWidth(), same reason) while touching this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions