项目作者: carloscabo

项目描述 :
Simple color constants to output background / foreground colors in CLI (bash, cmd, powershell...)
高级语言: PHP
项目地址: git://github.com/carloscabo/PHP-CLI-colors.git
创建时间: 2020-03-06T13:58:16Z
项目社区:https://github.com/carloscabo/PHP-CLI-colors

开源协议:MIT License

下载


PHP CLI colors

Simple color constants to output background / foreground colors in cli (bash, cmd, powershell…)

screenshot

Why?

There are several options to colorize CLI outputs from PHP but I find them innecesary complex (from my point of view), I think it’s much easier use some well defined constants instead classes and objects.

How to use

  1. echo cli_red, "My string", cli_eol;
  2. // Outputs "My string" in red color and a final breakline (\n)
  3. echo cli_yellow, cli_blue_bg, "My string", cli_eol;
  4. // Outputs "My string" in yellow over blue background and a final breakline (\n)

It’s important yo notice that color settings will continue to affect CLI outputs in all the sebseuquential echoes. To reset the CLI colors to it’s default values use cli_reset;

  1. echo cli_red, "Red text", cli_ellow, "Yellow text", cli_eol;
  2. echo cli_reset, "This will be in default CLI color.", cli_eol;

Available colors

Foregrounds

  1. cli_default
  2. cli_black
  3. cli_red
  4. cli_green
  5. cli_yellow
  6. cli_blue
  7. cli_magenta
  8. cli_cyan
  9. cli_grey
  10. cli_light_red
  11. cli_light_green
  12. cli_light_yellow
  13. cli_light_blue
  14. cli_light_magenta
  15. cli_light_cyan
  16. cli_white
  17. // Not in all systems
  18. cli_light_red_alt
  19. cli_light_green_alt
  20. cli_light_yellow_alt
  21. cli_light_blue_alt
  22. cli_light_magenta_alt
  23. cli_light_cyan_alt
  24. cli_white_alt

Backgrounds

  1. cli_default_bg
  2. cli_black_bg
  3. cli_red_bg
  4. cli_green_bg
  5. cli_yellow_bg
  6. cli_blue_bg
  7. cli_magenta_bg
  8. cli_cyan_bg
  9. cli_grey_bg
  10. cli_dark_grey_bg
  11. cli_light_red_bg
  12. cli_light_green_bg
  13. cli_light_yellow_bg
  14. cli_light_blue_bg
  15. cli_light_magenta_bg
  16. cli_light_cyan_bg
  17. cli_white_bg

Text modifiers

  1. cli_bold
  2. cli_dim
  3. cli_underline
  4. cli_blink
  5. cli_reverse
  6. cli_hidden

For instance

  1. echo cli_red_bg, "White text over red bg", cli_reverse, "Red text over white bg", cli_reset, cli_eol;

Helpers

Four helpers are included, in order to show error / warnings / infos / successes…

screenshot

  1. cli_error
  2. cli_warning
  3. cli_info
  4. cli_success
  1. echo cli_error, "Error message!", cli_reset;

Tests

Run php -f cli-colors-test.php from your CLI.

TO-DO

¿Convert to composer module?