bebat/console-color
Composer 安装命令:
composer require bebat/console-color
包简介
Apply colors & styles to text for command line output
README 文档
README
Console Color is a lightweight PHP 8.1+ library for adding color & other styles to text in the command line.
Installation
The Console Color library can be installed via Composer:
composer require bebat/console-color
Basic Usage
To apply style to a text you need an instance of BeBat\ConsoleColor\Style. With that you can apply a style object to some text:
use BeBat\ConsoleColor\Style; $style = new Style(); echo $style->apply('This text is green', Style\Color::Green) . PHP_EOL;
The Style class will try to determine if console output can be styled automatically (more details on this can be found in the Environment Variables section below). By default, it looks at STDOUT and checks if it is a TTY. If you are using a different output type (such as a php:// I/O stream) you may pass that to Style() to have it determine if that stream supports styling:
use BeBat\ConsoleColor\Style; $handle = fopen($something, 'w'); $style = new Style($handle); fwrite($handle, $style->apply( 'If $something points to a console this text will have a cyan background. ' . 'Otherwise it will just be plain.', Style\BackgroundColor::Cyan, ));
You may use the force() method to make Style always apply styles to text, regardless of whether it thinks the output resource supports it:
use BeBat\ConsoleColor\Style; $handle = fopen($something, 'w'); $style = new Style($handle); $style->force(); fwrite($handle, $style->apply( 'This text will always have styling applied to it.', Style\Text::Bold, ));
Environment Variables
In addition to checking if STDOUT is a TTY, Style will look at several environment variables to determine if styles should be applied, and what level of support the user's terminal has for styles.
FORCE_COLOR- If the user has setFORCE_COLORstyling will be applied, even ifStyledoesn't think the output is a TTY.NO_COLOR- If the user has setNO_COLORstyling will be disabled.NO_COLORtakes precedence overFORCE_COLOR.TERM-Stylewill checkTERMto see if it supports 256 colors.COLORTERM- IfCOLORTERMis set totruecolorthenStylewill apply RGB based colors.
Included Styles
Style::apply() accepts an instance of StyleInterface, and Console Color includes a number of styles that implement this interface.
To see what styles your terminal supports and what they look like, run the included sample.php file.
Basic Styles
Style\Text, Style\Underline, Style\Color, and Style\BackgroundColor are backed enumerations which helps to make Console Color's API extremely clean. These styles have the broadest support in most terminals (with some exceptions).
Text
Style\Text::None- no styleStyle\Text::Bold- bold text (may also "brighten" the color in some terminals)Style\Text::Faint- dim text colorStyle\Text::Italic- italicsStyle\Text::Underline- underlineStyle\Text::Blink- blinking text (use wisely 😉)Style\Text::Reverse- swap foreground and background colorsStyle\Text::Concealed- hides the text (it is still present and can be selected)Style\Text::Strike- strikethrough, not supported in all terminalsStyle\Text::DoubleUnderline- double or bold underline, not supported in all terminalsStyle\Text::Overline- line over text, not supported in all terminalsStyle\Text::Superscript- superscript text, rarely supportedStyle\Text::Subscript- subscript text, rarely supported
Underline
Custom underline styles are not widely supported but some terminals do include them. Most, if not all, will fall back on just displaying a single underline.
Style\Underline::SingleStyle\Underline::DoubleStyle\Underline::WavyStyle\Underline::DottedStyle\Underline::Dashed
Foreground & Background Color
Style\Color and Style\BackgroundColor both support the same values and have an identical API.
Style\Color::BlackStyle\Color::RedStyle\Color::GreenStyle\Color::YellowStyle\Color::BlueStyle\Color::MagentaStyle\Color::CyanStyle\Color::WhiteStyle\Color::DefaultStyle\Color::BrightBlackStyle\Color::BrightRedStyle\Color::BrightGreenStyle\Color::BrightYellowStyle\Color::BrightBlueStyle\Color::BrightMagentaStyle\Color::BrightCyanStyle\Color::BrightWhite
256 & True Color
Most terminal programs support 256 color output, and many now also support 24-bit "true" colors. Console Color can apply those colors to the foreground, background, or underline color through the Style\Color256 and Style\ColorRGB classes.
Style\Color256::foreground(int $code)- apply a 256 color to the textStyle\Color256::background(int $code)- apply a 256 color to the backgroundStyle\Color256::underline(int $code)- apply a 256 color to an underline (see composite styles below)Style\ColorRGB::foreground(int $red, int $green, int $blue)- apply an RGB color to the textStyle\ColorRGB::background(int $red, int $green, int $blue)- apply an RGB color to the backgroundStyle\ColorRGB::underline(int $red, int $green, int $blue)- apply an RGB color to an underline (see composite styles below)
Composite Styles
It's possible to apply more than one style to your text by using the Style\Composite() class. For example, to apply color to both the foreground and background:
use BeBat\ConsoleColor\Style; $style = new Style(); echo $style->apply( 'This text is white on red', new Style\Composite(Style\Color::BrightWhite, Style\BackgroundColor::Red), ) . PHP_EOL;
Style\Composite() is also required if you want to add color to an underline:
use BeBat\ConsoleColor\Style; $style = new Style(); echo $style->apply( 'This text has a typo', new Style\Composite(Style\Underline::Wavy, Style\ColorRGB::underline(255, 0, 0)), ) . PHP_EOL;
Style\Composite() can actually take as many styles as you need to apply:
use BeBat\ConsoleColor\Style; $style = new Style(); echo $style->apply( "Please don't do this\n", new Style\Composite( Style\Text::Bold, Style\Text::Italic, Style\Text::Blink, Style\Color256::background(34), Style\Color256::foreground(227), ), );
bebat/console-color 适用场景与选型建议
bebat/console-color 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.37k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cli」 「console」 「command line」 「color」 「style」 「ansi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bebat/console-color 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bebat/console-color 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bebat/console-color 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This class provides you with an easy-to-use interface to progress bars.
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
A Laravel package to retrieve data from Google Search Console
Additional artisan commands for Laravel
Shell command module for PHP.
统计信息
- 总下载量: 1.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-11