1. sfFormatter.class.php
  2. /** * sfFormatter provides methods to format text to be displayed on a console. * * @package symfony * @subpackage command * @author Fabien Potencier * @version SVN: $Id: sfFormatter.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfFormatter
  4. {
  5. protected
  6. $size = null;
  7. function __construct($maxLineSize = null)
  8. {
  9. if (null === $maxLineSize)
  10. {
  11. // this is tricky because "tput cols 2>&1" is not accurate
  12. $maxLineSize = ctype_digit(trim(shell_exec('tput cols 2>&1'))) ? (integer) shell_exec('tput cols') : 78;
  13. }
  14. $this->size = $maxLineSize;
  15. }
  16. /**
  17. * Sets a new style.
  18. *
  19. * @param string $name The style name
  20. * @param array $options An array of options
  21. */
  22. public function setStyle($name, $options = array())
  23. {
  24. }
  25. /**
  26. * Formats a text according to the given parameters.
  27. *
  28. * @param string $text The test to style
  29. * @param mixed $parameters An array of parameters
  30. *
  31. * @return string The formatted text
  32. */
  33. public function format($text = '', $parameters = array())
  34. {
  35. return $text;
  36. }
  37. /**
  38. * Formats a message within a section.
  39. *
  40. * @param string $section The section name
  41. * @param string $text The text message
  42. * @param integer $size The maximum size allowed for a line
  43. */
  44. public function formatSection($section, $text, $size = null)
  45. {
  46. if (!$size)
  47. {
  48. $size = $this->size;
  49. }
  50. $section = sprintf('>> %-9s ', $section);
  51. return $section.$this->excerpt($text, $size - strlen($section));
  52. }
  53. /**
  54. * Truncates a line.
  55. *
  56. * @param string $text The text
  57. * @param integer $size The maximum size of the returned string
  58. *
  59. * @return string The truncated string
  60. */
  61. public function excerpt($text, $size = null)
  62. {
  63. if (!$size)
  64. {
  65. $size = $this->size;
  66. }
  67. if (strlen($text) < $size)
  68. {
  69. return $text;
  70. }
  71. $subsize = floor(($size - 3) / 2);
  72. return substr($text, 0, $subsize).'...'.substr($text, -$subsize);
  73. }
  74. /**
  75. * Sets the maximum line size.
  76. *
  77. * @param integer $size The maximum line size for a message
  78. */
  79. public function setMaxLineSize($size)
  80. {
  81. $this->size = $size;
  82. }
  83. }

Debug toolbar