PHP-cli 模式在終端輸出彩色標記文字以及動態內容

biyugwv發表於2020-06-28

文字的各種效果標記寫法

  1. 字型顏色與背景色

     \033[30m 至 \33[37m 設定前景色  
     \033[40m 至 \33[47m 設定背景色  
     例如 echo "\033[30m  this is a test msg  \033[0m".PHP_EOL; 
          echo "\033[45m  this is a test msg  \033[0m".PHP_EOL;
     文字背景顏色範圍:
     40:41:深紅  
     42:43:黃色  
     44:藍色  
     45:紫色  
     46:深綠  
     47:白色  
    
     文字顏色:
     30:31:32:33:34:藍色  
     35:紫色  
     36:深綠   
     37:白色
  2. 標記閉合

      所有效果在文字結尾處要加上閉合的標記:\033[0m;
  3. 文字高亮等其他效果

     \033[1m 文字高亮
     \033[4m 下劃線
     \033[5m 閃爍
     \033[7m 反顯
     \033[8m 消隱
  4. 多種效果組合使用

      多種效果組合使用時用英文分號隔開,例如藍底紅字下劃線加閃爍
      echo "\033[44;31;4m  this is a test msg  \033[0m".PHP_EOL

游標的移動與設定

  1. 移動
     \033[nA 游標上移n行   
     \033[nB 游標下移n行  
     \033[nC 游標右移n行  
     \033[nD 游標左移n行
  2. 設定
     \033[y;xH設定游標位置  
     \033[2J 清屏  
     \033[K 清除從游標到行尾的內容  
     \033[s 儲存游標位置   
     \033[u 恢復游標位置  
     \033[?25l 隱藏游標  
     \033[?25h 顯示游標

簡單的實現

  1. 文字效果操作類

    namespace Console;
    class Style{
         private $colors = [
             "black"=>30,
             "red"=>31,
             "green"=>32,
             "yellow"=>33,
             "blue"=>34,
             "purple"=>35,
             "darkGreen"=>36,
             "white"=>37,
         ];
         private $backgrounds = [
             "black"=>40,
             "darkRed"=>41,
             "green"=>42,
             "yellow"=>43,
             "blue"=>44,
             "purple"=>45,
             "darkGreen"=>46,
             "white"=>47,
         ];
    
         public $msg;
         public $style = [];
         public function __construct($msg){
             $this->msg = $msg;
         }
         // 設定文字顏色
         public function color( string $c ){
             if( isset( $this->colors[$c]) ) $this->style[] = $this->colors[$c];
             return $this;
         }
         // 設定背景色
         public function bg( string $c ){
             if(isset($this->backgrounds[$c]) ) $this->style[] = $this->backgrounds[$c];
             return $this;
         }
         // 高亮
         public function highLight(){
             $this->style[] = 1;
             return $this;
         }
         // 下劃線
         public function underLine(){
             $this->style[] = 4;
             return $this;
         }
         // 閃爍
         public function twinkle(){
             $this->style[] = 5;
             return $this;
         }
         // 反顯
         public function rshow(){
             $this->style[] = 7;
             return $this;
         }
         //  消隱
         public function hide(){
             $this->style[] = 8;
             return $this;
         }
    
         public function toString(){
             $this->style = array_unique($this->style);
             if($this->msg){
                 if(sizeof($this->style)  ){
                     return "\033[".implode(';',$this->style)."m"  . $this->msg . "\033[0m";
                 }else{
                     return $this->msg. "\033[0m";
                 }
             }else{
                 return false;
             }
         }
     }
  2. 游標操作類

    namespace Console;
    
     // 游標的資訊以及操作
     class Cursor{
         // 游標設定 \033[y;xH
         private $x=0;
         private $y=0;
         // 獲取游標X軸位置
         public function offsetX(){
             return $this->x;
         }
         // 獲取游標Y軸位置
         public function offsetY(){
             return $this->y;
         }
    
         // 獲取游標座標
         public function offset(){
             return [
                 'x'=>$this->x,
                 'y'=>$this->y,
             ];
         }
         public function setX( int $x ){
             $this->x = $x;
         }
    
         public function setY( int $y ){
             $this->y = $y;
         }
    
         public function setOffset( int $x , int $y ){
             $this->x = $x;
             $this->y = $y;
             return $this->toString();
         }
         // 清屏
         public function clear(){
             return "\033[2J";
         }
    
         public function show(){
             return "\33[?25h";
         }
         public function hide(){
             return "\33[?25l";
         }
    
         public function toString(){
             if($this->x<0)$dx = 'D';
             else $dx = 'C';
             if($this->y<0)$dy = 'A';
             else $dy = 'B';
             $absx = abs($this->x);
             $absy = abs($this->y);
             return "\033[{$absx}{$dx}\033[{$absy}{$dy}";
         }
     }
  3. table類,通便html的table標記語言,輸出table

    namespace Console;
    class Table{
         public $table=[];
         private $getV;
         private $currentTag='table';
         private $props = [
             'color','bg','twinkle','highLight','underLine','colspan','rowspan','width','border','align'
         ];
         public function __construct( string $html){
             // 解析字串最好
             $this->html=$html;
             $this->parse();
         }
         // 解析字串 將table的每個tr td以及屬性都解析出來 
         private function parse(){
             if( !preg_match("/<table(\s+.*?)?>(.*?)<\/table>/is",$this->html) || !preg_match("/<tr(\s+.*?)?>(.*?)<\/tr>/is",$this->html) || !preg_match("/<td(\s+.*?)?>(.*?)<\/td>/is",$this->html) ){
                 die('標籤有誤,必須包含table tr  td標籤且標籤閉合');
             }
    
             $this->table['html'] = $this->html;
             $this->getPrototype('table',$this->table);
             preg_match_all("/<table(\s+.*?)?>(.*?)<\/table>/is",$this->html,$innerTable);
             if( $innerTable[0][0] ){
                 preg_match_all("/<tr(\s+.*?)?>(.*?)<\/tr>/is",$this->html,$trList);
                 if( $trList[0] ){
                     $this->table['tr'] = $trList[0];
                     foreach($this->table['tr'] as $k=>$tr){
                         $this->table['tr'][$k] = [];
                         preg_match_all("/<td(\s+.*?)?>(.*?)<\/td>/is",$tr,$tdList);
                         $this->table['tr'][$k]['td'] = $tdList[0];
                         $this->table['tr'][$k]['html'] =$tr;
                         $this->getPrototype('tr',$this->table['tr'][$k]);
                         foreach ($this->table['tr'][$k]['td'] as $kk=>$td){
                             $this->table['tr'][$k]['td'][$kk] = [
                                 'html'=>$td,
                                 'content'=>$tdList[2][$kk]
                             ];
                             $this->getPrototype('td',$this->table['tr'][$k]['td'][$kk]);
                         }
                     }
                 }else{
                     die('Tr 不存在');
                 }
             }else{
                 die('Table 不存在');
             }
         }
         private function getPrototype(string $tag,&$v){
             preg_match("/<{$tag}(\s.*?)?>(.*?)<\/{$tag}>/is",$v['html'],$arr);
             if(strlen($arr[1])){
                 $arr2 = explode(" ", trim(preg_replace("/( +)/is"," ",$arr[1])));
                 foreach ($arr2 as $arr3){
                     $arr4 = explode('=',str_replace([' ',"\"","\'"],'',$arr3));
                     if(in_array($arr4[0],$this->props)){
                         $v[$arr4[0]] = $arr4[1];
                     }
                 }
             }
         }
     }
  4. console類,輸出自定義的文字或table格式文字

    namespace Console;
    class Console{
         public $cursor;
         private $msgList=[];
         private $lastCountLine=0;
         public function __construct(){
             $this->cursor= new Cursor();
         }
    
         private static function getStrlen($str){
             if(!$str) return 0;
             $l=0;
             $strArr = preg_split('//u',$str,-1, PREG_SPLIT_NO_EMPTY);
             if(is_array($strArr)){
                 foreach($strArr as $v){
                     if(preg_match("/^[\x{4e00}-\x{9fa5}]$/u", $v)) $l += 2;
                     else $l += 1;
                 }
             }
             return $l;
         }
    
         public function write($msg){
             $msgStyle = new Style($msg);
             $this->msgList[] =  $msgStyle;
             return  $msgStyle;
         }
    
         public function table(array $table){
             foreach($table['tr'] as $tr){
    
                 foreach($tr['td'] as $td){
                     if($td['content']){
                         $len = self::getStrlen($td['content']); // 顯示問題矯正
                         $tdlen = $td['width'] ?? max(15,$len);
                         $tdlen = max($tdlen,$len);
                         $align = $td['align'] ??$tr['align']??$table['align']?? false;
                         if( $tdlen>$len ){
                             if( $align=='left'){
                                 $td['content'] =  $td['content'].str_repeat(' ',$tdlen-$len);
                             }else if($align=='right'){
                                 $td['content'] = str_repeat(' ',$tdlen-$len) . $td['content'];
                             }else{
                                 $td['content'] = str_repeat(' ',floor(($tdlen-$len)/2)) . $td['content'] . str_repeat(' ',ceil(($tdlen-$len)/2));
                             }
                         }
                         $msg = $this->write($td['content']);
                         $color = $td['color']  ??   $tr['color'] ??$table['color']??false;
                         $twinkle = $td['twinkle']  ??   $tr['twinkle'] ??$table['twinkle']??false;
                         $bg  = $td['bg ']  ??   $tr['bg '] ??$table['bg ']??false;
                         $highLight = $td['highLight']  ??   $tr['highLight'] ??$table['highLight']??false;
                         $underLine = $td['underLine']  ??   $tr['underLine'] ??$table['underLine']??false;
    
                         if($color) $msg->color($color);
                         if($bg) $msg->bg($bg);
                         if($twinkle) $msg->twinkle();
                         if($highLight) $msg->highLight();
                         if($underLine) $msg->underLine();
                     }
                 }
                 $this->write("\n");
    
             }
    
             $this->dump();
         }
         public function dump(){
             foreach( $this->msgList as $msg){
                 echo $msg->toString();
             }
             $this->lastCountLine = $this->getLine();
             $this->msgList=[];
         }
         public function cursorMove( int $x  , int $y  ) {
    
             $this->write( $this->cursor->setOffset( $x,$y));
         }
         public function getCountLine(){
             return $this->lastCountLine;
         }
         private function getLine(){
             if(!sizeof($this->msgList)) return 0;
             else{
                 $line=0;
                 foreach(  $this->msgList as $msg ){
                     for($i=0;$i<mb_strlen($msg->msg);$i++){
                         if(mb_substr($msg->msg ,$i,1) == PHP_EOL) $line++;
                     }
                 }
    
                 return $line;
             }
         }
    
     }

例項

  1. 直接輸出帶效果的文字
    // 例項化console類
    $c = new Console\Console();
    // 向console類裡新增文字
    $msg = $c->write('this is a test msg.'.PHP_EOL);
    // 文字設定效果
    $msg->color('red')->bg('darkGreen')->highLight()->underLine();
    // 再次新增一個文字
    $msg2 = $c->write('this is another  msg.'.PHP_EOL);
    // 文字設定效果
    $msg2->color('yellow')->bg('purple')->twinkle()->underLine();
    // 輸出文字
    $c->dump();
    截圖:
  2. 通過table標籤標記輸出文字
      /*
     table標記注意事項
     1. 標籤有且有table、tr、td,且表橋閉合
     2. table、tr、td標籤屬性可設定color、bg、twinkle(等文字效果)、width、align。目前只實現了這些
     3. 資料的動態展示原理是通過計算table的行數並移動游標達到的,並不是很嚴謹,效果也一般
     */
      // 用於隨機字元
     $zmstr='abcdefghijklmnopqrstuvwxyz';
     while(true){
         $html='
             <table align="right">
             <tr color="red">
                 <td width=20 >英文</td>
                 <td width=30>數字</td>
                 <td width=30>中文</td>
             </tr>
             <tr>
             </tr>
         ';
         for($i=0;$i<5;$i++){
             $num = rand(10,99);
             $color='';
             if($num>80){
                 $color='red';
             }else if($num>50){
                 $color='green';
             }else if($num>30){
                 $color='purple';
             }
             $html.='<tr>
                         <td width=20>'.$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].'</td>
                         <td width=30 color="'.$color.'">'.$num.'</td>
                         <td width=30 >測試</td>
                     </tr>';
         }
         $html.='</table>';
         // 移動游標
         $c->cursorMove(-1000,-($c->getCountLine()));
         // 通過table標籤例項Table類
         $t = new Table($html);
         // 輸出解析後的table標籤
         $c->table($t->table);
         sleep(2);
     }
    截圖:
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章