magallanes/Mage/Console/Colors.php

79 lines
1.8 KiB
PHP
Raw Normal View History

2011-11-22 12:16:06 +01:00
<?php
2013-11-05 17:12:09 +01:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Console;
2013-12-19 16:07:57 +01:00
use Mage\Config;
/**
* Parses the different colors available for the Terminal or Console.
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class Colors
{
2014-08-06 19:01:39 +02:00
/**
* List of Colors and they Terminal/Console representation.
* @var array
*/
private static $foregroundColors = array(
2014-08-06 19:01:39 +02:00
'black' => '0;30',
2014-11-01 22:31:04 +01:00
'bold' => '1',
2014-08-06 19:01:39 +02:00
'blue' => '0;34',
'light_blue' => '1;34',
'green' => '0;32',
'light_green' => '1;32',
'cyan' => '0;36',
'light_cyan' => '1;36',
'red' => '0;31',
'light_red' => '1;31',
'purple' => '0;35',
'light_purple' => '1;35',
2014-08-06 19:01:39 +02:00
'brown' => '0;33',
'yellow' => '1;33',
'light_gray' => '0;37',
'white' => '1;37'
2013-11-05 17:12:09 +01:00
);
/**
* Parses a Text to represent Colors in the Terminal/Console.
*
* @param string $string
2014-08-06 19:00:11 +02:00
* @param Config $config
* @return string
*/
2013-12-19 16:07:57 +01:00
public static function color($string, Config $config)
{
2014-08-06 19:01:39 +02:00
$disabled = $config->getParameter('no-color', !$config->general('colors', true));
2013-12-19 16:07:57 +01:00
2014-08-06 19:01:39 +02:00
if ($disabled) {
$string = strip_tags($string);
return $string;
}
2013-12-19 16:07:57 +01:00
foreach (self::$foregroundColors as $key => $code) {
$replaceFrom = array(
'<' . $key . '>',
2014-08-06 19:01:39 +02:00
'</' . $key . '>'
);
$replaceTo = array(
"\033[" . $code . 'm',
"\033[0m"
);
2013-11-05 17:12:09 +01:00
$string = str_replace($replaceFrom, $replaceTo, $string);
}
return $string;
}
2011-11-22 12:16:06 +01:00
}