PHP Class Command Line Colors (bash,cmd)

PHP 21NN 1年前 (2018-05-24) 46次浏览 已收录 0个评论 扫描二维码

PHP Class Command Line Colors (bash,cmd)

color.class.php

<?php

class Colors {
private $foreground_colors = array();
private $background_colors = array();

public function __construct() {
// Set up shell colors
$this->foreground_colors[‘black’] = ‘0;30’;
$this->foreground_colors[‘dark_gray’] = ‘1;30’;
$this->foreground_colors[‘blue’] = ‘0;34’;
$this->foreground_colors[‘light_blue’] = ‘1;34’;
$this->foreground_colors[‘green’] = ‘0;32’;
$this->foreground_colors[‘light_green’] = ‘1;32’;
$this->foreground_colors[‘cyan’] = ‘0;36’;
$this->foreground_colors[‘light_cyan’] = ‘1;36’;
$this->foreground_colors[‘red’] = ‘0;31’;
$this->foreground_colors[‘light_red’] = ‘1;31’;
$this->foreground_colors[‘purple’] = ‘0;35’;
$this->foreground_colors[‘light_purple’] = ‘1;35’;
$this->foreground_colors[‘brown’] = ‘0;33’;
$this->foreground_colors[‘yellow’] = ‘1;33’;
$this->foreground_colors[‘light_gray’] = ‘0;37’;
$this->foreground_colors[‘white’] = ‘1;37’;

$this->background_colors[‘black’] = ’40’;
$this->background_colors[‘red’] = ’41’;
$this->background_colors[‘green’] = ’42’;
$this->background_colors[‘yellow’] = ’43’;
$this->background_colors[‘blue’] = ’44’;
$this->background_colors[‘magenta’] = ’45’;
$this->background_colors[‘cyan’] = ’46’;
$this->background_colors[‘light_gray’] = ’47’;
}

// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = “”;

// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= “33[” . $this->foreground_colors[$foreground_color] . “m”;
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= “33[” . $this->background_colors[$background_color] . “m”;
}

// Add string and end coloring
$colored_string .= $string . “33[0m”;

return $colored_string;
}

// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}

// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}

?>

banner.php

<?php
include(“color.class.php”);

$colors = new Colors();

$a=$colors->getColoredString(” ## ### “, “light_red”);

$x=$b=$colors->getColoredString(” c0d3b3:”, “light_red”,”red”);


$b=$colors->getColoredString(“lost”, “light_red”,”red”);

$c=$colors->getColoredString(“wolf “, “light_red”,”red”);
echo “rn”;

echo ” $x”.”$b”.”$c”;

echo “nrnrnrnrn”;

echo ” ######## #”.”n”;
echo ” ################# #”.”n”;
echo ” ###################### #”.”n”;
echo ” ######################### #”.”n”;
echo ” ############################”.”n”;
echo ” ##############################”.”n”;
echo ” ###############################”.”n”;
echo ” ###############################”.”n”;
echo ” ##############################”.”n”;
echo ” # ######## #”;
echo ” n $a”;echo”#### ##”.”n”;
echo ” ### ###”.”n”;
echo ” #### ###”.”n”;
echo ” #### ########## ####”.”n”;
echo ” ####################### ####”.”n”;
echo ” #################### ####”.”n”;
echo ” ################## ####”.”n”;
echo ” ############ ##”.”n”;
echo ” ######## ###”.”n”;
echo ” ######### #####”.”n”;
echo ” ############ ######”.”n”;
echo ” ######## #########”.”n”;
echo ” ##### ########”.”n”;
echo ” ### #########”.”n”;
echo ” ###### ############”.”n”;
echo ” #######################”.”n”;
echo ” # # ### # # ##”.”n”;
echo ” ########################”.”n”;
echo ” ## ## ## ##”.”rnrn”;

?>

Colors class basic usage examples

<?php

// Create new Colors class
$colors = new Colors();

// Test some basic printing with Colors class
echo $colors->getColoredString(“Testing Colors class, this is purple string on yellow background.”, “purple”, “yellow”) . “n”;
echo $colors->getColoredString(“Testing Colors class, this is blue string on light gray background.”, “blue”, “light_gray”) . “n”;
echo $colors->getColoredString(“Testing Colors class, this is red string on black background.”, “red”, “black”) . “n”;
echo $colors->getColoredString(“Testing Colors class, this is cyan string on green background.”, “cyan”, “green”) . “n”;
echo $colors->getColoredString(“Testing Colors class, this is cyan string on default background.”, “cyan”) . “n”;
echo $colors->getColoredString(“Testing Colors class, this is default string on cyan background.”, null, “cyan”) . “n”;

?>

All Foreground and background colors printed

<?php

// Create new Colors class
$colors = new Colors();

// Get Foreground Colors
$fgs = $colors->getForegroundColors();
// Get Background Colors
$bgs = $colors->getBackgroundColors();

// Loop through all foreground and background colors
$count = count($fgs);
for ($i = 0; $i < $count; $i++) {
echo $colors->getColoredString(“Test Foreground colors”, $fgs[$i]) . “t”;
if (isset($bgs[$i])) {
echo $colors->getColoredString(“Test Background colors”, null, $bgs[$i]);
}
echo “n”;
}
echo “n”;

// Loop through all foreground and background colors
foreach ($fgs as $fg) {
foreach ($bgs as $bg) {
echo $colors->getColoredString(“Test Colors”, $fg, $bg) . “t”;
}
echo “n”;
}

?>

//////////////////////////////////////////////////////////

ANSI Command Line Colors under Windows

Having colored text in the command line is a great help for spotting error or success messages. Unfortunately, those of us developing under Windows do not have this feature by default. Here’s how to enable it:

  1. Download ANSICON from http://adoxa.110mb.com/ansicon/index.html
  2. Extract the proper files (Depending on if you have a 32 or 64 bit machine) to c:ansicon (For example). I have a 32 bit machine and hence I extracted the files from inside the x86 folder.
  3. Open a command line prompt and go to c:ansicon, and then type “ansicon -i” without the quotes
  4. Done

You can now enjoy the colored output of PHPUnit for example.

摘自 lostwolf’s blog


本文:PHP Class Command Line Colors (bash,cmd) 本文链接:https://www.21nn.cn/biancheng/phpbiancheng/6987.html 本站所以图片、文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如有侵权请邮件与我们联系处理。i@ki4.cn
喜欢 (0)
[1353713598@qq.com]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址