annotate vendor/symfony/console/Helper/Helper.php @ 0:4c8ae668cc8c

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