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
;
13 use Zend\Console\Charset
;
14 use Zend\Console\Exception
;
15 use Zend\Console\Color\Xterm256
;
16 use Zend\Console\ColorInterface
as Color
;
19 * @todo Add GNU readline support
20 * @link http://en.wikipedia.org/wiki/ANSI_escape_code
22 class Posix
extends AbstractAdapter
25 * Whether or not mbstring is enabled
29 protected static $hasMBString;
32 * @var Charset\CharsetInterface
37 * Map of colors to ANSI codes
41 protected static $ansiColorMap = array(
43 Color
::NORMAL
=> '22;39',
44 Color
::RESET
=> '22;39',
46 Color
::BLACK
=> '0;30',
48 Color
::GREEN
=> '0;32',
49 Color
::YELLOW
=> '0;33',
50 Color
::BLUE
=> '0;34',
51 Color
::MAGENTA
=> '0;35',
52 Color
::CYAN
=> '0;36',
53 Color
::WHITE
=> '0;37',
55 Color
::GRAY
=> '1;30',
56 Color
::LIGHT_RED
=> '1;31',
57 Color
::LIGHT_GREEN
=> '1;32',
58 Color
::LIGHT_YELLOW
=> '1;33',
59 Color
::LIGHT_BLUE
=> '1;34',
60 Color
::LIGHT_MAGENTA
=> '1;35',
61 Color
::LIGHT_CYAN
=> '1;36',
62 Color
::LIGHT_WHITE
=> '1;37',
65 Color
::NORMAL
=> '0;49',
66 Color
::RESET
=> '0;49',
71 Color
::YELLOW
=> '43',
73 Color
::MAGENTA
=> '45',
78 Color
::LIGHT_RED
=> '41',
79 Color
::LIGHT_GREEN
=> '42',
80 Color
::LIGHT_YELLOW
=> '43',
81 Color
::LIGHT_BLUE
=> '44',
82 Color
::LIGHT_MAGENTA
=> '45',
83 Color
::LIGHT_CYAN
=> '46',
84 Color
::LIGHT_WHITE
=> '47',
89 * Last fetched TTY mode
93 protected $lastTTYMode = null;
96 * Write a single line of text to console and advance cursor to the next line.
98 * This override works around a bug in some terminals that cause the background color
99 * to fill the next line after EOL. To remedy this, we are sending the colored string with
100 * appropriate color reset sequences before sending EOL character.
102 * @link https://github.com/zendframework/zf2/issues/4167
103 * @param string $text
104 * @param null|int $color
105 * @param null|int $bgColor
107 public function writeLine($text = "", $color = null, $bgColor = null)
109 $this->write($text, $color, $bgColor);
110 $this->write(PHP_EOL
);
114 * Determine and return current console width.
118 public function getWidth()
126 * Try to read env variable
128 if (($result = getenv('COLUMNS')) !== false) {
129 return $width = (int) $result;
133 * Try to read console size from "tput" command
135 $result = exec('tput cols', $output, $return);
136 if (!$return && is_numeric($result)) {
137 return $width = (int) $result;
140 return $width = parent
::getWidth();
144 * Determine and return current console height.
148 public function getHeight()
155 // Try to read env variable
156 if (($result = getenv('LINES')) !== false) {
157 return $height = (int) $result;
160 // Try to read console size from "tput" command
161 $result = exec('tput lines', $output, $return);
162 if (!$return && is_numeric($result)) {
163 return $height = (int) $result;
166 return $height = parent
::getHeight();
170 * Run a mode command and store results
174 protected function runModeCommand()
176 exec('mode', $output, $return);
177 if ($return ||
!count($output)) {
178 $this->modeResult
= '';
180 $this->modeResult
= trim(implode('', $output));
185 * Check if console is UTF-8 compatible
189 public function isUtf8()
191 // Try to retrieve it from LANG env variable
192 if (($lang = getenv('LANG')) !== false) {
193 return stristr($lang, 'utf-8') ||
stristr($lang, 'utf8');
200 * Show console cursor
202 public function showCursor()
208 * Hide console cursor
210 public function hideCursor()
216 * Set cursor position
220 public function setPos($x, $y)
222 echo "\x1b[" . $y . ';' . $x . 'f';
226 * Prepare a string that will be rendered in color.
228 * @param string $string
230 * @param null|int $bgColor
231 * @throws Exception\BadMethodCallException
234 public function colorize($string, $color = null, $bgColor = null)
236 $color = $this->getColorCode($color, 'fg');
237 $bgColor = $this->getColorCode($bgColor, 'bg');
238 return ($color !== null ?
"\x1b[" . $color . 'm' : '')
239 . ($bgColor !== null ?
"\x1b[" . $bgColor . 'm' : '')
241 . "\x1b[22;39m\x1b[0;49m";
245 * Change current drawing color.
248 * @throws Exception\BadMethodCallException
250 public function setColor($color)
252 $color = $this->getColorCode($color, 'fg');
253 echo "\x1b[" . $color . 'm';
257 * Change current drawing background color
259 * @param int $bgColor
260 * @throws Exception\BadMethodCallException
262 public function setBgColor($bgColor)
264 $bgColor = $this->getColorCode($bgColor, 'bg');
265 echo "\x1b[" . ($bgColor) . 'm';
269 * Reset color to console default.
271 public function resetColor()
273 echo "\x1b[0;49m"; // reset bg color
274 echo "\x1b[22;39m"; // reset fg bold, bright and faint
275 echo "\x1b[25;39m"; // reset fg blink
276 echo "\x1b[24;39m"; // reset fg underline
280 * Set Console charset to use.
282 * @param Charset\CharsetInterface $charset
284 public function setCharset(Charset\CharsetInterface
$charset)
286 $this->charset
= $charset;
290 * Get charset currently in use by this adapter.
292 * @return Charset\CharsetInterface $charset
294 public function getCharset()
296 if ($this->charset
=== null) {
297 $this->charset
= $this->getDefaultCharset();
300 return $this->charset
;
304 * @return Charset\CharsetInterface
306 public function getDefaultCharset()
308 if ($this->isUtf8()) {
309 return new Charset\Utf8
;
311 return new Charset\
DECSG();
315 * Read a single character from the console input
317 * @param string|null $mask A list of allowed chars
320 public function readChar($mask = null)
322 $this->setTTYMode('-icanon -echo');
324 $stream = fopen('php://stdin', 'rb');
326 $char = fgetc($stream);
327 } while (strlen($char) !== 1 ||
($mask !== null && false === strstr($mask, $char)));
330 $this->restoreTTYMode();
335 * Reset color to console default.
337 public function clear()
339 echo "\x1b[2J"; // reset bg color
340 $this->setPos(1, 1); // reset cursor position
344 * Restore TTY (Console) mode to previous value.
348 protected function restoreTTYMode()
350 if ($this->lastTTYMode
=== null) {
354 shell_exec('stty ' . escapeshellarg($this->lastTTYMode
));
358 * Change TTY (Console) mode
360 * @link http://en.wikipedia.org/wiki/Stty
363 protected function setTTYMode($mode)
366 $this->lastTTYMode
= trim(`stty
-g`
);
369 shell_exec('stty '.escapeshellcmd($mode));
373 * Get the final color code and throw exception on error
375 * @param null|int|Xterm256 $color
376 * @throws Exception\BadMethodCallException
379 protected function getColorCode($color, $type = 'fg')
381 if ($color instanceof Xterm256
) {
382 $r = new ReflectionClass($color);
383 $code = $r->getStaticPropertyValue('color');
385 $code = sprintf($code, $color::FOREGROUND
);
387 $code = sprintf($code, $color::BACKGROUND
);
392 if ($color !== null) {
393 if (!isset(static::$ansiColorMap[$type][$color])) {
394 throw new Exception\
BadMethodCallException(sprintf(
395 'Unknown color "%s". Please use one of the Zend\Console\ColorInterface constants or use Zend\Console\Color\Xterm256::calculate',
400 return static::$ansiColorMap[$type][$color];