LimeOutput.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Prints text on the console in different formats.
  13. *
  14. * You can use the various methods in this class to print nicely formatted
  15. * text message in the console. If the console does not support text formatting,
  16. * text formatting is suppressed, unless you pass the argument $forceColors=TRUE
  17. * in the constructor.
  18. *
  19. * @package symfony
  20. * @subpackage lime
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  23. * @version SVN: $Id$
  24. */
  25. class LimeOutput
  26. {
  27. const
  28. ERROR = 'ERROR',
  29. INFO = 'INFO',
  30. PARAMETER = 'PARAMETER',
  31. COMMENT = 'COMMENT',
  32. GREEN_BAR = 'GREEN_BAR',
  33. RED_BAR = 'RED_BAR',
  34. INFO_BAR = 'INFO_BAR';
  35. protected static
  36. $styles = array(self::ERROR, self::INFO, self::PARAMETER, self::COMMENT, self::GREEN_BAR, self::RED_BAR, self::INFO_BAR);
  37. protected
  38. $colorizer = null;
  39. /**
  40. * Constructor.
  41. *
  42. * @param boolean $forceColors If set to TRUE, colorization will be enforced
  43. * whether or not the current console supports it
  44. */
  45. public function __construct($forceColors = false)
  46. {
  47. if (LimeColorizer::isSupported() || $forceColors)
  48. {
  49. $colorizer = new LimeColorizer();
  50. $colorizer->setStyle(self::ERROR, array('bg' => 'red', 'fg' => 'white', 'bold' => true));
  51. $colorizer->setStyle(self::INFO, array('fg' => 'green', 'bold' => true));
  52. $colorizer->setStyle(self::PARAMETER, array('fg' => 'cyan'));
  53. $colorizer->setStyle(self::COMMENT, array('fg' => 'yellow'));
  54. $colorizer->setStyle(self::GREEN_BAR, array('fg' => 'white', 'bg' => 'green', 'bold' => true));
  55. $colorizer->setStyle(self::RED_BAR, array('fg' => 'white', 'bg' => 'red', 'bold' => true));
  56. $colorizer->setStyle(self::INFO_BAR, array('fg' => 'cyan', 'bold' => true));
  57. $this->colorizer = $colorizer;
  58. }
  59. }
  60. /**
  61. * Colorizes the given text with the given style.
  62. *
  63. * @param string $text Some text
  64. * @param string $style One of the predefined style constants
  65. * @return string The formatted text
  66. */
  67. protected function colorize($text, $style)
  68. {
  69. if (!in_array($style, self::$styles))
  70. {
  71. throw new InvalidArgumentException(sprintf('The style "%s" does not exist', $style));
  72. }
  73. return is_null($this->colorizer) ? $text : $this->colorizer->colorize($text, $style);
  74. }
  75. /**
  76. * ?
  77. */
  78. public function diag()
  79. {
  80. $messages = func_get_args();
  81. foreach ($messages as $message)
  82. {
  83. echo $this->colorize('# '.join("\n# ", (array) $message), self::COMMENT)."\n";
  84. }
  85. }
  86. /**
  87. * Prints a comment.
  88. *
  89. * @param string $message
  90. */
  91. public function comment($message)
  92. {
  93. echo $this->colorize(sprintf('# %s', $message), self::COMMENT)."\n";
  94. }
  95. /**
  96. * Prints an informational message.
  97. *
  98. * @param string $message
  99. */
  100. public function info($message)
  101. {
  102. echo $this->colorize(sprintf('> %s', $message), self::INFO_BAR)."\n";
  103. }
  104. /**
  105. * Prints an error.
  106. *
  107. * @param string $message
  108. */
  109. public function error($message)
  110. {
  111. echo $this->colorize(sprintf(' %s ', $message), self::RED_BAR)."\n";
  112. }
  113. /**
  114. * Prints and automatically colorizes a line.
  115. *
  116. * You can wrap the whole line into a specific predefined style by passing
  117. * the style constant in the second parameter.
  118. *
  119. * @param string $message The message to colorize
  120. * @param string $style The desired style constant
  121. * @param boolean $colorize Whether to automatically colorize parts of the
  122. * line
  123. */
  124. public function echoln($message, $style = null, $colorize = true)
  125. {
  126. if ($colorize)
  127. {
  128. $message = preg_replace('/(?:^|\.)((?:not ok|dubious) *\d*)\b/e', '$this->colorize(\'$1\', self::ERROR)', $message);
  129. $message = preg_replace('/(?:^|\.)(ok *\d*)\b/e', '$this->colorize(\'$1\', self::INFO)', $message);
  130. $message = preg_replace('/"(.+?)"/e', '$this->colorize(\'$1\', self::PARAMETER)', $message);
  131. $message = preg_replace('/(\->|\:\:)?([a-zA-Z0-9_]+?)\(\)/e', '$this->colorize(\'$1$2()\', self::PARAMETER)', $message);
  132. }
  133. echo ($style ? $this->colorize($message, $style) : $message)."\n";
  134. }
  135. /**
  136. * Prints a message in a green box.
  137. *
  138. * @param string $message
  139. */
  140. public function greenBar($message)
  141. {
  142. echo $this->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), self::GREEN_BAR)."\n";
  143. }
  144. /**
  145. * Prints a message a in a red box.
  146. *
  147. * @param string $message
  148. */
  149. public function redBar($message)
  150. {
  151. echo $this->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), self::RED_BAR)."\n";
  152. }
  153. }