sfAnsiColorFormatter.class.php 4.1 KB

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