热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

学习PHP-cli模式在终端输出彩色标记文字以及动态内容

文字的各种效果标记写法有多种,本文从php模式学习在终端输出彩色标记文字以及动态内容,干货多多,希望和大家学习讨论。

文字的各种效果标记写法

  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 = &#39;D&#39;;
             else $dx = &#39;C&#39;;
             if($this->y<0)$dy = &#39;A&#39;;
             else $dy = &#39;B&#39;;
             $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=&#39;table&#39;;
         private $props = [
             &#39;color&#39;,&#39;bg&#39;,&#39;twinkle&#39;,&#39;highLight&#39;,&#39;underLine&#39;,&#39;colspan&#39;,&#39;rowspan&#39;,&#39;width&#39;,&#39;border&#39;,&#39;align&#39;
         ];
         public function __construct( string $html){
             // 解析字符串最好
             $this->html=$html;
             $this->parse();
         }
         // 解析字符串 将table的每个tr td以及属性都解析出来 
         private function parse(){
             if( !preg_match("/(.*?)<\/table>/is",$this->html) || !preg_match("/(.*?)<\/tr>/is",$this->html) || !preg_match("/(.*?)<\/td>/is",$this->html) ){
                 die(&#39;标签有误,必须包含table tr  td标签且标签闭合&#39;);
             }
    
             $this->table[&#39;html&#39;] = $this->html;
             $this->getPrototype(&#39;table&#39;,$this->table);
             preg_match_all("/(.*?)<\/table>/is",$this->html,$innerTable);
             if( $innerTable[0][0] ){
                 preg_match_all("/(.*?)<\/tr>/is",$this->html,$trList);
                 if( $trList[0] ){
                     $this->table[&#39;tr&#39;] = $trList[0];
                     foreach($this->table[&#39;tr&#39;] as $k=>$tr){
                         $this->table[&#39;tr&#39;][$k] = [];
                         preg_match_all("/(.*?)<\/td>/is",$tr,$tdList);
                         $this->table[&#39;tr&#39;][$k][&#39;td&#39;] = $tdList[0];
                         $this->table[&#39;tr&#39;][$k][&#39;html&#39;] =$tr;
                         $this->getPrototype(&#39;tr&#39;,$this->table[&#39;tr&#39;][$k]);
                         foreach ($this->table[&#39;tr&#39;][$k][&#39;td&#39;] as $kk=>$td){
                             $this->table[&#39;tr&#39;][$k][&#39;td&#39;][$kk] = [
                                 &#39;html&#39;=>$td,
                                 &#39;content&#39;=>$tdList[2][$kk]
                             ];
                             $this->getPrototype(&#39;td&#39;,$this->table[&#39;tr&#39;][$k][&#39;td&#39;][$kk]);
                         }
                     }
                 }else{
                     die(&#39;Tr 不存在&#39;);
                 }
             }else{
                 die(&#39;Table 不存在&#39;);
             }
         }
         private function getPrototype(string $tag,&$v){
             preg_match("/<{$tag}(\s.*?)?>(.*?)<\/{$tag}>/is",$v[&#39;html&#39;],$arr);
             if(strlen($arr[1])){
                 $arr2 = explode(" ", trim(preg_replace("/( +)/is"," ",$arr[1])));
                 foreach ($arr2 as $arr3){
                     $arr4 = explode(&#39;=&#39;,str_replace([&#39; &#39;,"\"","\&#39;"],&#39;&#39;,$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(&#39;//u&#39;,$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[&#39;tr&#39;] as $tr){
    
                 foreach($tr[&#39;td&#39;] as $td){
                     if($td[&#39;content&#39;]){
                         $len = self::getStrlen($td[&#39;content&#39;]); // 显示问题矫正
                         $tdlen = $td[&#39;width&#39;] ?? max(15,$len);
                         $tdlen = max($tdlen,$len);
                         $align = $td[&#39;align&#39;] ??$tr[&#39;align&#39;]??$table[&#39;align&#39;]?? false;
                         if( $tdlen>$len ){
                             if( $align==&#39;left&#39;){
                                 $td[&#39;content&#39;] =  $td[&#39;content&#39;].str_repeat(&#39; &#39;,$tdlen-$len);
                             }else if($align==&#39;right&#39;){
                                 $td[&#39;content&#39;] = str_repeat(&#39; &#39;,$tdlen-$len) . $td[&#39;content&#39;];
                             }else{
                                 $td[&#39;content&#39;] = str_repeat(&#39; &#39;,floor(($tdlen-$len)/2)) . $td[&#39;content&#39;] . str_repeat(&#39; &#39;,ceil(($tdlen-$len)/2));
                             }
                         }
                         $msg = $this->write($td[&#39;content&#39;]);
                         $color = $td[&#39;color&#39;]  ??   $tr[&#39;color&#39;] ??$table[&#39;color&#39;]??false;
                         $twinkle = $td[&#39;twinkle&#39;]  ??   $tr[&#39;twinkle&#39;] ??$table[&#39;twinkle&#39;]??false;
                         $bg  = $td[&#39;bg &#39;]  ??   $tr[&#39;bg &#39;] ??$table[&#39;bg &#39;]??false;
                         $highLight = $td[&#39;highLight&#39;]  ??   $tr[&#39;highLight&#39;] ??$table[&#39;highLight&#39;]??false;
                         $underLine = $td[&#39;underLine&#39;]  ??   $tr[&#39;underLine&#39;] ??$table[&#39;underLine&#39;]??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;$imsg);$i++){
                         if(mb_substr($msg->msg ,$i,1) == PHP_EOL) $line++;
                     }
                 }
    
                 return $line;
             }
         }
    
     }

实例

  1. 直接输出带效果的文字
    // 实例化console类$c = new Console\Console();// 向console类里添加文本$msg = $c->write(&#39;this is a test msg.&#39;.PHP_EOL);// 文本设置效果$msg->color(&#39;red&#39;)->bg(&#39;darkGreen&#39;)->highLight()->underLine();// 再次添加一个文本$msg2 = $c->write(&#39;this is another  msg.&#39;.PHP_EOL);// 文本设置效果$msg2->color(&#39;yellow&#39;)->bg(&#39;purple&#39;)->twinkle()->underLine();// 输出文本$c->dump();
    截图:
  2. 通过table标签标记输出文本
      /*
     table标记注意事项
     1. 标签有且有table、tr、td,且表桥闭合
     2. table、tr、td标签属性可设置color、bg、twinkle(等文字效果)、width、align。目前只实现了这些
     3. 数据的动态展示原理是通过计算table的行数并移动光标达到的,并不是很严谨,效果也一般
     */
      // 用于随机字符
     $zmstr=&#39;abcdefghijklmnopqrstuvwxyz&#39;;
     while(true){
         $html=&#39;
             
         &#39;;
         for($i=0;$i<5;$i++){
             $num = rand(10,99);
             $color=&#39;&#39;;
             if($num>80){
                 $color=&#39;red&#39;;
             }else if($num>50){
                 $color=&#39;green&#39;;
             }else if($num>30){
                 $color=&#39;purple&#39;;
             }
             $html.=&#39;&#39;;
         }
         $html.=&#39;
    英文 数字 中文
    &#39;.$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].$zmstr[rand(0,25)].&#39; &#39;.$num.&#39; 测试
    &#39;; // 移动光标 $c->cursorMove(-1000,-($c->getCountLine())); // 通过table标签实例Table类 $t = new Table($html); // 输出解析后的table标签 $c->table($t->table); sleep(2); }
    截图:

相关学习推荐:PHP编程从入门到精通

以上就是学习PHP-cli 模式在终端输出彩色标记文字以及动态内容的详细内容,更多请关注 第一PHP社区 其它相关文章!


推荐阅读
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • 本文介绍了使用CentOS7.0 U盘刻录工具进行安装的详细步骤,包括使用USBWriter工具刻录ISO文件到USB驱动器、格式化USB磁盘、设置启动顺序等。通过本文的指导,用户可以轻松地使用U盘安装CentOS7.0操作系统。 ... [详细]
  • Lodop中特殊符号打印设计和预览样式不同的问题解析
    本文主要解析了在Lodop中使用特殊符号打印设计和预览样式不同的问题。由于调用的本机ie引擎版本可能不同,导致在不同浏览器下样式解析不同。同时,未指定文字字体和样式设置也会导致打印设计和预览的差异。文章提出了通过指定具体字体和样式来解决问题的方法,并强调了以打印预览和虚拟打印机测试为准。 ... [详细]
  • Final关键字的含义及用法详解
    本文详细介绍了Java中final关键字的含义和用法。final关键字可以修饰非抽象类、非抽象类成员方法和变量。final类不能被继承,final类中的方法默认是final的。final方法不能被子类的方法覆盖,但可以被继承。final成员变量表示常量,只能被赋值一次,赋值后值不再改变。文章还讨论了final类和final方法的应用场景,以及使用final方法的两个原因:锁定方法防止修改和提高执行效率。 ... [详细]
  • 本文介绍了求解gcdexgcd斐蜀定理的迭代法和递归法,并解释了exgcd的概念和应用。exgcd是指对于不完全为0的非负整数a和b,gcd(a,b)表示a和b的最大公约数,必然存在整数对x和y,使得gcd(a,b)=ax+by。此外,本文还给出了相应的代码示例。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
author-avatar
U友50140862
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有