3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Console\Adapter
;
12 use Zend\Console\Charset
;
13 use Zend\Console\Exception
;
14 use Zend\Stdlib\StringUtils
;
17 * Common console adapter codebase
19 abstract class AbstractAdapter
implements AdapterInterface
22 * Whether or not mbstring is enabled
26 protected static $hasMBString;
29 * @var Charset\CharsetInterface
34 * Current cursor X position
41 * Current cursor Y position
48 * Write a chunk of text to console.
51 * @param null|int $color
52 * @param null|int $bgColor
54 public function write($text, $color = null, $bgColor = null)
56 if ($color !== null ||
$bgColor !== null) {
57 echo $this->colorize($text, $color, $bgColor);
67 * @param null|int $color
68 * @param null|int $bgColor
70 public function writeText($text, $color = null, $bgColor = null)
72 return $this->write($text, $color, $bgColor);
76 * Write a single line of text to console and advance cursor to the next line.
79 * @param null|int $color
80 * @param null|int $bgColor
82 public function writeLine($text = "", $color = null, $bgColor = null)
84 $this->write($text . PHP_EOL
, $color, $bgColor);
88 * Write a piece of text at the coordinates of $x and $y
91 * @param string $text Text to write
92 * @param int $x Console X coordinate (column)
93 * @param int $y Console Y coordinate (row)
94 * @param null|int $color
95 * @param null|int $bgColor
97 public function writeAt($text, $x, $y, $color = null, $bgColor = null)
99 $this->setPos($x, $y);
100 $this->write($text, $color, $bgColor);
104 * Write a box at the specified coordinates.
105 * If X or Y coordinate value is negative, it will be calculated as the distance from far right or bottom edge
106 * of the console (respectively).
108 * @param int $x1 Top-left corner X coordinate (column)
109 * @param int $y1 Top-left corner Y coordinate (row)
110 * @param int $x2 Bottom-right corner X coordinate (column)
111 * @param int $y2 Bottom-right corner Y coordinate (row)
112 * @param int $lineStyle (optional) Box border style.
113 * @param int $fillStyle (optional) Box fill style or a single character to fill it with.
114 * @param int $color (optional) Foreground color
115 * @param int $bgColor (optional) Background color
116 * @param null|int $fillColor (optional) Foreground color of box fill
117 * @param null|int $fillBgColor (optional) Background color of box fill
118 * @throws Exception\BadMethodCallException if coordinates are invalid
120 public function writeBox(
125 $lineStyle = self
::LINE_SINGLE
,
126 $fillStyle = self
::FILL_NONE
,
132 // Sanitize coordinates
138 // Translate negative coordinates
140 $x2 = $this->getWidth() - $x2;
144 $y2 = $this->getHeight() - $y2;
147 // Validate coordinates
153 throw new Exception\
BadMethodCallException('Supplied X,Y coordinates are invalid.');
156 // Determine charset and dimensions
157 $charset = $this->getCharset();
158 $width = $x2 - $x1 +
1;
159 $height = $y2 - $y1 +
1;
162 $lineStyle = static::LINE_NONE
;
165 // Activate line drawing
166 $this->write($charset::ACTIVATE
);
168 // Draw horizontal lines
169 if ($lineStyle !== static::LINE_NONE
) {
170 switch ($lineStyle) {
171 case static::LINE_SINGLE
:
172 $lineChar = $charset::LINE_SINGLE_EW
;
175 case static::LINE_DOUBLE
:
176 $lineChar = $charset::LINE_DOUBLE_EW
;
179 case static::LINE_BLOCK
:
181 $lineChar = $charset::LINE_BLOCK_EW
;
185 $this->setPos($x1 +
1, $y1);
186 $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
187 $this->setPos($x1 +
1, $y2);
188 $this->write(str_repeat($lineChar, $width - 2), $color, $bgColor);
191 // Draw vertical lines and fill
192 if (is_numeric($fillStyle)
193 && $fillStyle !== static::FILL_NONE
) {
195 switch ($fillStyle) {
196 case static::FILL_SHADE_LIGHT
:
197 $fillChar = $charset::SHADE_LIGHT
;
199 case static::FILL_SHADE_MEDIUM
:
200 $fillChar = $charset::SHADE_MEDIUM
;
202 case static::FILL_SHADE_DARK
:
203 $fillChar = $charset::SHADE_DARK
;
205 case static::FILL_BLOCK
:
207 $fillChar = $charset::BLOCK
;
211 } elseif ($fillStyle) {
212 $fillChar = StringUtils
::getWrapper()->substr($fillStyle, 0, 1);
217 if ($lineStyle === static::LINE_NONE
) {
218 for ($y = $y1; $y <= $y2; $y++
) {
219 $this->setPos($x1, $y);
220 $this->write(str_repeat($fillChar, $width), $fillColor, $fillBgColor);
223 switch ($lineStyle) {
224 case static::LINE_DOUBLE
:
225 $lineChar = $charset::LINE_DOUBLE_NS
;
227 case static::LINE_BLOCK
:
228 $lineChar = $charset::LINE_BLOCK_NS
;
230 case static::LINE_SINGLE
:
232 $lineChar = $charset::LINE_SINGLE_NS
;
236 for ($y = $y1 +
1; $y < $y2; $y++
) {
237 $this->setPos($x1, $y);
238 $this->write($lineChar, $color, $bgColor);
239 $this->write(str_repeat($fillChar, $width - 2), $fillColor, $fillBgColor);
240 $this->write($lineChar, $color, $bgColor);
246 if ($lineStyle !== static::LINE_NONE
) {
247 if ($color !== null) {
248 $this->setColor($color);
250 if ($bgColor !== null) {
251 $this->setBgColor($bgColor);
253 if ($lineStyle === static::LINE_SINGLE
) {
254 $this->writeAt($charset::LINE_SINGLE_NW
, $x1, $y1);
255 $this->writeAt($charset::LINE_SINGLE_NE
, $x2, $y1);
256 $this->writeAt($charset::LINE_SINGLE_SE
, $x2, $y2);
257 $this->writeAt($charset::LINE_SINGLE_SW
, $x1, $y2);
258 } elseif ($lineStyle === static::LINE_DOUBLE
) {
259 $this->writeAt($charset::LINE_DOUBLE_NW
, $x1, $y1);
260 $this->writeAt($charset::LINE_DOUBLE_NE
, $x2, $y1);
261 $this->writeAt($charset::LINE_DOUBLE_SE
, $x2, $y2);
262 $this->writeAt($charset::LINE_DOUBLE_SW
, $x1, $y2);
263 } elseif ($lineStyle === static::LINE_BLOCK
) {
264 $this->writeAt($charset::LINE_BLOCK_NW
, $x1, $y1);
265 $this->writeAt($charset::LINE_BLOCK_NE
, $x2, $y1);
266 $this->writeAt($charset::LINE_BLOCK_SE
, $x2, $y2);
267 $this->writeAt($charset::LINE_BLOCK_SW
, $x1, $y2);
271 // Deactivate line drawing and reset colors
272 $this->write($charset::DEACTIVATE
);
277 * Write a block of text at the given coordinates, matching the supplied width and height.
278 * In case a line of text does not fit desired width, it will be wrapped to the next line.
279 * In case the whole text does not fit in desired height, it will be truncated.
281 * @param string $text Text to write
282 * @param int $width Maximum block width. Negative value means distance from right edge.
283 * @param int|null $height Maximum block height. Negative value means distance from bottom edge.
284 * @param int $x Block X coordinate (column)
285 * @param int $y Block Y coordinate (row)
286 * @param null|int $color (optional) Text color
287 * @param null|int $bgColor (optional) Text background color
289 public function writeTextBlock(
301 * Determine and return current console width.
305 public function getWidth()
311 * Determine and return current console height.
315 public function getHeight()
321 * Determine and return current console width and height.
323 * @return array array($width, $height)
325 public function getSize()
334 * Check if console is UTF-8 compatible
338 public function isUtf8()
344 * Set cursor position
349 public function setPos($x, $y)
354 * Show console cursor
356 public function showCursor()
361 * Hide console cursor
363 public function hideCursor()
368 * Return current console window title.
372 public function getTitle()
378 * Prepare a string that will be rendered in color.
380 * @param string $string
382 * @param null|int $bgColor
385 public function colorize($string, $color = null, $bgColor = null)
391 * Change current drawing color.
395 public function setColor($color)
400 * Change current drawing background color
404 public function setBgColor($color)
409 * Reset color to console default.
411 public function resetColor()
416 * Set Console charset to use.
418 * @param Charset\CharsetInterface $charset
420 public function setCharset(Charset\CharsetInterface
$charset)
422 $this->charset
= $charset;
426 * Get charset currently in use by this adapter.
428 * @return Charset\CharsetInterface $charset
430 public function getCharset()
432 if ($this->charset
=== null) {
433 $this->charset
= $this->getDefaultCharset();
436 return $this->charset
;
440 * @return Charset\Utf8
442 public function getDefaultCharset()
444 return new Charset\Utf8
;
448 * Clear console screen
450 public function clear()
456 * Clear line at cursor position
458 public function clearLine()
460 echo "\r" . str_repeat(" ", $this->getWidth()) . "\r";
464 * Clear console screen
466 public function clearScreen()
468 return $this->clear();
472 * Read a single line from the console input
474 * @param int $maxLength Maximum response length
477 public function readLine($maxLength = 2048)
479 $f = fopen('php://stdin','r');
480 $line = stream_get_line($f, $maxLength, PHP_EOL
);
482 return rtrim($line,"\n\r");
486 * Read a single character from the console input
488 * @param string|null $mask A list of allowed chars
491 public function readChar($mask = null)
493 $f = fopen('php://stdin','r');
496 } while ("" === $char ||
($mask !== null && false === strstr($mask, $char)));