Skip to content

Commit

Permalink
repleace some util methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 28, 2021
1 parent 3924621 commit 8520aeb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 74 deletions.
2 changes: 1 addition & 1 deletion examples/alone
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Toolkit\Cli\Color;

define('BASE_PATH', dirname(__DIR__));

require dirname(__DIR__) . '/test/boot.php';
require dirname(__DIR__) . '/test/bootstrap.php';

$input = new Input();
$ctrl = new HomeController($input, new Output());
Expand Down
2 changes: 1 addition & 1 deletion examples/alone-app
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Toolkit\Cli\Color;

define('BASE_PATH', dirname(__DIR__));

require dirname(__DIR__) . '/test/boot.php';
require dirname(__DIR__) . '/test/bootstrap.php';

try {
$input = new Input();
Expand Down
2 changes: 1 addition & 1 deletion examples/app
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Inhere\Console\Application;

define('BASE_PATH', dirname(__DIR__));

require dirname(__DIR__) . '/test/boot.php';
require dirname(__DIR__) . '/test/bootstrap.php';

// create app instance
$app = new Application([
Expand Down
1 change: 1 addition & 0 deletions src/Concern/ApplicationHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use function str_replace;
use function strpos;
use function strtr;
use function vdump;
use const PHP_EOL;
use const PHP_OS;
use const PHP_VERSION;
Expand Down
56 changes: 9 additions & 47 deletions src/Util/FormatUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@
namespace Inhere\Console\Util;

use Toolkit\Cli\ColorTag;
use Toolkit\Stdlib\Helper\Format;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Str;
use Toolkit\Sys\Sys;
use function array_keys;
use function array_merge;
use function count;
use function date;
use function explode;
use function floor;
use function implode;
use function is_array;
use function is_bool;
use function is_int;
use function is_numeric;
use function is_scalar;
use function rtrim;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strpos;
Expand Down Expand Up @@ -169,28 +166,18 @@ public static function alignOptions(array $options): array
}

/**
* @param float $memory
*
* @return string
* ```
* FormatUtil::memoryUsage(memory_get_usage(true));
* ```
*
* @param float|int $memory
*
* @return string
* @deprecated use Format::memory($secs);
*/
public static function memoryUsage($memory): string
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.2f Gb', $memory / 1024 / 1024 / 1024);
}

if ($memory >= 1024 * 1024) {
return sprintf('%.2f Mb', $memory / 1024 / 1024);
}

if ($memory >= 1024) {
return sprintf('%.2f Kb', $memory / 1024);
}

return sprintf('%d B', $memory);
return Format::memory($memory);
}

/**
Expand All @@ -199,36 +186,11 @@ public static function memoryUsage($memory): string
* @param int $secs
*
* @return string
* @deprecated use Format::howLongAgo($secs);
*/
public static function howLongAgo(int $secs): string
{
static $timeFormats = [
[0, '< 1 sec'],
[1, '1 sec'],
[2, 'secs', 1],
[60, '1 min'],
[120, 'mins', 60],
[3600, '1 hr'],
[7200, 'hrs', 3600],
[86400, '1 day'],
[172800, 'days', 86400],
];

foreach ($timeFormats as $index => $format) {
if ($secs >= $format[0]) {
$next = $timeFormats[$index + 1] ?? false;

if (($next && $secs < $next[0]) || $index === count($timeFormats) - 1) {
if (2 === count($format)) {
return $format[1];
}

return floor($secs / $format[2]) . ' ' . $format[1];
}
}
}

return date('Y-m-d H:i:s', $secs);
return Format::howLongAgo($secs);
}

/**
Expand Down
32 changes: 8 additions & 24 deletions src/Util/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use RecursiveIteratorIterator;
use RuntimeException;
use Swoole\Coroutine;
use Toolkit\Stdlib\Arr\ArrayHelper;
use function class_exists;
use function file_exists;
use function is_dir;
Expand Down Expand Up @@ -173,29 +174,19 @@ public static function findSimilar(string $need, $iterator, int $similarPercent
/**
* get key Max Width
*
* @param array $data
* [
* [
* 'key1' => 'value1',
* 'key2-test' => 'value2',
* ]
* @param bool $expectInt
* ]
*
* @param array $data
* @param bool $excludeInt
*
* @return int
*/
public static function getKeyMaxWidth(array $data, bool $expectInt = false): int
public static function getKeyMaxWidth(array $data, bool $excludeInt = true): int
{
$keyMaxWidth = 0;

foreach ($data as $key => $value) {
// key is not a integer
if (!$expectInt || !is_numeric($key)) {
$width = mb_strlen((string)$key, 'UTF-8');

$keyMaxWidth = $width > $keyMaxWidth ? $width : $keyMaxWidth;
}
}

return $keyMaxWidth;
return ArrayHelper::getKeyMaxWidth($data, $excludeInt);
}

/**
Expand All @@ -206,11 +197,4 @@ public static function throwInvalidArgument(string $format, ...$args): void
{
throw new InvalidArgumentException(sprintf($format, ...$args));
}

/**
* @param string $optsStr
*/
public static function formatOptions(string $optsStr): void
{
}
}

0 comments on commit 8520aeb

Please sign in to comment.