轉自: http://www.neatstudio.com/show-2568-1.shtmlphp
終端顯示顏色,在之前的想法當中,都是由於有了.profile的配色方案。而我通常也都是 採用默認的(snakevil是寫過一個bash帶顏色的方案的。我以爲太花哨了就沒有使用)html
爲何忽然間又想到這個?是由於在使用PHP輸出LOG的時候,千篇一率,從屏幕中找關鍵字很累,因此就想着,是否是用PHP也能夠輸出這種帶顏色的關鍵字?固然,這是由於我正好看到了一個PHP是這麼輸出的,它就是laraval,它的工具(laraval.phar)在命令行的輸出就是有不一樣顏色的,它給了我指引,意思就是,這個想法是能夠實現的。python
OK。找資料,知道在終端中用指定的字符來作爲背景色和字體色(http://blog.csdn.net/acmee/article/details/6613060)git
文中這樣介紹:github
1、shell下的實現方法 shell
先來說在shell下,如何實現。用echo命令就能夠實現,參看如下例子: 編程
echo -e "\033[32mHello, world!"
echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"
int color = 32; printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);
color=32 print 「\033[20;1H\033[1;4;%dHello, world.\033[0m"%color
其實在看到這一段以前,snakevil在本身的github的項目(https://github.com/snakevil/bashrc.x)中也介紹過,其實我相對仍是喜歡ubuntu的默認配色,snakevil的這個配色我是真心不是特別喜歡。。ubuntu
但究竟怎麼用PHP輸出呢?在用PHP輸出以前,找了一下網絡,發現已經有有用perl實現過了。那麼說實在的。若是沒有使用到一些特別的函數,其實php和perl實在是太象了,因此,能夠直接參考(http://perldoc.perl.org/Term/ANSIColor.html),這裏的代碼,除了那個類外,都仍是能夠復刻的。因而,再隨便找了點,果真仍是有現成的PHP代碼的(http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/)bash
好吧。這個URL太長了。仍是直接貼代碼吧:網絡
class Colors { private $foreground_colors = array(); private $background_colors = array(); public function __construct() { // Set up shell colors $this->foreground_colors['black'] = '0;30'; $this->foreground_colors['dark_gray'] = '1;30'; $this->foreground_colors['blue'] = '0;34'; $this->foreground_colors['light_blue'] = '1;34'; $this->foreground_colors['green'] = '0;32'; $this->foreground_colors['light_green'] = '1;32'; $this->foreground_colors['cyan'] = '0;36'; $this->foreground_colors['light_cyan'] = '1;36'; $this->foreground_colors['red'] = '0;31'; $this->foreground_colors['light_red'] = '1;31'; $this->foreground_colors['purple'] = '0;35'; $this->foreground_colors['light_purple'] = '1;35'; $this->foreground_colors['brown'] = '0;33'; $this->foreground_colors['yellow'] = '1;33'; $this->foreground_colors['light_gray'] = '0;37'; $this->foreground_colors['white'] = '1;37'; $this->background_colors['black'] = '40'; $this->background_colors['red'] = '41'; $this->background_colors['green'] = '42'; $this->background_colors['yellow'] = '43'; $this->background_colors['blue'] = '44'; $this->background_colors['magenta'] = '45'; $this->background_colors['cyan'] = '46'; $this->background_colors['light_gray'] = '47'; } // Returns colored string public function getColoredString($string, $foreground_color = null, $background_color = null) { $colored_string = ""; // Check if given foreground color found if (isset($this->foreground_colors[$foreground_color])) { $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; } // Check if given background color found if (isset($this->background_colors[$background_color])) { $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; } // Add string and end coloring $colored_string .= $string . "\033[0m"; return $colored_string; } // Returns all foreground color names public function getForegroundColors() { return array_keys($this->foreground_colors); } // Returns all background color names public function getBackgroundColors() { return array_keys($this->background_colors); } }
用法也比較簡單:
// Create new Colors class $colors = new Colors(); // Test some basic printing with Colors class echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "\n"; echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n"; echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n"; echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n"; echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n"; echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";
固然,若是你以爲這個代碼太麻煩,還有一個簡單的方法:
function colorize($text, $status) { $out = ""; switch($status) { case "SUCCESS": $out = "[42m"; //Green background break; case "FAILURE": $out = "[41m"; //Red background break; case "WARNING": $out = "[43m"; //Yellow background break; case "NOTE": $out = "[44m"; //Blue background break; default: throw new Exception("Invalid status: " . $status); } return chr(27) . "$out" . "$text" . chr(27) . "[0m"; } echo colorize("Your command was successfully executed...", "SUCCESS");
四種顏色也夠了。不過。。。NOTE的blue background。。。若是你的字符仍是黑色的,真心看不到字符串了。
至此,介紹完畢,你能夠試試(我已經用在項目中了)