轉自: http://www.neatstudio.com/show-2568-1.shtml
終端顯示顏色,在以前的想法當中,都是因為有了.profile的配色方案。而我一般也都是 采用默認的(snakevil是寫過一個bash帶顏色的方案的。我覺得太花哨了就沒有使用)
為什么突然間又想到這個?是因為在使用PHP輸出LOG的時候,千篇一率,從屏幕中找關鍵字很累,所以就想着,是不是用PHP也可以輸出這種帶顏色的關鍵字?當然,這是因為我正好看到了一個PHP是這么輸出的,它就是laraval,它的工具(laraval.phar)在命令行的輸出就是有不同顏色的,它給了我指引,意思就是,這個想法是可以實現的。
OK。找資料,知道在終端中用指定的字符來做為背景色和字體色(http://blog.csdn.net/acmee/article/details/6613060)
文中這樣介紹:
一、shell下的實現方法
先來講在shell下,如何實現。用echo命令就可以實現,參看以下例子:
echo -e "\033[32mHello, world!"
- 當你在終端里敲下這條命令后,是不是發現系統用綠色輸出了"Hello,world!",不止如此,連之后的命令提示符都變成了綠色?不要着急,聽我繼續說。echo命令-e選項的作用是激活終端對反斜線轉義符(即\)的解釋。引號內\033用於引導非常規字符序列,在這里的作用就是引導設置輸出屬性,后邊的[32m就是將前景色設置為綠色,字母m表示設置的屬性類別,數字代表屬性值。設置可以單獨使用,例如:
- echo -e "\033[0m"
- 這行命令的作用是恢復屬性為默認值,也就是說0m設置項用於恢復默認值。現在你的終端是不是又一切正常了?
- 理解了這些,剩下的就簡單了。用這種命令,除了設置文本前景色,還可以設置很多屬性。下邊列出其他的設置項:
- --------------------------------------------------------------------------
- \033[0m 關閉所有屬性
- \033[1m 設置高亮度
- \033[4m 下划線
- \033[5m 閃爍
- \033[7m 反顯
- \033[8m 消隱
- \033[30m 至 \33[37m 設置前景色
- \033[40m 至 \33[47m 設置背景色
- \033[nA 光標上移n行
- \033[nB 光標下移n行
- \033[nC 光標右移n行
- \033[nD 光標左移n行
- \033[y;xH設置光標位置
- \033[2J 清屏
- \033[K 清除從光標到行尾的內容
- \033[s 保存光標位置
- \033[u 恢復光標位置
- \033[?25l 隱藏光標
- \033[?25h 顯示光標
- --------------------------------------------------------------------------
- 各數字所代表的顏色如下:
- 字背景顏色范圍:40----49
- 40:黑
- 41:深紅
- 42:綠
- 43:黃色
- 44:藍色
- 45:紫色
- 46:深綠
- 47:白色
- 字顏色:30-----------39
- 30:黑
- 31:紅
- 32:綠
- 33:黃
- 34:藍色
- 35:紫色
- 36:深綠
- 37:白色
- 另外,同類的多種設置項可以組合在一起,中間用分號(;)隔開。如下:
-
echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"
- 這行命令首先\033[20;1H將光標移動到終端第20行第1列,之后的\033[1;4;32m將文本屬性設置為高亮、帶下划線且顏色為綠色,然后輸出Hello,world;最后\033[0m將終端屬性恢復為默認值,這樣就不會看到連命令完成后的命令提示符也變了樣兒了。
- 通過以上各種命令的組合就可以實現對終端輸出地復雜控制。
- 二、如何在C編程中實現?
- 理解了以上在Shell中的實現方法,關於在C中如何實現就很簡單了。可以說只需要用printf函數代替上邊的echo -e就OK了。參見下例:
-
int color = 32; printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color); - 這個例子類似上邊shell中最后那個例子,只是這里顏色值通過變量color來指定(當然,也可以直接指定)。
- 三、聯想
- 看到這里你可能會想,是不是在其他編程語言里也可以用類似的方法實現對終端輸出的控制呢?答案是肯定的!比如在python中,可以如下輸出:
color=32 print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color
- 這個例子的效果跟上邊C的例子是相同的。
其實在看到這一段之前,snakevil在自己的github的項目(https://github.com/snakevil/bashrc.x)中也介紹過,其實我相對還是喜歡ubuntu的默認配色,snakevil的這個配色我是真心不是特別喜歡。。
但究竟怎么用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/)
好吧。這個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。。。如果你的字符還是黑色的,真心看不到字符串了。
至此,介紹完畢,你可以試試(我已經用在項目中了)

