mirror of
https://gitee.com/ShopeX/OMS
synced 2026-03-23 02:45:33 +08:00
164 lines
3.5 KiB
PHP
164 lines
3.5 KiB
PHP
<?php
|
||
/**
|
||
* Copyright 2012-2026 ShopeX (https://www.shopex.cn)
|
||
*
|
||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
* you may not use this file except in compliance with the License.
|
||
* You may obtain a copy of the License at
|
||
*
|
||
* http://www.apache.org/licenses/LICENSE-2.0
|
||
*
|
||
* Unless required by applicable law or agreed to in writing, software
|
||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
* See the License for the specific language governing permissions and
|
||
* limitations under the License.
|
||
*/
|
||
|
||
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);
|
||
}
|
||
}
|