jaswrks/chalk

一个用于设置终端输出的工具。

0.1.1 2020-12-05 04:10 UTC

This package is auto-updated.

Last update: 2024-09-05 13:13:48 UTC


README

Chalk启发,这是一个用于PHP设置终端输出的工具。

安装

通过composer
将chalk作为依赖项: composer require martin-pettersson/chalk
包含composer自动加载文件:require_once 'vendor/autoload.php'
手动
下载/克隆仓库
Chalk符合PSR-4规范,因此任何PSR-4自动加载器都适用,只需添加Chalk命名空间"Chalk"并将其指向Chalk/src目录。

使用

快速示例

您可以将其复制到.php文件中并测试。

<?php

require_once 'vendor/autoload.php';

use Chalk\Chalk;
use Chalk\Style;
use Chalk\Color;
use Chalk\BackgroundColor;

// use the style method to style the text
echo Chalk::style('This text is green', Color::GREEN) . PHP_EOL;

// convenience methods are available for colors (case insensitive)
echo Chalk::green('This text is also green') . PHP_EOL;
echo Chalk::Red('This text is red') . PHP_EOL;

// use a style object to compose styles
$style = new Style([Color::RED, BackgroundColor::GREEN, Style::UNDERLINED]);
echo Chalk::style('This text is red with green background', $style) . PHP_EOL;

// use the parse method for more advanced compositions
echo Chalk::parse('This text is {green}, {yellow}, {red}, {blue} and {something else}', [
  Color::GREEN,
  Color::YELLOW,
  Color::RED,
  Color::BLUE,
  $style
]) . PHP_EOL;

// you can nest styles
// note that the text at the end of the string is still green even after the background styling is reset
echo Chalk::Green('This text has some ' . Chalk::style('nested', BackgroundColor::WHITE) . ' styling') . PHP_EOL;

示例输出

example

方法

Chalk::style(string $string, mixed $style)
通过传递Color/BackgroundColor/Style或组合样式对象来设置字符串样式。

Chalk::parse(string $string, array $styles)
对于更高级的样式,请使用parse方法来设置字符串的特定部分。使用"{}"标签来定位字符串的一部分,并传递包含样式的数组。样式与找到的标签的索引相匹配。

便利方法
如果您使用Chalk::{COLOR}($string)与任何可用的颜色(不区分大小写),它将方便地映射到Chalk::style($string, {COLOR}),例如Chalk::Red($string) === Chalk::style($string, Color::RED)

可用样式

颜色

  • Color::NONE (默认)
  • Color::BLACK
  • Color::RED
  • Color::GREEN
  • Color::YELLOW
  • Color::BLUE
  • Color::MAGENTA
  • Color::CYAN
  • Color::LIGHT_GRAY
  • Color::DARK_GRAY
  • Color::LIGHT_RED
  • Color::LIGHT_GREEN
  • Color::LIGHT_YELLOW
  • Color::LIGHT_BLUE
  • Color::LIGHT_MAGENTA
  • Color::LIGHT_CYAN
  • Color::WHITE

背景颜色

  • BackgroundColor::NONE (默认)
  • BackgroundColor::BLACK
  • BackgroundColor::RED
  • BackgroundColor::GREEN
  • BackgroundColor::YELLOW
  • BackgroundColor::BLUE
  • BackgroundColor::MAGENTA
  • BackgroundColor::CYAN
  • BackgroundColor::LIGHT_GRAY
  • BackgroundColor::DARK_GRAY
  • BackgroundColor::LIGHT_RED
  • BackgroundColor::LIGHT_GREEN
  • BackgroundColor::LIGHT_YELLOW
  • BackgroundColor::LIGHT_BLUE
  • BackgroundColor::LIGHT_MAGENTA
  • BackgroundColor::LIGHT_CYAN
  • BackgroundColor::WHITE

样式

  • Style::BOLD
  • Style::DIM
  • Style::UNDERLINED
  • Style::BLINK
  • Style::INVERTED
  • Style::HIDDEN