Chris@12
|
1 <?php
|
Chris@12
|
2 /**
|
Chris@12
|
3 * @link http://github.com/zendframework/zend-stdlib for the canonical source repository
|
Chris@12
|
4 * @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@12
|
5 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@12
|
6 */
|
Chris@12
|
7
|
Chris@12
|
8 namespace Zend\Stdlib;
|
Chris@12
|
9
|
Chris@12
|
10 /**
|
Chris@12
|
11 * Utilities for console tooling.
|
Chris@12
|
12 *
|
Chris@12
|
13 * Provides the following facilities:
|
Chris@12
|
14 *
|
Chris@12
|
15 * - Colorize strings using markup (e.g., `<info>message</info>`,
|
Chris@12
|
16 * `<error>message</error>`)
|
Chris@12
|
17 * - Write output to a specified stream, optionally with colorization.
|
Chris@12
|
18 * - Write a line of output to a specified stream, optionally with
|
Chris@12
|
19 * colorization, using the system EOL sequence..
|
Chris@12
|
20 * - Write an error message to STDERR.
|
Chris@12
|
21 *
|
Chris@12
|
22 * Colorization will only occur when expected sequences are discovered, and
|
Chris@12
|
23 * then, only if the console terminal allows it.
|
Chris@12
|
24 *
|
Chris@12
|
25 * Essentially, provides the bare minimum to allow you to provide messages to
|
Chris@12
|
26 * the current console.
|
Chris@12
|
27 */
|
Chris@12
|
28 class ConsoleHelper
|
Chris@12
|
29 {
|
Chris@12
|
30 const COLOR_GREEN = "\033[32m";
|
Chris@12
|
31 const COLOR_RED = "\033[31m";
|
Chris@12
|
32 const COLOR_RESET = "\033[0m";
|
Chris@12
|
33
|
Chris@12
|
34 const HIGHLIGHT_INFO = 'info';
|
Chris@12
|
35 const HIGHLIGHT_ERROR = 'error';
|
Chris@12
|
36
|
Chris@12
|
37 private $highlightMap = [
|
Chris@12
|
38 self::HIGHLIGHT_INFO => self::COLOR_GREEN,
|
Chris@12
|
39 self::HIGHLIGHT_ERROR => self::COLOR_RED,
|
Chris@12
|
40 ];
|
Chris@12
|
41
|
Chris@12
|
42 /**
|
Chris@12
|
43 * @var string Exists only for testing.
|
Chris@12
|
44 */
|
Chris@12
|
45 private $eol = PHP_EOL;
|
Chris@12
|
46
|
Chris@12
|
47 /**
|
Chris@12
|
48 * @var resource Exists only for testing.
|
Chris@12
|
49 */
|
Chris@12
|
50 private $stderr = STDERR;
|
Chris@12
|
51
|
Chris@12
|
52 /**
|
Chris@12
|
53 * @var bool
|
Chris@12
|
54 */
|
Chris@12
|
55 private $supportsColor;
|
Chris@12
|
56
|
Chris@12
|
57 /**
|
Chris@12
|
58 * @param resource $resource
|
Chris@12
|
59 */
|
Chris@12
|
60 public function __construct($resource = STDOUT)
|
Chris@12
|
61 {
|
Chris@12
|
62 $this->supportsColor = $this->detectColorCapabilities($resource);
|
Chris@12
|
63 }
|
Chris@12
|
64
|
Chris@12
|
65 /**
|
Chris@12
|
66 * Colorize a string for use with the terminal.
|
Chris@12
|
67 *
|
Chris@12
|
68 * Takes strings formatted as `<key>string</key>` and formats them per the
|
Chris@12
|
69 * $highlightMap; if color support is disabled, simply removes the formatting
|
Chris@12
|
70 * tags.
|
Chris@12
|
71 *
|
Chris@12
|
72 * @param string $string
|
Chris@12
|
73 * @return string
|
Chris@12
|
74 */
|
Chris@12
|
75 public function colorize($string)
|
Chris@12
|
76 {
|
Chris@12
|
77 $reset = $this->supportsColor ? self::COLOR_RESET : '';
|
Chris@12
|
78 foreach ($this->highlightMap as $key => $color) {
|
Chris@12
|
79 $pattern = sprintf('#<%s>(.*?)</%s>#s', $key, $key);
|
Chris@12
|
80 $color = $this->supportsColor ? $color : '';
|
Chris@12
|
81 $string = preg_replace($pattern, $color . '$1' . $reset, $string);
|
Chris@12
|
82 }
|
Chris@12
|
83 return $string;
|
Chris@12
|
84 }
|
Chris@12
|
85
|
Chris@12
|
86 /**
|
Chris@12
|
87 * @param string $string
|
Chris@12
|
88 * @param bool $colorize Whether or not to colorize the string
|
Chris@12
|
89 * @param resource $resource Defaults to STDOUT
|
Chris@12
|
90 * @return void
|
Chris@12
|
91 */
|
Chris@12
|
92 public function write($string, $colorize = true, $resource = STDOUT)
|
Chris@12
|
93 {
|
Chris@12
|
94 if ($colorize) {
|
Chris@12
|
95 $string = $this->colorize($string);
|
Chris@12
|
96 }
|
Chris@12
|
97
|
Chris@12
|
98 $string = $this->formatNewlines($string);
|
Chris@12
|
99
|
Chris@12
|
100 fwrite($resource, $string);
|
Chris@12
|
101 }
|
Chris@12
|
102
|
Chris@12
|
103 /**
|
Chris@12
|
104 * @param string $string
|
Chris@12
|
105 * @param bool $colorize Whether or not to colorize the line
|
Chris@12
|
106 * @param resource $resource Defaults to STDOUT
|
Chris@12
|
107 * @return void
|
Chris@12
|
108 */
|
Chris@12
|
109 public function writeLine($string, $colorize = true, $resource = STDOUT)
|
Chris@12
|
110 {
|
Chris@12
|
111 $this->write($string . $this->eol, $colorize, $resource);
|
Chris@12
|
112 }
|
Chris@12
|
113
|
Chris@12
|
114 /**
|
Chris@12
|
115 * Emit an error message.
|
Chris@12
|
116 *
|
Chris@12
|
117 * Wraps the message in `<error></error>`, and passes it to `writeLine()`,
|
Chris@12
|
118 * using STDERR as the resource; emits an additional empty line when done,
|
Chris@12
|
119 * also to STDERR.
|
Chris@12
|
120 *
|
Chris@12
|
121 * @param string $message
|
Chris@12
|
122 * @return void
|
Chris@12
|
123 */
|
Chris@12
|
124 public function writeErrorMessage($message)
|
Chris@12
|
125 {
|
Chris@12
|
126 $this->writeLine(sprintf('<error>%s</error>', $message), true, $this->stderr);
|
Chris@12
|
127 $this->writeLine('', false, $this->stderr);
|
Chris@12
|
128 }
|
Chris@12
|
129
|
Chris@12
|
130 /**
|
Chris@12
|
131 * @param resource $resource
|
Chris@12
|
132 * @return bool
|
Chris@12
|
133 */
|
Chris@12
|
134 private function detectColorCapabilities($resource = STDOUT)
|
Chris@12
|
135 {
|
Chris@12
|
136 if ('\\' === DIRECTORY_SEPARATOR) {
|
Chris@12
|
137 // Windows
|
Chris@12
|
138 return false !== getenv('ANSICON')
|
Chris@12
|
139 || 'ON' === getenv('ConEmuANSI')
|
Chris@12
|
140 || 'xterm' === getenv('TERM');
|
Chris@12
|
141 }
|
Chris@12
|
142
|
Chris@12
|
143 return function_exists('posix_isatty') && posix_isatty($resource);
|
Chris@12
|
144 }
|
Chris@12
|
145
|
Chris@12
|
146 /**
|
Chris@12
|
147 * Ensure newlines are appropriate for the current terminal.
|
Chris@12
|
148 *
|
Chris@12
|
149 * @param string
|
Chris@12
|
150 * @return string
|
Chris@12
|
151 */
|
Chris@12
|
152 private function formatNewlines($string)
|
Chris@12
|
153 {
|
Chris@12
|
154 $string = str_replace($this->eol, "\0PHP_EOL\0", $string);
|
Chris@12
|
155 $string = preg_replace("/(\r\n|\n|\r)/", $this->eol, $string);
|
Chris@12
|
156 return str_replace("\0PHP_EOL\0", $this->eol, $string);
|
Chris@12
|
157 }
|
Chris@12
|
158 }
|