Skip to content

Auto-detect colour support for output #969

Open
@fredden

Description

@fredden

PHP_CodeSniffer supports colourised output when requested. This can be enabled with --colors and disabled with --no-colors. By default, no colour is used in output.

Other tools can selectively show colours depending on the output destination.

For example, grep in Linux has a single --color (and equivalent --colour) flag which takes an optional argument. The argument has three possible values: always, never, and auto. (When the argument is not supplied, auto is assumed.)

Describe the solution you'd like

PHP_CodeSniffer should have an auto-detection mode for colour support.

  1. [next minor] Create functionality within PHP_CodeSniffer to allow for auto-detecting if colour support could be enabled in the current output.
    • Note that this functionality can be based on what has been done in other projects, like Symfony.
  2. [next minor] Adjust the existing --colors command-line flag to support an optional argument.
    These suggestions are in line with other PHP command-line tools.
    • --colors behaves the same as --colors=always
    • --colors=always turns on colour support
    • --colors=auto allows PHP_CodeSniffer to selectively enable / disable colourised output at its discretion.
    • --colors=never turns off colours support
    • --no-colors behaves the same as --colors=never
  3. [next minor] Soft-deprecate --no-colors flag.
  4. [last minor] Hard-deprecate --no-colors flag.
  5. [next minor +1, or next major] Change default value from never to auto.
  6. [next major] Remove --no-colors command-line flag

Future enhancements

  • Review code-base for existing use of colourised output: check that these use proper colour codes, and respect the setting.
  • Check for places where colours could be used, but are not currently.
  • Consider helper methods / utility for adding colour to text.
  • Consider if we should be adding colour always, and have an output processor remove colours as needed, or if we want to put if statements in the code where messages are generated. The helper methods / utility may be useful here.

Related issues / pull requests


Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions