承接 enco/cli-styles 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

enco/cli-styles

Composer 安装命令:

composer require enco/cli-styles

包简介

Package to manage cli output styles (color, background, font style)

README 文档

README

With this library you can stylize CLI output

Available styles:

  • Bold
  • Italic
  • Blink
  • Underline
  • Cross line
  • Over line
  • Text color
  • Background color

How to use:

You can add different styles to different parts of text. For example, you specify two styles with names 'success' and 'error'. So your text must be like:

Some text without styles
<success>Text with success styles</success>
Some text without styles
<error>Text with error styles</error>
Some text without styles

NOTE: You can`t use tags hierarchy. This text will be incorrect:

Some text without styles
<success>Text with success styles
    <error>Text with error styles</error>
    Text with success styles
</success>
Some text without styles

In your code you must use 2 classes: \Enco\CliStyles and \Enco\Style

\Enco\Style uses to define style
You are obligated to set style name. Otherwise, the exception will be thrown
The name must match ^[a-z0-9_-]+$ regular expression
Available methods:

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;

Also, you can specify default style, that applies to text without tags
To do this you just need to specify \Enco\Style::DEFAULT_STYLE_NAME constant as name of style

\Enco\CliStyles uses to apply styles to text
You must add styles, that you want to use

Available methods:

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

You can use $cliStylesObject to echo text. Styles will be applied automatically. Example:

$cliStylesObject = new \Enco\CliStylesPool();
...
echo $cliStylesObject;

Also, you can escape tag:

<error>This is example of \<error>\</error> tags</error>

Output will be:

This is example of <error></error> tags

PHP code example:

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 cursor management

You can manage CLI cursor position. For ex. mov cursor or set absolute position

To manage CLI cursor you must use \Enco\CliCursor

Available methods:

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

You must be careful to use \Enco\CliStyles and \Enco\CliCursor, because \Enco\CliCursor in some cases can reset styles (when new text override styled text)

Code example:

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';

Output will be:

1
 2
  3
  Height: 52 lines. Width: 204 columns

You always can ask questions to <bednarsasha@gmail.com>

enco/cli-styles 适用场景与选型建议

enco/cli-styles 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 enco/cli-styles 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 enco/cli-styles 我们能提供哪些服务?
定制开发 / 二次开发

基于 enco/cli-styles 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2022-09-14