touki/console-colors

面向对象的控制台颜色游乐场

dev-master 2013-12-16 16:30 UTC

This package is auto-updated.

Last update: 2024-09-07 23:15:15 UTC


README

这个库只是一个用于在UNIX控制台中用面向对象的方式使用颜色的娱乐游乐场。

## 安装

  1. https://getcomposer.org.cn 获取Composer
  2. 在命令行中输入
composer.phar require touki/console-colors

完成

用法

Touki\ConsoleColors\Formatter 接口看起来像这样

<?php

/**
 * Formats colors into a string
 *
 * @param string         $string     String to format
 * @param ColorInterface $foreground Foreground color
 * @param ColorInterface $background Background color
 * @param StyleInterface $style      Style
 *
 * @return string Formatted color
 */
function format($string, ColorInterface $foreground, ColorInterface $background = null, StyleInterface $style = null);

?>

要选择 Touki\ConsoleColors\ColorInterface,您可以从 预定义列表 中选择任何一个。
请注意,Color\LightColor\Color 有以下特殊行为。

同样,对于 Touki\ConsoleColors\StyleInterface,您可以从 预定义列表 中选择任何一个。

<?php

use Touki\ConsoleColors\Formatter;
use Touki\ConsoleColors\Color;
use Touki\ConsoleColors\Style;

$formatter = new Formatter;

// Prints red text
echo $formatter->format('foobar', new Color\Red);

// Prints red text on blue background
echo $formatter->format('foobar', new Color\Red, new Color\Blue); 

// Prints underlined red text
echo $formatter->format('foobar', new Color\Red, null, new Style\Underline);

// Prints bold red text on blue background
echo $formatter->format('foobar', new Color\Red, new Color\Blue, new Style\Bold);

?>

Color\Light 颜色需要一个 ColorInterface 作为构造函数参数。(不能与自己组合)

<?php

use Touki\ConsoleColors\Formatter;
use Touki\ConsoleColors\Color;
use Touki\ConsoleColors\Style;

$formatter = new Formatter;

// Prints light red text
echo $formatter->format('foobar', new Color\Light(new Color\Red));

// Prints red text on light cyan background
echo $formatter->format('foobar', new Color\Red, new Light(new Color\Cyan)); 

// Prints lightblue text on grey background
echo $formatter->format('foobar', new Color\Light(new Color\Blue), new Light(new Color\Black)); 
?>