Colorizing Windows Command Line Output from PHP

How to echo with different colors in the Windows command line

I wanted to to print one single line in a different color.

Use ANSI Escape Sequences.

Windows before 10 - no native support for ANSI colors on the console

For Windows version below 10, the Windows command console doesn't support output coloring by default. You could install either Cmder, ConEmu, ANSICON or Mintty (used by default in GitBash and Cygwin) to add coloring support to your Windows command console.

Windows 10 - Command Line Colors

Starting from Windows 10 the Windows console support ANSI Escape Sequences and some colors by default. The feature shipped with the Threshold 2 Update in Nov 2015.

MSDN Documentation

Update (05-2019): The ColorTool enables you to change the color scheme of the console. It's part of the Microsoft Terminal project.

Demo

Sample Image

Batch Command

The win10colors.cmd was written by Michele Locati:

The text below is stripped of special characters and will not work. You must copy it from here.

@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m [31mred foreground color[0m
echo ^<ESC^>[7m [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m

Change text colour in WINDOWS command line

Use SetConsoleTextAttribute to set the text colour.

How to enable color for PHP CLI?

First we use an escape character so we can actually define a output color. This is done with \033 (\e). Then we open the color statement with [31m. Red in this case.

The "some colored text" will be the text outputted in a different color. And after that we have to close the color statement with \033[0m.

php -r 'echo "\033[31m some colored text \033[0m some white text \n";'

ref 1

ref 2

Sample Image

PHPUnit enable Color Output

You will have to use http://softkube.com/blog/ansi-command-line-colors-under-windows or get some sane console like conemu https://code.google.com/p/conemu-maximus5/

Also, I suggest to download git for windows and use bash as your default shell.

How do you get the Windows Console \ Command Prompt to display color?

As Symfony2 console component documentation says, windows doesn't support natively ANSI colors. You could install ANSICON as suggested there or you can try with cygwin, it has full shell support (bash or zsh) so you can have colors and many more things.

How do I style text in console using php? [running in PHP CLI]

If you're aiming to output to a *nix console, you need to use ANSI escape codes. There are some packages you can use that wrap these escape codes in an easy API, like ansi-php.

How can I change the text color in the windows command prompt

You need to access the Win32 Console API. Unfortunately, I don't know how you'd do that from Ruby. In Perl, I'd use the Win32::Console module. The Windows console does not respond to ANSI escape codes.

According to the article on colorizing Ruby output that artur02 mentioned, you need to install & load the win32console gem.



Related Topics



Leave a reply



Submit