php command 命令行格式化输出

php command 命令行格式化输出


最近需要写在命令行输出带颜色 加粗闪烁等格式化文字。封装了一个类方便调用

<?php
/**
 * 命令行输出字符格式化类
 * Class FormatOutput
 * @package MPQueue\OutPut
 */
class FormatOutput
{
    private $label = '';
    private $content;
    private $outFile = null;

    public function __construct(string $content)
    {
        $this->content = $content;
    }

    /**
     * 设置输出到文件/直接输出
     * @param null $outFile 文件地址/null(直接输出)
     */
    public function setOutFile($outFile = null){
        $this->outFile = $outFile;
        return $this;
    }

    /**
     * 设置粗体/增强
     */
    public function strong()
    {
        $this->label.= '1;';
        return $this;
    }

    /**
     * 设置斜体(未广泛支持。有时视为反相显示。)
     */
    public function italic()
    {
        $this->label.= '3;';
        return $this;
    }

    /**
     * 上划线
     */
    public function overline(){
        $this->label.= '53;';
        return $this;
    }

    /**
     * 中划线(未广泛支持)
     */
    public function lineThrough()
    {
        $this->label.= '9;';
        return $this;
    }

    /**
     * 下划线
     */
    public function underline()
    {
        $this->label.= '4;';
        return $this;
    }

    /**
     * 缓慢闪烁(低于每分钟150次)
     */
    public function slowBlink()
    {
        $this->label.= '5;';
        return $this;
    }

    /**
     * 快速闪烁(未广泛支持)
     */
    public function fastBlink()
    {
        $this->label.= '6;';
        return $this;
    }

    /**
     * 设置字体颜色/前景色(rgb色值 默认为白色)
     * @param int $r 红 0-255
     * @param int $g 绿 0-255
     * @param int $b 蓝 0-255
     */
    public function color(int $r = 255, int $g = 255, int $b = 255)
    {
        $this->label.= "38;2;$r;$g;$b;";
        return $this;
    }

    /**
     * 设置背景景色(rgb色值 默认为黑色)
     * @param int $r 红 0-255
     * @param int $g 绿 0-255
     * @param int $b 蓝 0-255
     */
    public function backgroundColor(int $r = 0, int $g = 0, int $b = 0)
    {
        $this->label.= "48;2;$r;$g;$b;";
        return $this;

    }

    /**
     * 输出内容
     */
    public function outPut(){
        if($this->outFile){
            file_put_contents($this->outFile,$this->getFormatContent(),FILE_APPEND | LOCK_EX);
        }else{
            echo $this->getFormatContent();
        }
    }

    /**
     * 获取格式化后的输出内容
     * @return string
     */
    public function getFormatContent(): string
    {
        $this->label = rtrim($this->label,';');
        return "\e[{$this->label}m{$this->content}\e[0m";
    }

    /**
     * 静态调用快速设置内容
     * @param string $content
     * @return FormatOutput
     */
    public static function setContent(string $content)
    {
        return new self($content);
    }
}

调用方式

FormatOutput::setContent("白底红字\n")->backgroundColor(255,255,255)->color(255,0,0)->outPut();

调用效果
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值