Development

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

You must first sign up to be able to contribute.

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

Revision 21908, 3.6 kB (checked in by fabien, 5 years ago)

[1.3] made some optimizations by replacing is_null() by null ===

  • 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    *
49    * @return string The styled text
50    */
51   public function format($text = '', $parameters = array())
52   {
53     if (!is_array($parameters) && 'NONE' == $parameters)
54     {
55       return $text;
56     }
57
58     if (!is_array($parameters) && isset($this->styles[$parameters]))
59     {
60       $parameters = $this->styles[$parameters];
61     }
62
63     $codes = array();
64     if (isset($parameters['fg']))
65     {
66       $codes[] = $this->foreground[$parameters['fg']];
67     }
68     if (isset($parameters['bg']))
69     {
70       $codes[] = $this->background[$parameters['bg']];
71     }
72     foreach ($this->options as $option => $value)
73     {
74       if (isset($parameters[$option]) && $parameters[$option])
75       {
76         $codes[] = $value;
77       }
78     }
79
80     return "\033[".implode(';', $codes).'m'.$text."\033[0m";
81   }
82
83   /**
84    * Formats a message within a section.
85    *
86    * @param string  $section  The section name
87    * @param string  $text     The text message
88    * @param integer $size     The maximum size allowed for a line
89    * @param string  $style    The color scheme to apply to the section string (INFO, ERROR, COMMENT or QUESTION)
90    */
91   public function formatSection($section, $text, $size = null, $style = 'INFO')
92   {
93     if (null === $size)
94     {
95       $size = $this->size;
96     }
97
98     $style = array_key_exists($style, $this->styles) ? $style : 'INFO';
99     $width = 9 + strlen($this->format('', $style));
100
101     return sprintf(">> %-{$width}s %s", $this->format($section, $style), $this->excerpt($text, $size - 4 - (strlen($section) > 9 ? strlen($section) : 9)));
102   }
103
104   /**
105    * Truncates a line.
106    *
107    * @param string  $text The text
108    * @param integer $size The maximum size of the returned string
109    *
110    * @return string The truncated string
111    */
112   public function excerpt($text, $size = null)
113   {
114     if (!$size)
115     {
116       $size = $this->size;
117     }
118
119     if (strlen($text) < $size)
120     {
121       return $text;
122     }
123
124     $subsize = floor(($size - 3) / 2);
125
126     return substr($text, 0, $subsize).$this->format('...', 'INFO').substr($text, -$subsize);
127   }
128 }
129
Note: See TracBrowser for help on using the browser.