3 * @link http://github.com/zendframework/zend-stdlib for the canonical source repository
4 * @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license http://framework.zend.com/license/new-bsd New BSD License
11 * Utilities for console tooling.
13 * Provides the following facilities:
15 * - Colorize strings using markup (e.g., `<info>message</info>`,
16 * `<error>message</error>`)
17 * - Write output to a specified stream, optionally with colorization.
18 * - Write a line of output to a specified stream, optionally with
19 * colorization, using the system EOL sequence..
20 * - Write an error message to STDERR.
22 * Colorization will only occur when expected sequences are discovered, and
23 * then, only if the console terminal allows it.
25 * Essentially, provides the bare minimum to allow you to provide messages to
26 * the current console.
30 const COLOR_GREEN = "\033[32m";
31 const COLOR_RED = "\033[31m";
32 const COLOR_RESET = "\033[0m";
34 const HIGHLIGHT_INFO = 'info';
35 const HIGHLIGHT_ERROR = 'error';
37 private $highlightMap = [
38 self::HIGHLIGHT_INFO => self::COLOR_GREEN,
39 self::HIGHLIGHT_ERROR => self::COLOR_RED,
43 * @var string Exists only for testing.
45 private $eol = PHP_EOL;
48 * @var resource Exists only for testing.
50 private $stderr = STDERR;
55 private $supportsColor;
58 * @param resource $resource
60 public function __construct($resource = STDOUT)
62 $this->supportsColor = $this->detectColorCapabilities($resource);
66 * Colorize a string for use with the terminal.
68 * Takes strings formatted as `<key>string</key>` and formats them per the
69 * $highlightMap; if color support is disabled, simply removes the formatting
72 * @param string $string
75 public function colorize($string)
77 $reset = $this->supportsColor ? self::COLOR_RESET : '';
78 foreach ($this->highlightMap as $key => $color) {
79 $pattern = sprintf('#<%s>(.*?)</%s>#s', $key, $key);
80 $color = $this->supportsColor ? $color : '';
81 $string = preg_replace($pattern, $color . '$1' . $reset, $string);
87 * @param string $string
88 * @param bool $colorize Whether or not to colorize the string
89 * @param resource $resource Defaults to STDOUT
92 public function write($string, $colorize = true, $resource = STDOUT)
95 $string = $this->colorize($string);
98 $string = $this->formatNewlines($string);
100 fwrite($resource, $string);
104 * @param string $string
105 * @param bool $colorize Whether or not to colorize the line
106 * @param resource $resource Defaults to STDOUT
109 public function writeLine($string, $colorize = true, $resource = STDOUT)
111 $this->write($string . $this->eol, $colorize, $resource);
115 * Emit an error message.
117 * Wraps the message in `<error></error>`, and passes it to `writeLine()`,
118 * using STDERR as the resource; emits an additional empty line when done,
121 * @param string $message
124 public function writeErrorMessage($message)
126 $this->writeLine(sprintf('<error>%s</error>', $message), true, $this->stderr);
127 $this->writeLine('', false, $this->stderr);
131 * @param resource $resource
134 private function detectColorCapabilities($resource = STDOUT)
136 if ('\\' === DIRECTORY_SEPARATOR) {
138 return false !== getenv('ANSICON')
139 || 'ON' === getenv('ConEmuANSI')
140 || 'xterm' === getenv('TERM');
143 return function_exists('posix_isatty') && posix_isatty($resource);
147 * Ensure newlines are appropriate for the current terminal.
152 private function formatNewlines($string)
154 $string = str_replace($this->eol, "\0PHP_EOL\0", $string);
155 $string = preg_replace("/(\r\n|\n|\r)/", $this->eol, $string);
156 return str_replace("\0PHP_EOL\0", $this->eol, $string);