sfAnsiColorFormatter.class.php 4.2 KB

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