testomat/terminal-colour

此包已弃用,不再维护。没有建议的替代包。

以风格返回您的终端消息!使用ANSI颜色代码从终端、控制台或shell界面更改文本样式、文本颜色和文本背景颜色。


README

  • 在命令行/终端中设置文本样式最简单的方法
  • 将文本颜色改为红色、绿色、黄色...
  • 将背景颜色改为红色、绿色、黄色...
  • 将文本样式改为粗体、暗淡、下划线、闪烁...
  • 支持 0, 16, 256 和真彩色...

安装

运行

$ composer require testomat/terminal-colour

注意:默认情况下,Windows命令控制台不支持输出着色。此库禁用了Windows系统的输出着色。但如果您的命令或脚本调用了其他脚本,它们将被错误地显示为原始转义字符。请安装 CmderConEmuANSICONMintty(默认用于GitBash和Cygwin),以向您的Windows命令控制台添加着色支持。

用法

使用颜色样式

<?php

declare(strict_types=1);

use Testomat\TerminalColour\Formatter;

$formatter = new Formatter();

// green text
$formatter->format('<info>foo</info>');

// yellow text
$formatter->format('<comment>foo</comment>');

// black text on a cyan background
$formatter->format('<question>foo</question>');

// white text on a red background
$formatter->format('<error>foo</error>');

关闭标签可以被 </> 替换,这将取消由最后打开的标签建立的全部格式选项。

您可以使用Formatter类定义自己的样式。

<?php

declare(strict_types=1);

use Testomat\TerminalColour\Formatter;
use Testomat\TerminalColour\Style;

$style = new Style('red', 'yellow', ['bold', 'blink']);

$formatter = new Formatter(false, ['fire' => $style]);

$formatter->format('<fire>foo</fire>');

可用的前景和背景颜色有 black, red, green, yellow, blue, magenta, cyan, white, default, dark_grey, light_grey, light_red, light_green, light_yellow, light_blue, light_magenta, light_cyan, light_white

可用选项包括: none, bold, dark, italic, underscore, blink, blink_fast, crossed_out, double_underline, curly_underline, overlinem, reverse(启用“反转视频”模式,其中背景和前景颜色互换)和 conceal(将前景颜色设置为透明,使输入的文本不可见——尽管它可以被选中并复制;此选项通常用于要求用户输入敏感信息时)。

如果您想使用 256 colorstrue colors,则必须使用 StyleCode 类。

<?php

declare(strict_types=1);

use Testomat\TerminalColour\Formatter;
use Testomat\TerminalColour\StyleCode;

$style256 = new StyleCode(34); // will be transformed to '38;5;34' blue
$trueStyle = new StyleCode("38;2;1;1;1"); // same goes for 1;1;1 as value; will be transformed to '38;2;1;1;1'

$formatter = new Formatter(false, ['color256Blue' => $style256,  'trueColor' => $trueStyle]);

$formatter->format('<color256Blue>foo</color256Blue>');

您还可以直接在标签名称内设置这些颜色和选项。

// green text
$formatter->format('<fg=green>foo</>');

// black text on a cyan background
$formatter->format('<fg=black;bg=cyan>foo</>');

// bold text on a yellow background
$formatter->format('<bg=yellow;options=bold>foo</>');

// bold text with underscore
$formatter->format('<options=bold,underscore>foo</>');

注意:如果您需要以字面方式渲染标签,请使用反斜杠转义:<info> 或使用 escape() 方法转义给定字符串中包含的所有标签。

您可以使用 formatAndWrap(string $message, int $width) 函数将文本包装到特定宽度,0 表示不包装。

注意:如果您想检查终端的颜色支持,请查看示例文件夹。

显示可点击链接

命令可以使用特殊的 <href> 标签显示像网页中的 <a> 元素一样的链接。

$formatter->format('<href=https://narrowspark.com>Narrowspark Homepage</>');

如果您的终端属于 支持链接的终端仿真器列表,您可以通过单击“Narrowspark Homepage”文本在默认浏览器中打开其URL。否则,您将看到“Narrowspark Homepage”作为普通文本,URL将丢失。

终端工具

您想知道,您的终端支持哪些颜色或多少列,您可以调用

<?php

declare(strict_types=1);

use Testomat\TerminalColour\Util;

echo Util::getNumberOfColumns(); // returns the number of the columns

echo Util::getSupportedColor(); // returns the supported color as int

链接

3/4位颜色

XVilka/TrueColour.md

终端仿真器中的超链接(即类似HTML的锚点)

版本控制

此库遵循语义版本控制,代码规则集的增加在主要版本中进行。

变更日志

请查看CHANGELOG.md

贡献

请查看CONTRIBUTING.md

行为准则

请查看CODE_OF_CONDUCT.md

许可证

本软件包使用MIT许可证授权。

请查看LICENSE.md