forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.php
More file actions
293 lines (253 loc) · 8.18 KB
/
Copy pathHelper.php
File metadata and controls
293 lines (253 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 16-4-1
* Time: 上午10:08
* Used:
* file: Color.php
*/
namespace inhere\console;
/**
* Class Helper
* @package inhere\console
*/
class Helper
{
/**
* clear Ansi Code
* @param $string
* @return mixed
*/
public static function stripAnsiCode($string)
{
return preg_replace('/\033\[[\d;?]*\w/', '', $string);
}
/**
* from Symfony
* @param $string
* @return int
*/
public static function strLen($string)
{
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return strlen($string);
}
return mb_strwidth($string, $encoding);
}
/**
* Returns true if STDOUT supports colorization.
* This code has been copied and adapted from
* \Symfony\Component\Console\Output\OutputStream.
* @return boolean
*/
public static function isSupportColor()
{
if (DIRECTORY_SEPARATOR === '\\') {
return
'10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM')
// || 'cygwin' === getenv('TERM')
;
}
if (!defined('STDOUT')) {
return false;
}
return self::isInteractive(STDOUT);
}
/**
* Returns if the file descriptor is an interactive terminal or not.
* @param int|resource $fileDescriptor
* @return boolean
*/
public static function isInteractive($fileDescriptor)
{
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
}
/**
* @param $memory
* @return string
* ```
* Helper::formatMemory(memory_get_usage(true));
* ```
*/
public static function formatMemory($memory)
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
}
if ($memory >= 1024 * 1024) {
return sprintf('%.1f MiB', $memory / 1024 / 1024);
}
if ($memory >= 1024) {
return sprintf('%d KiB', $memory / 1024);
}
return sprintf('%d B', $memory);
}
/**
* get key Max Width
*
* @param array $data
* [
* 'key1' => 'value1',
* 'key2-test' => 'value2',
* ]
* @param bool $expectInt
* @return int
*/
public static function getKeyMaxWidth(array $data, $expectInt = true)
{
$keyMaxWidth = 0;
foreach ($data as $key => $value) {
// key is not a integer
if ( !$expectInt || !is_numeric($key) ) {
$width = mb_strlen($key, 'UTF-8');
$keyMaxWidth = $width > $keyMaxWidth ? $width : $keyMaxWidth;
}
}
return $keyMaxWidth;
}
/**
* spliceArray
* @param array $data
* e.g [
* 'system' => 'Linux',
* 'version' => '4.4.5',
* ]
* @param array $opts
* @return string
*/
public static function spliceKeyValue(array $data, array $opts = [])
{
$text = '';
$opts = array_merge([
'leftChar' => '', // e.g ' ', ' * '
'sepChar' => ' ', // e.g ' | ' => OUT: key | value
'keyStyle' => '', // e.g 'info','comment'
'keyMaxWidth' => null, // if not set, will automatic calculation
], $opts);
if ( !is_numeric($opts['keyMaxWidth']) ) {
$opts['keyMaxWidth'] = self::getKeyMaxWidth($data);
}
$keyStyle = trim($opts['keyStyle']);
foreach ($data as $key => $value) {
$text .= $opts['leftChar'];
if ($opts['keyMaxWidth'] && !is_int($key)) {
$key = str_pad($key, $opts['keyMaxWidth'], ' ');
$text .= ( $keyStyle ? "<{$keyStyle}>$key</{$keyStyle}> " : $key ) . $opts['sepChar'];
}
// if value is array, translate array to string
if ( is_array($value) ) {
$temp = '';
foreach ($value as $k => $val) {
if (is_bool($val)) {
$val = $val ? 'True' : 'False';
} else {
$val = (string)$val;
}
$temp .= (!is_numeric($k) ? "$k: " : '') . "<info>$val</info>, ";
}
$value = rtrim($temp, ' ,');
} else if (is_bool($value)) {
$value = $value ? 'True' : 'False';
} else {
$value = (string)$value;
}
$value = ucfirst($value);
$text .= "$value\n";
}
return $text;
}
// next: form yii2
/**
* Returns true if the console is running on windows
* @return boolean
*/
public static function isOnWindows()
{
return DIRECTORY_SEPARATOR === '\\';
}
/**
* Usage: list($width, $height) = ConsoleHelper::getScreenSize();
*
* @param boolean $refresh whether to force checking and not re-use cached size value.
* This is useful to detect changing window size while the application is running but may
* not get up to date values on every terminal.
* @return array|boolean An array of ($width, $height) or false when it was not able to determine size.
*/
public static function getScreenSize($refresh = false)
{
static $size;
if ($size !== null && !$refresh) {
return $size;
}
if (static::isOnWindows()) {
$output = [];
exec('mode con', $output);
if (isset($output, $output[1]) && strpos($output[1], 'CON') !== false) {
return $size = [(int) preg_replace('~\D~', '', $output[3]), (int) preg_replace('~\D~', '', $output[4])];
}
} else {
// try stty if available
$stty = [];
if (exec('stty -a 2>&1', $stty) && preg_match('/rows\s+(\d+);\s*columns\s+(\d+);/mi', implode(' ', $stty), $matches)) {
return $size = [$matches[2], $matches[1]];
}
// fallback to tput, which may not be updated on terminal resize
if (($width = (int) exec('tput cols 2>&1')) > 0 && ($height = (int) exec('tput lines 2>&1')) > 0) {
return $size = [$width, $height];
}
// fallback to ENV variables, which may not be updated on terminal resize
if (($width = (int) getenv('COLUMNS')) > 0 && ($height = (int) getenv('LINES')) > 0) {
return $size = [$width, $height];
}
}
return $size = false;
}
/**
* Word wrap text with indentation to fit the screen size
*
* If screen size could not be detected, or the indentation is greater than the screen size, the text will not be wrapped.
*
* The first line will **not** be indented, so `Console::wrapText("Lorem ipsum dolor sit amet.", 4)` will result in the
* following output, given the screen width is 16 characters:
*
* ```
* Lorem ipsum
* dolor sit
* amet.
* ```
*
* @param string $text the text to be wrapped
* @param integer $indent number of spaces to use for indentation.
* @param integer $width
* @return string the wrapped text.
* @since 2.0.4
*/
public static function wrapText($text, $indent = 0, $width = 0)
{
if (!$text) {
return $text;
}
if ( (int)$width <= 0 ) {
$size = static::getScreenSize();
if ( $size === false || $size[0] <= $indent) {
return $text;
}
$width = $size[0];
}
$pad = str_repeat(' ', $indent);
$lines = explode("\n", wordwrap($text, $width - $indent, "\n", true));
$first = true;
foreach ($lines as $i => $line) {
if ($first) {
$first = false;
continue;
}
$lines[$i] = $pad . $line;
}
return $pad . ' ' . implode("\n", $lines);
}
}