PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Helper / Helper.php

Helper.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Helper/Helper.php

133 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Helper;
13
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15
16 /**
17 * Helper is the base class for all helper classes.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21 abstract class Helper implements HelperInterface
22 {
23 protected $helperSet = null;
24
25 /**
26 * {@inheritdoc}
27 */
28 public function setHelperSet(HelperSet $helperSet = null)
29 {
30 $this->helperSet = $helperSet;
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 public function getHelperSet()
37 {
38 return $this->helperSet;
39 }
40
41 /**
42 * Returns the length of a string, using mb_strwidth if it is available.
43 *
44 * @return int The length of the string
45 */
46 public static function strlen(?string $string)
47 {
48 if (false === $encoding = mb_detect_encoding($string, null, true)) {
49 return \strlen($string);
50 }
51
52 return mb_strwidth($string, $encoding);
53 }
54
55 /**
56 * Returns the subset of a string, using mb_substr if it is available.
57 *
58 * @return string The string subset
59 */
60 public static function substr(string $string, int $from, int $length = null)
61 {
62 if (false === $encoding = mb_detect_encoding($string, null, true)) {
63 return substr($string, $from, $length);
64 }
65
66 return mb_substr($string, $from, $length, $encoding);
67 }
68
69 public static function formatTime($secs)
70 {
71 static $timeFormats = [
72 [0, '< 1 sec'],
73 [1, '1 sec'],
74 [2, 'secs', 1],
75 [60, '1 min'],
76 [120, 'mins', 60],
77 [3600, '1 hr'],
78 [7200, 'hrs', 3600],
79 [86400, '1 day'],
80 [172800, 'days', 86400],
81 ];
82
83 foreach ($timeFormats as $index => $format) {
84 if ($secs >= $format[0]) {
85 if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
86 || $index == \count($timeFormats) - 1
87 ) {
88 if (2 == \count($format)) {
89 return $format[1];
90 }
91
92 return floor($secs / $format[2]).' '.$format[1];
93 }
94 }
95 }
96 }
97
98 public static function formatMemory(int $memory)
99 {
100 if ($memory >= 1024 * 1024 * 1024) {
101 return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
102 }
103
104 if ($memory >= 1024 * 1024) {
105 return sprintf('%.1f MiB', $memory / 1024 / 1024);
106 }
107
108 if ($memory >= 1024) {
109 return sprintf('%d KiB', $memory / 1024);
110 }
111
112 return sprintf('%d B', $memory);
113 }
114
115 public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
116 {
117 return self::strlen(self::removeDecoration($formatter, $string));
118 }
119
120 public static function removeDecoration(OutputFormatterInterface $formatter, $string)
121 {
122 $isDecorated = $formatter->isDecorated();
123 $formatter->setDecorated(false);
124 // remove <...> formatting
125 $string = $formatter->format($string);
126 // remove already formatted characters
127 $string = preg_replace("/\033\[[^m]*m/", '', $string);
128 $formatter->setDecorated($isDecorated);
129
130 return $string;
131 }
132 }
133