-
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathHelper.php
More file actions
167 lines (132 loc) · 4.57 KB
/
Helper.php
File metadata and controls
167 lines (132 loc) · 4.57 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\String\UnicodeString;
/**
* Helper is the base class for all helper classes.
*
* @author Fabien Potencier <[email protected]>
*/
abstract class Helper implements HelperInterface
{
protected ?HelperSet $helperSet = null;
public function setHelperSet(?HelperSet $helperSet): void
{
$this->helperSet = $helperSet;
}
public function getHelperSet(): ?HelperSet
{
return $this->helperSet;
}
/**
* Returns the width of a string, using mb_strwidth if it is available.
* The width is how many characters positions the string will use.
*/
public static function width(?string $string): int
{
$string ??= '';
if (preg_match('//u', $string)) {
$string = preg_replace('/[\p{Cc}\x7F]++/u', '', $string, -1, $count);
return (new UnicodeString($string))->width(false) + $count;
}
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return \strlen($string);
}
return mb_strwidth($string, $encoding);
}
/**
* Returns the length of a string, using mb_strlen if it is available.
* The length is related to how many bytes the string will use.
*/
public static function length(?string $string): int
{
$string ??= '';
if (preg_match('//u', $string)) {
return (new UnicodeString($string))->length();
}
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return \strlen($string);
}
return mb_strlen($string, $encoding);
}
/**
* Returns the subset of a string, using mb_substr if it is available.
*/
public static function substr(?string $string, int $from, ?int $length = null): string
{
$string ??= '';
if (preg_match('//u', $string)) {
return (new UnicodeString($string))->slice($from, $length);
}
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return substr($string, $from, $length);
}
return mb_substr($string, $from, $length, $encoding);
}
public static function formatTime(int|float $secs, int $precision = 1): string
{
$ms = (int) ($secs * 1000);
$secs = (int) floor($secs);
if (0 === $ms) {
return '< 1 ms';
}
static $timeFormats = [
[1, 'ms'],
[1000, 's'],
[60000, 'min'],
[3600000, 'h'],
[86_400_000, 'd'],
];
$times = [];
foreach ($timeFormats as $index => $format) {
$milliSeconds = isset($timeFormats[$index + 1]) ? $ms % $timeFormats[$index + 1][0] : $ms;
if (isset($times[$index - $precision])) {
unset($times[$index - $precision]);
}
if (0 === $milliSeconds) {
continue;
}
$unitCount = ($milliSeconds / $format[0]);
$times[$index] = $unitCount.' '.$format[1];
if ($ms === $milliSeconds) {
break;
}
$ms -= $milliSeconds;
}
return implode(', ', array_reverse($times));
}
public static function formatMemory(int $memory): string
{
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);
}
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string): string
{
$isDecorated = $formatter->isDecorated();
$formatter->setDecorated(false);
// remove <...> formatting
$string = $formatter->format($string ?? '');
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
// remove terminal hyperlinks
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
$formatter->setDecorated($isDecorated);
return $string;
}
}