Development

/branches/1.2/lib/command/sfAnsiColorFormatter.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/command/sfAnsiColorFormatter.class.php

Revision 17858, 4.1 kB (checked in by FabianLange, 5 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfAnsiColorFormatter provides methods to colorize text to be displayed on a console.
13  *
14  * @package    symfony
15  * @subpackage command
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfAnsiColorFormatter extends sfFormatter
20 {
21   protected
22     $styles = array(
23       'ERROR'    => array('bg' => 'red', 'fg' => 'white', 'bold' => true),
24       'INFO'     => array('fg' => 'green', 'bold' => true),
25       'COMMENT'  => array('fg' => 'yellow'),
26       'QUESTION' => array('bg' => 'cyan', 'fg' => 'black', 'bold' => false),
27     ),
28     $options    = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8),
29     $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37),
30     $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
31
32   /**
33    * Sets a new style.
34    *
35    * @param string $name    The style name
36    * @param array  $options An array of options
37    */
38   public function setStyle($name, $options = array())
39   {
40     $this->styles[$name] = $options;
41   }
42
43   /**
44    * Formats a text according to the given style or parameters.
45    *
46    * @param string   $text       The test to style
47    * @param mixed    $parameters An array of options or a style name
48    * @param resource $stream     The stream to format for
49    *
50    * @return string The styled text
51    */
52   public function format($text = '', $parameters = array(), $stream = STDOUT)
53   {
54     if (!$this->supportsColors($stream))
55     {
56       return $text;
57     }
58
59     if (!is_array($parameters) && 'NONE' == $parameters)
60     {
61       return $text;
62     }
63
64     if (!is_array($parameters) && isset($this->styles[$parameters]))
65     {
66       $parameters = $this->styles[$parameters];
67     }
68
69     $codes = array();
70     if (isset($parameters['fg']))
71     {
72       $codes[] = $this->foreground[$parameters['fg']];
73     }
74     if (isset($parameters['bg']))
75     {
76       $codes[] = $this->background[$parameters['bg']];
77     }
78     foreach ($this->options as $option => $value)
79     {
80       if (isset($parameters[$option]) && $parameters[$option])
81       {
82         $codes[] = $value;
83       }
84     }
85
86     return "\033[".implode(';', $codes).'m'.$text."\033[0m";
87   }
88
89   /**
90    * Formats a message within a section.
91    *
92    * @param string $section The section name
93    * @param string  $text     The text message
94    * @param integer $size     The maximum size allowed for a line (65 by default)
95    * @param string  $style    The color scheme to apply to the section string (INFO, ERROR, or COMMAND)
96    */
97   public function formatSection($section, $text, $size = null, $style = 'INFO')
98   {
99     $style = !array_key_exists($style, $this->styles) ? 'INFO' : $style;
100     
101     $width = 9 + strlen($this->format('', $style));
102
103     return sprintf(">> %-${width}s %s", $this->format($section, $style), $this->excerpt($text, $size));
104   }
105
106   /**
107    * Truncates a line.
108    *
109    * @param string  $text The text
110    * @param integer $size The maximum size of the returned string (65 by default)
111    *
112    * @return string The truncated string
113    */
114   public function excerpt($text, $size = null)
115   {
116     if (!$size)
117     {
118       $size = $this->size;
119     }
120
121     if (strlen($text) < $size)
122     {
123       return $text;
124     }
125
126     $subsize = floor(($size - 3) / 2);
127
128     return substr($text, 0, $subsize).$this->format('...', 'INFO').substr($text, -$subsize);
129   }
130
131   /**
132    * Returns true if the stream supports colorization.
133    *
134    * Colorization is disabled if not supported by the stream:
135    *
136    *  -  windows
137    *  -  non tty consoles
138    *
139    * @param mixed $stream A stream
140    *
141    * @return Boolean true if the stream supports colorization, false otherwise
142    */
143   public function supportsColors($stream)
144   {
145     return DIRECTORY_SEPARATOR != '\\' && function_exists('posix_isatty') && @posix_isatty($stream);
146   }
147 }
148
Note: See TracBrowser for help on using the browser.