字体颜色与背景色
\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:白色
标记闭合
所有效果在文本结尾处要加上闭合的标记:\033[0m;
文字高亮等其他效果
\033[1m 文字高亮 \033[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐
多种效果组合使用
多种效果组合使用时用英文分号隔开,例如蓝底红字下划线加闪烁 echo "\033[44;31;4m this is a test msg \033[0m".PHP_EOL;
\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 显示光标
文字效果操作类
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; } } }
光标操作类
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}"; } }
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("/
(.*?)<\/td>/is",$this->html) ){ die('标签有误,必须包含table tr td标签且标签闭合'); } $this->table['html'] = $this->html; $this->getPrototype('table',$this->table); preg_match_all("/
|