| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfFormatter |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$size = null; |
|---|
| 23 |
|
|---|
| 24 |
function __construct($maxLineSize = null) |
|---|
| 25 |
{ |
|---|
| 26 |
if (null === $maxLineSize) |
|---|
| 27 |
{ |
|---|
| 28 |
if (function_exists('shell_exec')) |
|---|
| 29 |
{ |
|---|
| 30 |
|
|---|
| 31 |
$maxLineSize = ctype_digit(trim(shell_exec('tput cols 2>&1'))) ? (integer) shell_exec('tput cols') : 78; |
|---|
| 32 |
} |
|---|
| 33 |
else |
|---|
| 34 |
{ |
|---|
| 35 |
$maxLineSize = 78; |
|---|
| 36 |
} |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
$this->size = $maxLineSize; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Sets a new style. |
|---|
| 44 |
* |
|---|
| 45 |
* @param string $name The style name |
|---|
| 46 |
* @param array $options An array of options |
|---|
| 47 |
*/ |
|---|
| 48 |
public function setStyle($name, $options = array()) |
|---|
| 49 |
{ |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
* Formats a text according to the given parameters. |
|---|
| 54 |
* |
|---|
| 55 |
* @param string $text The test to style |
|---|
| 56 |
* @param mixed $parameters An array of parameters |
|---|
| 57 |
* |
|---|
| 58 |
* @return string The formatted text |
|---|
| 59 |
*/ |
|---|
| 60 |
public function format($text = '', $parameters = array()) |
|---|
| 61 |
{ |
|---|
| 62 |
return $text; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* Formats a message within a section. |
|---|
| 67 |
* |
|---|
| 68 |
* @param string $section The section name |
|---|
| 69 |
* @param string $text The text message |
|---|
| 70 |
* @param integer $size The maximum size allowed for a line |
|---|
| 71 |
*/ |
|---|
| 72 |
public function formatSection($section, $text, $size = null) |
|---|
| 73 |
{ |
|---|
| 74 |
if (!$size) |
|---|
| 75 |
{ |
|---|
| 76 |
$size = $this->size; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
$section = sprintf('>> %-9s ', $section); |
|---|
| 80 |
|
|---|
| 81 |
return $section.$this->excerpt($text, $size - strlen($section)); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* Truncates a line. |
|---|
| 86 |
* |
|---|
| 87 |
* @param string $text The text |
|---|
| 88 |
* @param integer $size The maximum size of the returned string |
|---|
| 89 |
* |
|---|
| 90 |
* @return string The truncated string |
|---|
| 91 |
*/ |
|---|
| 92 |
public function excerpt($text, $size = null) |
|---|
| 93 |
{ |
|---|
| 94 |
if (!$size) |
|---|
| 95 |
{ |
|---|
| 96 |
$size = $this->size; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
if (strlen($text) < $size) |
|---|
| 100 |
{ |
|---|
| 101 |
return $text; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
$subsize = floor(($size - 3) / 2); |
|---|
| 105 |
|
|---|
| 106 |
return substr($text, 0, $subsize).'...'.substr($text, -$subsize); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
* Sets the maximum line size. |
|---|
| 111 |
* |
|---|
| 112 |
* @param integer $size The maximum line size for a message |
|---|
| 113 |
*/ |
|---|
| 114 |
public function setMaxLineSize($size) |
|---|
| 115 |
{ |
|---|
| 116 |
$this->size = $size; |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|