localgod/console-color

Tool for working with console colors

v1.0.3 2021-05-17 13:52 UTC

This package is not auto-updated.

Last update: 2024-04-18 04:47:12 UTC


README

Main Latest Stable Version License

Console Color

console-color makes it simple to work with ANSI color codes in you console.

Base methods

escape($string) : Escapes % so they don't get interpreted as color codes; Takes a string as an argument and returns the escaped string.

convert($string, $colored = true)) : Transform color codes whithin supplied string into ANSI control codes

Magic methods

For each of the colors supported you can call:

  • {colorname}Normal($string)
  • {colorname}Bright($string)
  • {colorname}Background($string)

to quickly format the string accordingly.

Code conversion table

Normal Bright Background
Black %k %K %0
Red %r %R %1
Green %g %G %2
Yellow %y %Y %3
Blue %b %B %4
Magenta %m %M %5
Cyan %c %C %6
White %w %W %7
Code Effect
%F Blinking, Flashing
%U Underline
%8 Reverse
%_,%9 Bold
%n Resets the color
%% A single %

Examples

use Localgod\Console\Color;
// Let's add a little color to the world
// %n resets the color so the following stuff doesn't get messed up
print Color::convert("%bHello World!%n\n");
// Paint it red
print Color::redNormal("Paint it red!\n");
// Colorless mode, in case you need to strip colorcodes off a text
print Color::convert("%rHello World!%n\n", false);
// The uppercase version makes a colorcode bold/bright
print Color::convert("%BHello World!%n\n");
// To print a %, you use %%
print Color::convert("3 out of 4 people make up about %r75%% %nof the world population.\n");
// Or you can use the escape() method.
print Color::convert("%y".Color::escape('If you feel that you do everying wrong, be random, there\'s a 50% Chance of making the right decision.')."%n\n");

Inspiration

This projects is a reimplementation of the ideas used in http://pear.php.net/package/Console_Color/