1. sfAnsiColorFormatter.class.php
  2. /** * sfAnsiColorFormatter provides methods to colorize text to be displayed on a console. * * @package symfony * @subpackage command * @author Fabien Potencier * @version SVN: $Id: sfAnsiColorFormatter.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfAnsiColorFormatter extends sfFormatter
  4. {
  5. protected
  6. $styles = array(
  7. 'ERROR' => array('bg' => 'red', 'fg' => 'white', 'bold' => true),
  8. 'INFO' => array('fg' => 'green', 'bold' => true),
  9. 'COMMENT' => array('fg' => 'yellow'),
  10. 'QUESTION' => array('bg' => 'cyan', 'fg' => 'black', 'bold' => false),
  11. ),
  12. $options = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8),
  13. $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37),
  14. $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
  15. /**
  16. * Sets a new style.
  17. *
  18. * @param string $name The style name
  19. * @param array $options An array of options
  20. */
  21. public function setStyle($name, $options = array())
  22. {
  23. $this->styles[$name] = $options;
  24. }
  25. /**
  26. * Formats a text according to the given style or parameters.
  27. *
  28. * @param string $text The test to style
  29. * @param mixed $parameters An array of options or a style name
  30. *
  31. * @return string The styled text
  32. */
  33. public function format($text = '', $parameters = array())
  34. {
  35. if (!is_array($parameters) && 'NONE' == $parameters)
  36. {
  37. return $text;
  38. }
  39. if (!is_array($parameters) && isset($this->styles[$parameters]))
  40. {
  41. $parameters = $this->styles[$parameters];
  42. }
  43. $codes = array();
  44. if (isset($parameters['fg']))
  45. {
  46. $codes[] = $this->foreground[$parameters['fg']];
  47. }
  48. if (isset($parameters['bg']))
  49. {
  50. $codes[] = $this->background[$parameters['bg']];
  51. }
  52. foreach ($this->options as $option => $value)
  53. {
  54. if (isset($parameters[$option]) && $parameters[$option])
  55. {
  56. $codes[] = $value;
  57. }
  58. }
  59. return "\033[".implode(';', $codes).'m'.$text."\033[0m";
  60. }
  61. /**
  62. * Formats a message within a section.
  63. *
  64. * @param string $section The section name
  65. * @param string $text The text message
  66. * @param integer $size The maximum size allowed for a line
  67. * @param string $style The color scheme to apply to the section string (INFO, ERROR, COMMENT or QUESTION)
  68. */
  69. public function formatSection($section, $text, $size = null, $style = 'INFO')
  70. {
  71. if (null === $size)
  72. {
  73. $size = $this->size;
  74. }
  75. $style = array_key_exists($style, $this->styles) ? $style : 'INFO';
  76. $width = 9 + strlen($this->format('', $style));
  77. return sprintf(">> %-{$width}s %s", $this->format($section, $style), $this->excerpt($text, $size - 4 - (strlen($section) > 9 ? strlen($section) : 9)));
  78. }
  79. /**
  80. * Truncates a line.
  81. *
  82. * @param string $text The text
  83. * @param integer $size The maximum size of the returned string
  84. *
  85. * @return string The truncated string
  86. */
  87. public function excerpt($text, $size = null)
  88. {
  89. if (!$size)
  90. {
  91. $size = $this->size;
  92. }
  93. if (strlen($text) < $size)
  94. {
  95. return $text;
  96. }
  97. $subsize = floor(($size - 3) / 2);
  98. return substr($text, 0, $subsize).$this->format('...', 'INFO').substr($text, -$subsize);
  99. }
  100. }

Debug toolbar