enco / cli-styles
管理 CLI 输出样式的包(颜色、背景、字体样式)
1.0.4
2022-09-14 20:29 UTC
Requires
- php: ^7.2.0
This package is auto-updated.
Last update: 2024-09-15 00:52:55 UTC
README
使用这个库,您可以美化 CLI 输出
可用样式
- 粗体
- 斜体
- 闪烁
- 下划线
- 删除线
- 上划线
- 文本颜色
- 背景颜色
如何使用
您可以将不同样式添加到文本的不同部分。例如,您指定两个带有名称 'success' 和 'error' 的样式。因此,您的文本必须如下所示
Some text without styles <success>Text with success styles</success> Some text without styles <error>Text with error styles</error> Some text without styles
注意: 您不能使用标签层级。此文本将是错误的
Some text without styles <success>Text with success styles <error>Text with error styles</error> Text with success styles </success> Some text without styles
在您的代码中,您必须使用 2 个类: \Enco\CliStyles
和 \Enco\Style
\Enco\Style
用于定义样式
您必须设置样式名称。否则,将抛出异常
名称必须匹配 ^[a-z0-9_-]+$
正则表达式
可用方法
use \Enco\Style; public function setName(string $name): Style; //Set name of style. Used like html tags public function setColor(string $color): Style; //Only color hash. Ex: #ff0024 public function setBackground(string $background): Style; //Only color hash. Ex: #ff0024 public function setBold(bool $isBold = true): Style; public function setItalic(bool $isItalic = true): Style; public function setUnderline(bool $isUnderline = true): Style; public function setCrossLine(bool $isCrossLine = true): Style; public function setOverLine(bool $isOverline = true): Style; public function setBlink(bool $isBlink = true): Style; public static function formatLink(string $url, string $text): string;
此外,您还可以指定默认样式,该样式应用于不带标签的文本
为此,只需将 \Enco\Style::DEFAULT_STYLE_NAME
常量指定为样式的名称即可
\Enco\CliStyles
用于将样式应用于文本
您必须添加您想要使用的样式
可用方法
use Enco\CliStylesPool; use Enco\Style; public function addStyle(Style $style): CliStylesPool; public function addText(string $text): CliStylesPool public function resolve(): string; public function __toString(): string; //Symlink to resolve() method
您可以使用 $cliStylesObject
来输出文本。样式将自动应用。示例
$cliStylesObject = new \Enco\CliStylesPool(); ... echo $cliStylesObject;
此外,您还可以转义标签
<error>This is example of \<error>\</error> tags</error>
输出将是
This is example of <error></error> tags
PHP 代码示例
use Enco\CliStylesPool; use Enco\Style; $style = new Style(); $style->setName('temp'); $style->setColor('#555555') ->setBackground('#ffffff') ->setItalic() ->setBold() ->setCrossLine();; $defaultStyle = new Style(); $defaultStyle->setName(Style::DEFAULT_STYLE_NAME) ->setColor('#ff00ff'); $errorStyle = new Style(); $errorStyle->setName('error') ->setColor('#ffffff') ->setBackground('#ff0000'); $text = " This is simple text without styles\n <temp>Some text with styles</temp>\n <error>One more text with styles</error>\n "; $cliColors = new CliStylesPool(); $cliColors->setText($text) ->addStyle($style) ->addStyle($defaultStyle) ->addStyle($errorStyle); echo $cliColors;
CLI 光标管理
您可以管理 CLI 光标位置。例如,移动光标或设置绝对位置
要管理 CLI 光标,您必须使用 \Enco\CliCursor
可用方法
use \Enco\CliCursor; public function saveCursorPosition(): CliCursor //You can save only one position public function restoreCursorPosition(): CliCursor //Restore cursor position from saved one public function moveLeft(int $cols): CliCursor //Move cursor left. $cols must be > 0 public function moveRight(int $cols): CliCursor //Move cursor right. $cols must be > 0 public function moveUp(int $lines, bool $moveToStartOfLine = false): CliCursor //Move cursor up. If $moveToStartOfLine = true -> also moves cursor to first column public function moveDown(int $lines, bool $moveToStartOfLine = false): CliCursor // Move cursor down. If $moveToStartOfLine = true -> also moves cursor to first column public function setCol(int $col): CliCursor //Set cursor column position (horizontal) public function setLine(int $line): CliCursor //Set cursor line position (vertical) public function setPosition(int $line, int $col): CliCursor //Set absolute cursor position in both directions public function eraseWindow(): CliCursor //Erase window (works like `clear` in bash) and set position to 1;1 public function eraseCurrentLine(): CliCursor //Erase current line and set cursor position to start of line public function scrollUp(int $height): CliCursor //Scroll terminal up (add new lines to header). $height must be > 0. Lines at bottom will be erased public function scrollDown(int $height): CliCursor //Scroll terminal down (add new lines to bottom). $height must be > 0. Lines at header will be erased public function hideCursor(): CliCursor //Hide cursor (cursor in terminal will be invisible, but still works) public function showCursor(): CliCursor //Show cursor position (cursor blink in terminal) public function getTerminalWidth(): int //Returns terminal width in cols public function getTerminalHeight(): int //Returns terminal height in lines
您必须小心使用 \Enco\CliStyles
和 \Enco\CliCursor
,因为 \Enco\CliCursor
在某些情况下可以重置样式(当新文本覆盖样式文本时)
代码示例
use Enco\CliCursor; $cursor = new CliCursor(); $cursor->eraseWindow(); echo '1'; //After echo cursor moves right, so position will be 1, 2 $cursor->setPosition(2, 2); echo '2'; //After echo cursor moves right, so position will be 2, 3 $cursor->setPosition(3, 3); echo '3'; //After echo cursor moves right, so position will be 3, 4 $cursor->moveLeft(1) ->moveDown(1); echo "Height: " . $cursor->getTerminalHeight() . ' lines. '; echo "Width: " . $cursor->getTerminalWidth() . 'columns';
输出将是
1
2
3
Height: 52 lines. Width: 204 columns
您始终可以向 <bednarsasha@gmail.com>
提出问题