| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of the symfony package.
|
|
|
4 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
5 |
*
|
|
|
6 |
* For the full copyright and license information, please view the LICENSE
|
|
|
7 |
* file that was distributed with this source code.
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
/*
|
|
|
11 |
* $Id: AnsiColorFormatter.php 2702 2007-10-03 21:43:22Z Jonathan.Wage $
|
|
|
12 |
*
|
|
|
13 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
14 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
15 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
16 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
17 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
18 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
19 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
20 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
21 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
24 |
*
|
|
|
25 |
* This software consists of voluntary contributions made by many individuals
|
|
|
26 |
* and is licensed under the LGPL. For more information, see
|
|
|
27 |
* <http://www.doctrine-project.org>.
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Doctrine_AnsiColorFormatter provides methods to colorize text to be displayed on a console.
|
|
|
32 |
* This class was taken from the symfony-project source
|
|
|
33 |
*
|
|
|
34 |
* @package Doctrine
|
|
|
35 |
* @subpackage Cli
|
|
|
36 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
37 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
38 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
39 |
* @link www.doctrine-project.org
|
|
|
40 |
* @since 1.0
|
|
|
41 |
* @version $Revision: 4252 $
|
|
|
42 |
*/
|
|
|
43 |
class Doctrine_Cli_AnsiColorFormatter extends Doctrine_Cli_Formatter
|
|
|
44 |
{
|
|
|
45 |
protected
|
|
|
46 |
$_styles = array(
|
|
|
47 |
'HEADER' => array('fg' => 'black', 'bold' => true),
|
|
|
48 |
'ERROR' => array('bg' => 'red', 'fg' => 'white', 'bold' => true),
|
|
|
49 |
'INFO' => array('fg' => 'green', 'bold' => true),
|
|
|
50 |
'COMMENT' => array('fg' => 'yellow'),
|
|
|
51 |
),
|
|
|
52 |
$_options = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8),
|
|
|
53 |
$_foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37),
|
|
|
54 |
$_background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Sets a new style.
|
|
|
58 |
*
|
|
|
59 |
* @param string The style name
|
|
|
60 |
* @param array An array of options
|
|
|
61 |
*/
|
|
|
62 |
public function setStyle($name, $options = array())
|
|
|
63 |
{
|
|
|
64 |
$this->_styles[$name] = $options;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Formats a text according to the given style or parameters.
|
|
|
69 |
*
|
|
|
70 |
* @param string The test to style
|
|
|
71 |
* @param mixed An array of options or a style name
|
|
|
72 |
*
|
|
|
73 |
* @return string The styled text
|
|
|
74 |
*/
|
|
|
75 |
public function format($text = '', $parameters = array(), $stream = STDOUT)
|
|
|
76 |
{
|
|
|
77 |
if ( ! $this->supportsColors($stream)) {
|
|
|
78 |
return $text;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if ( ! is_array($parameters) && 'NONE' == $parameters) {
|
|
|
82 |
return $text;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if ( ! is_array($parameters) && isset($this->_styles[$parameters])) {
|
|
|
86 |
$parameters = $this->_styles[$parameters];
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$codes = array();
|
|
|
90 |
if (isset($parameters['fg'])) {
|
|
|
91 |
$codes[] = $this->_foreground[$parameters['fg']];
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if (isset($parameters['bg'])) {
|
|
|
95 |
$codes[] = $this->_background[$parameters['bg']];
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
foreach ($this->_options as $option => $value) {
|
|
|
99 |
if (isset($parameters[$option]) && $parameters[$option]) {
|
|
|
100 |
$codes[] = $value;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return "\033[".implode(';', $codes).'m'.$text."\033[0m";
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Formats a message within a section.
|
|
|
109 |
*
|
|
|
110 |
* @param string The section name
|
|
|
111 |
* @param string The text message
|
|
|
112 |
* @param integer The maximum size allowed for a line (65 by default)
|
|
|
113 |
*/
|
|
|
114 |
public function formatSection($section, $text, $size = null)
|
|
|
115 |
{
|
|
|
116 |
$width = 9 + strlen($this->format('', 'INFO'));
|
|
|
117 |
|
|
|
118 |
return sprintf(">> %-${width}s %s", $this->format($section, 'INFO'), $this->excerpt($text, $size));
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Truncates a line.
|
|
|
123 |
*
|
|
|
124 |
* @param string The text
|
|
|
125 |
* @param integer The maximum size of the returned string (65 by default)
|
|
|
126 |
*
|
|
|
127 |
* @return string The truncated string
|
|
|
128 |
*/
|
|
|
129 |
public function excerpt($text, $size = null)
|
|
|
130 |
{
|
|
|
131 |
if ( ! $size) {
|
|
|
132 |
$size = $this->size;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if (strlen($text) < $size) {
|
|
|
136 |
return $text;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
$subsize = floor(($size - 3) / 2);
|
|
|
140 |
|
|
|
141 |
return substr($text, 0, $subsize) . $this->format('...', 'INFO').substr($text, -$subsize);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Returns true if the stream supports colorization.
|
|
|
146 |
*
|
|
|
147 |
* Colorization is disabled if not supported by the stream:
|
|
|
148 |
*
|
|
|
149 |
* - windows
|
|
|
150 |
* - non tty consoles
|
|
|
151 |
*
|
|
|
152 |
* @param mixed A stream
|
|
|
153 |
*
|
|
|
154 |
* @return Boolean true if the stream supports colorization, false otherwise
|
|
|
155 |
*/
|
|
|
156 |
public function supportsColors($stream)
|
|
|
157 |
{
|
|
|
158 |
return DIRECTORY_SEPARATOR != '\\' && function_exists('posix_isatty') && @posix_isatty($stream);
|
|
|
159 |
}
|
|
|
160 |
}
|