-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathFormat.php
More file actions
55 lines (44 loc) · 1.43 KB
/
Format.php
File metadata and controls
55 lines (44 loc) · 1.43 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
<?php
declare(strict_types=1);
namespace Platformsh\Cli\Model\Metrics;
use Symfony\Component\Console\Helper\FormatterHelper;
enum Format: string
{
case Rounded = 'rounded';
case Rounded2p = 'rounded_2';
case Percent = 'percent';
case Disk = 'disk';
case Memory = 'memory';
private const RED_WARNING_THRESHOLD = 90; // percent
private const YELLOW_WARNING_THRESHOLD = 80; // percent
private function formatPercent(?float $pc, bool $warn = true): string
{
if (null === $pc) {
return '';
}
if ($warn) {
if ($pc >= self::RED_WARNING_THRESHOLD) {
return \sprintf('<options=bold;fg=red>%.1f%%</>', $pc);
}
if ($pc >= self::YELLOW_WARNING_THRESHOLD) {
return \sprintf('<options=bold;fg=yellow>%.1f%%</>', $pc);
}
}
return \sprintf('%.1f%%', $pc);
}
public function format(?float $value, bool $warn = true): string|float|int|null
{
if (null === $value) {
return null;
}
if (PHP_INT_MAX === (int) $value) {
return '∞';
}
return match ($this) {
Format::Rounded => round($value),
Format::Rounded2p => round($value, 2),
Format::Percent => $this->formatPercent($value, $warn),
Format::Disk, Format::Memory => FormatterHelper::formatMemory((int) $value),
};
}
}