win0err/php-terminal-tools

PHP 终端工具

1.1.3 2017-04-14 16:15 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:19:47 UTC


README

Travis Latest Stable Version

PHP 终端工具简化了在用 PHP 编写的项目中处理终端的操作。

安装

要安装,请使用 Composer

composer require win0err/php-terminal-tools

使用

<?php
use win0err\TerminalTools\TextFormatter;
use win0err\TerminalTools\Colors\{Classic, Extended, TrueColor};

$textFormatter = new TextFormatter( "Съешь ещё этих мягких французских булок, да выпей чаю" );
echo $textFormatter;
// или сокращённая запись: 
echo TextFormatter::format( 
	"В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!" /* $text */, // Обязательно
	new Extended(198) /* $textColor = null */, 
	new Extended(200) /* $backgroundColor = null*/, 
	TextFormatter::BOLD /* ...$styles */ 
	);

文字样式

使用 setStyles 方法安装样式。支持以下 6 种样式:粗体、灰色、下划线、闪烁、反转、隐藏。为了方便,它们都在 TextFormatter 类中预定义。

$textFormatter->setStyles( TextFormatter::UNDERLINE );

样式可以组合使用

$textFormatter->setStyles( TextFormatter::BOLD, TextFormatter::UNDERLINE );
// или
$textFormatter->setStyles( TextFormatter::BOLD );
$textFormatter->setStyles( TextFormatter::UNDERLINE );

文字颜色

使用 setTextColor 方法设置文字颜色,使用 setBackgroundColor 方法设置背景颜色。

经典调色板(16 种颜色)

在 Classic 类中,为了方便,预定义了该调色板所有可能颜色的常量。

$color = new Classic( Classic::MAGENTA );

$textFormatter
    ->setTextColor( $color )
    ->setBackgroundColor( new Classic( Classic::WHITE ) );

扩展调色板(256 种颜色)

在 Extended 类中,为了方便,预定义了调色板 16 种主要颜色的常量。其他颜色可以用 0 到 255 的数字指定。

$textFormatter
    ->setTextColor( new Extended( 198 ) )
    ->setBackgroundColor( new Extended( rand(0, 255) ) );

真彩色调色板(很少支持)

在 TrueColor 类中,为了方便,预定义了调色板主要颜色的常量。其他颜色可以用 HEX 代码或 0 到 255 范围内的红、绿、蓝值指定。

$textFormatter
    ->setTextColor( TrueColor::hex( TrueColor::REBECCA_PURPLE ) )
    ->setBackgroundColor( TrueColor::hex( "#fff" ) ) );

也可以用红、绿、蓝值指定

$textFormatter->setTextColor( new TrueColor( 255, 255, 255 ) );

示例

PHP Terminal Tools

示例代码

<?php
require 'vendor/autoload.php';

use win0err\TerminalTools\TextFormatter;
use win0err\TerminalTools\Colors\{
	Classic, Extended, TrueColor
};

$textFormatter = new TextFormatter( "  " );

echo 'System colors:' . PHP_EOL . PHP_EOL;
for( $i = 30; $i < 38; $i++ )
	echo $textFormatter->setBackgroundColor( new Classic( $i ) );
echo PHP_EOL;
for( $i = 90; $i < 98; $i++ )
	echo $textFormatter->setBackgroundColor( new Classic( $i ) );
echo PHP_EOL . PHP_EOL . PHP_EOL;


echo 'Extended palette: ' . PHP_EOL . PHP_EOL;
for( $green = 0; $green < 6; $green++ ) {
	for( $red = 0; $red < 6; $red++ ) {
		for( $blue = 0; $blue < 6; $blue++ ) 
			echo $textFormatter->setBackgroundColor( new Extended( 16 + ($red * 36) + ($green * 6) + $blue ) );
		
		echo " ";
	}
	echo PHP_EOL;
}
echo PHP_EOL;
for( $color = 232; $color < 256; $color++ )
	echo $textFormatter->setBackgroundColor( new Extended( $color ) );
echo PHP_EOL . PHP_EOL . PHP_EOL;

echo 'TrueColor palette (partially): ' . PHP_EOL . PHP_EOL;
for( $green = 0; $green < 256; $green += 51 ) {
	for( $red = 0; $red < 256; $red += 51 ) {
		for( $blue = 0; $blue < 256; $blue += 51 )
			echo $textFormatter->setBackgroundColor( new TrueColor( $red, $green, $blue ) );
		
		echo " ";
	}
	echo PHP_EOL;
}
echo PHP_EOL;