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 = &#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}";
}
}
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("/
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;
}
}
}
实例
直接输出带效果的文字
// 实例化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();