Files
OMS/app/taskmgr/lib/swconsole/format.php
2025-12-28 23:13:25 +08:00

153 lines
3.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Copyright © ShopeX http://www.shopex.cn. All rights reserved.
* See LICENSE file for license details.
*/
class taskmgr_swconsole_format
{
private $label = '';
private $content;
private $outFile = null;
/**
* __construct
* @param string $content content
* @return mixed 返回值
*/
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);
}
}