Development

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

You must first sign up to be able to contribute.

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

Revision 9652, 4.1 kB (checked in by nicolas, 6 years ago)

1.1: added the ability to set the style used when calling the logSection method in task classes (backward compatible)

  • 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     ),
27     $options    = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8),
28     $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37),
29     $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
30
31   /**
32    * Sets a new style.
33    *
34    * @param string $name    The style name
35    * @param array  $options An array of options
36    */
37   public function setStyle($name, $options = array())
38   {
39     $this->styles[$name] = $options;
40   }
41
42   /**
43    * Formats a text according to the given style or parameters.
44    *
45    * @param  string   $text       The test to style
46    * @param  mixed    $parameters An array of options or a style name
47    * @param  resource $stream     The stream to format for
48    *
49    * @return string The styled text
50    */
51   public function format($text = '', $parameters = array(), $stream = STDOUT)
52   {
53     if (!$this->supportsColors($stream))
54     {
55       return $text;
56     }
57
58     if (!is_array($parameters) && 'NONE' == $parameters)
59     {
60       return $text;
61     }
62
63     if (!is_array($parameters) && isset($this->styles[$parameters]))
64     {
65       $parameters = $this->styles[$parameters];
66     }
67
68     $codes = array();
69     if (isset($parameters['fg']))
70     {
71       $codes[] = $this->foreground[$parameters['fg']];
72     }
73     if (isset($parameters['bg']))
74     {
75       $codes[] = $this->background[$parameters['bg']];
76     }
77     foreach ($this->options as $option => $value)
78     {
79       if (isset($parameters[$option]) && $parameters[$option])
80       {
81         $codes[] = $value;
82       }
83     }
84
85     return "\033[".implode(';', $codes).'m'.$text."\033[0m";
86   }
87
88   /**
89    * Formats a message within a section.
90    *
91    * @param string  $section  The section name
92    * @param string  $text     The text message
93    * @param integer $size     The maximum size allowed for a line (65 by default)
94    * @param string  $style    The color scheme to apply to the section string (INFO, ERROR, or COMMAND)
95    */
96   public function formatSection($section, $text, $size = null, $style = 'INFO')
97   {
98     $style = !array_key_exists($style, $this->styles) ? 'INFO' : $style;
99     
100     $width = 9 + strlen($this->format('', $style));
101
102     return sprintf(">> %-${width}s %s", $this->format($section, $style), $this->excerpt($text, $size));
103   }
104
105   /**
106    * Truncates a line.
107    *
108    * @param string  $text The text
109    * @param integer $size The maximum size of the returned string (65 by default)
110    *
111    * @return string The truncated string
112    */
113   public function excerpt($text, $size = null)
114   {
115     if (!$size)
116     {
117       $size = $this->size;
118     }
119
120     if (strlen($text) < $size)
121     {
122       return $text;
123     }
124
125     $subsize = floor(($size - 3) / 2);
126
127     return substr($text, 0, $subsize).$this->format('...', 'INFO').substr($text, -$subsize);
128   }
129
130   /**
131    * Returns true if the stream supports colorization.
132    *
133    * Colorization is disabled if not supported by the stream:
134    *
135    *  -  windows
136    *  -  non tty consoles
137    *
138    * @param  mixed  $stream  A stream
139    *
140    * @return Boolean true if the stream supports colorization, false otherwise
141    */
142   public function supportsColors($stream)
143   {
144     return DIRECTORY_SEPARATOR != '\\' && function_exists('posix_isatty') && @posix_isatty($stream);
145   }
146 }
147
Note: See TracBrowser for help on using the browser.