PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Auto-detect colour support for output

Open fredden opened this issue 9 months ago • 2 comments

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

  • #662 ANSI color support on Windows

fredden avatar Apr 14 '25 10:04 fredden

I like the idea 👍🏻

jrfnl avatar Apr 14 '25 10:04 jrfnl

For inspiration regarding the auto-detection code: https://github.com/PHPCSStandards/PHPCSDevTools/blob/7e7690e82ffc81bd39d9c5d7735175cd63532f6b/Scripts/FeatureComplete/Config.php#L376-L409

jrfnl avatar May 02 '25 20:05 jrfnl