1. 程式人生 > >PHP- 如何在終端輸出帶顏色的字型?

PHP- 如何在終端輸出帶顏色的字型?

轉自: 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)

文中這樣介紹:

XML/HTML程式碼
  1. 一、shell下的實現方法  
  2.   
  3.        先來講在shell下,如何實現。用echo命令就可以實現,參看以下例子:  
  4.   
  5.        echo  -e  "\033[32mHello, world!"  
  6.   
  7.        當你在終端裡敲下這條命令後,是不是發現系統用綠色輸出了"Hello,world!",不止如此,連之後的命令提示符都變成了綠色?不要著急,聽我繼續說。echo命令-e選項的作用是啟用終端對反斜線轉義符(即\)的解釋。引號內\033用於引導非常規字元序列,在這裡的作用就是引導設定輸出屬性,後邊的[32m就是將前景色設定為綠色,字母m表示設定的屬性類別,數字代表屬性值。設定可以單獨使用,例如:  
  8.   
  9.        echo -e  "\033[0m"  
  10.   
  11.        這行命令的作用是恢復屬性為預設值,也就是說0m設定項用於恢復預設值。現在你的終端是不是又一切正常了?  
  12.   
  13.        理解了這些,剩下的就簡單了。用這種命令,除了設定文字前景色,還可以設定很多屬性。下邊列出其他的設定項:  
  14.   
  15.       --------------------------------------------------------------------------  
  16.   
  17.       \033[0m 關閉所有屬性  
  18.       \033[1m 設定高亮度  
  19.       \033[4m 下劃線  
  20.       \033[5m 閃爍  
  21.       \033[7m 反顯  
  22.       \033[8m 消隱  
  23.       \033[30m 至 \33[37m 設定前景色  
  24.       \033[40m 至 \33[47m 設定背景色  
  25.       \033[nA 游標上移n行   
  26.       \033[nB 游標下移n行  
  27.       \033[nC 游標右移n行  
  28.       \033[nD 游標左移n行  
  29.       \033[y;xH設定游標位置  
  30.       \033[2J 清屏  
  31.       \033[K 清除從游標到行尾的內容  
  32.       \033[s 儲存游標位置   
  33.       \033[u 恢復游標位置  
  34.       \033[?25l 隱藏游標  
  35.       \033[?25h 顯示游標  
  36.   
  37.       --------------------------------------------------------------------------  
  38.   
  39.       各數字所代表的顏色如下:  
  40.   
  41.       字背景顏色範圍:40----49  
  42.       40:黑  
  43.       41:深紅  
  44.       42:綠  
  45.       43:黃色  
  46.       44:藍色  
  47.       45:紫色  
  48.       46:深綠  
  49.       47:白色  
  50.   
  51.       字顏色:30-----------39  
  52.       30:黑  
  53.       31:紅  
  54.       32:綠  
  55.       33:黃  
  56.       34:藍色  
  57.       35:紫色  
  58.       36:深綠   
  59.       37:白色  
  60.   
  61.       另外,同類的多種設定項可以組合在一起,中間用分號(;)隔開。如下:  
  62.   
  63.       echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"  
  64.   
  65.       這行命令首先\033[20;1H將游標移動到終端第20行第1列,之後的\033[1;4;32m將文字屬性設定為高亮、帶下劃線且顏色為綠色,然後輸出Hello,world;最後\033[0m將終端屬性恢復為預設值,這樣就不會看到連命令完成後的命令提示符也變了樣兒了。  
  66.   
  67.       通過以上各種命令的組合就可以實現對終端輸出地複雜控制。  
  68.   
  69. 二、如何在C程式設計中實現?  
  70.   
  71.       理解了以上在Shell中的實現方法,關於在C中如何實現就很簡單了。可以說只需要用printf函式代替上邊的echo -e就OK了。參見下例:  
  72.   
  73.       int color = 32;  
  74.   
  75.       printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);  
  76.   
  77.       這個例子類似上邊shell中最後那個例子,只是這裡顏色值通過變數color來指定(當然,也可以直接指定)。  
  78.   
  79. 三、聯想  
  80.   
  81.       看到這裡你可能會想,是不是在其他程式語言裡也可以用類似的方法實現對終端輸出的控制呢?答案是肯定的!比如在python中,可以如下輸出:  
  82.   
  83.       color=32  
  84.   
  85.       print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color  
  86.   
  87.       這個例子的效果跟上邊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太長了。還是直接貼程式碼吧:

PHP程式碼
  1. <?php  
  2.    
  3.     class Colors {  
  4.         private $foreground_colors = array();  
  5.         private $background_colors = array();  
  6.    
  7.         public function __construct() {  
  8.             // Set up shell colors  
  9.             $this->foreground_colors['black'] = '0;30';  
  10.             $this->foreground_colors['dark_gray'] = '1;30';  
  11.             $this->foreground_colors['blue'] = '0;34';  
  12.             $this->foreground_colors['light_blue'] = '1;34';  
  13.             $this->foreground_colors['green'] = '0;32';  
  14.             $this->foreground_colors['light_green'] = '1;32';  
  15.             $this->foreground_colors['cyan'] = '0;36';  
  16.             $this->foreground_colors['light_cyan'] = '1;36';  
  17.             $this->foreground_colors['red'] = '0;31';  
  18.             $this->foreground_colors['light_red'] = '1;31';  
  19.             $this->foreground_colors['purple'] = '0;35';  
  20.             $this->foreground_colors['light_purple'] = '1;35';  
  21.             $this->foreground_colors['brown'] = '0;33';  
  22.             $this->foreground_colors['yellow'] = '1;33';  
  23.             $this->foreground_colors['light_gray'] = '0;37';  
  24.             $this->foreground_colors['white'] = '1;37';  
  25.    
  26.             $this->background_colors['black'] = '40';  
  27.             $this->background_colors['red'] = '41';  
  28.             $this->background_colors['green'] = '42';  
  29.             $this->background_colors['yellow'] = '43';  
  30.             $this->background_colors['blue'] = '44';  
  31.             $this->background_colors['magenta'] = '45';  
  32.             $this->background_colors['cyan'] = '46';  
  33.             $this->background_colors['light_gray'] = '47';  
  34.         }  
  35.    
  36.         // Returns colored string  
  37.         public function getColoredString($string, $foreground_color = null, $background_color = null) {  
  38.             $colored_string = "";  
  39.    
  40.             // Check if given foreground color found  
  41.             if (isset($this->foreground_colors[$foreground_color])) {  
  42.                 $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";  
  43.             }  
  44.             // Check if given background color found  
  45.             if (isset($this->background_colors[$background_color])) {  
  46.                 $colored_string .= "\033[" . $this->background_colors[$background_color] . "m";  
  47.             }  
  48.    
  49.             // Add string and end coloring  
  50.             $colored_string .=  $string . "\033[0m";  
  51.    
  52.             return $colored_string;  
  53.         }  
  54.    
  55.         // Returns all foreground color names  
  56.         public function getForegroundColors() {  
  57.             return array_keys($this->foreground_colors);  
  58.         }  
  59.    
  60.         // Returns all background color names  
  61.         public function getBackgroundColors() {  
  62.             return array_keys($this->background_colors);  
  63.         }  
  64.     }  
  65.    

 

用法也比較簡單:

PHP程式碼
  1. <?php  
  2.    
  3.     // Create new Colors class  
  4.     $colors = new Colors();  
  5.    
  6.     // Test some basic printing with Colors class  
  7.     echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "\n";  
  8.     echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n";  
  9.     echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n";  
  10.     echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n";  
  11.     echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n";  
  12.     echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";  
  13.    

 

當然,如果你覺得這個程式碼太麻煩,還有一個簡單的方法:

PHP程式碼
  1. function colorize($text, $status) {  
  2.  $out = "";  
  3.  switch($status) {  
  4.   case "SUCCESS":  
  5.    $out = "[42m"; //Green background  
  6.    break;  
  7.   case "FAILURE":  
  8.    $out = "[41m"; //Red background  
  9.    break;  
  10.   case "WARNING":  
  11.    $out = "[43m"; //Yellow background  
  12.    break;  
  13.   case "NOTE":  
  14.    $out = "[44m"; //Blue background  
  15.    break;  
  16.   default:  
  17.    throw new Exception("Invalid status: " . $status);  
  18.  }  
  19.  return chr(27) . "$out" . "$text" . chr(27) . "[0m";  
  20. }  
  21.   
  22. echo colorize("Your command was successfully executed...", "SUCCESS");  

 

四種顏色也夠了。不過。。。NOTE的blue background。。。如果你的字元還是黑色的,真心看不到字串了。

至此,介紹完畢,你可以試試(我已經用在專案中了)