Created
January 26, 2011 22:27
-
-
Save bgrimes/797624 to your computer and use it in GitHub Desktop.
Provides a function that may be used to style the output of a console application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* colorize | |
* Description: Provides a function that may be used to style the output of a console application. | |
* | |
* Examples: | |
* colorize("Something bolded in cyan on red background", 'bold', 'on_red', 'cyan') | |
* You may use multiple styles together: | |
* colorize("Something bolded underlines and blinking.", 'bold', 'underline', 'blink') | |
* | |
* Text should always be the first argument, the last argument is a variable hash so you can have | |
* as many arguments as you want: | |
* | |
* print colorize("Something bolded", 'bold'); | |
* print colorize("Something in cyan", 'cyan'); | |
* print colorize("Something bolded in cyan", 'bold', 'cyan'); | |
* print colorize("Something bolded in cyan on red" | |
* . "background while blinking and underlined", | |
* 'bold', 'underline', 'on_red', 'cyan', 'blink'); | |
*/ | |
function colorize($text = "") | |
{ | |
$colorize = array( | |
/* This must be appended to the end of every style string to reset the style. */ | |
"default" => "\033[0m", | |
/* styles */ | |
"bold" => "\033[1m", // Set Bold mode. | |
"underline" => "\033[4m", // Set Underline mode. | |
"blink" => "\033[5m", // Creates a neat blinking effect in the console. | |
"reverse" => "\033[7m", // Exchange foreground and background colors. | |
"concealed" => "\033[8m", // Hide text (foreground color would be same as background). | |
/* font colors */ | |
"black" => "\033[30m", | |
"red" => "\033[31m", | |
"green" => "\033[32m", | |
"yellow" => "\033[33m", | |
"blue" => "\033[34m", | |
"magenta" => "\033[35m", | |
"cyan" => "\033[36m", | |
"white" => "\033[37m", | |
/* background colors */ | |
"on_black" => "\033[40m", | |
"on_red" => "\033[41m", | |
"on_green" => "\033[42m", | |
"on_yellow" => "\033[43m", | |
"on_blue" => "\033[44m", | |
"on_magenta"=> "\033[45m", | |
"on_cyan" => "\033[46m", | |
"on_white" => "\033[47m" | |
); | |
if ( func_num_args() == 1 ) | |
{ | |
return $text; | |
} | |
$output = ""; | |
$args = func_get_args(); | |
foreach ($args as $key => $value) | |
{ | |
if ($key == 0) | |
{ | |
continue; | |
} | |
if (array_key_exists($value, $colorize)) | |
{ | |
$output .= $colorize[$value]; | |
} | |
} | |
$output .= $text . $colorize["default"]; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment