Lines
96.42%
27 / 28
Functions and Methods
83.33%
5 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StatusFormatter | 96.42% | 27 / 28 | 83.33% | 5 / 6 | 18 | 0.00% | 0 / 1 | |||
| formatBytes | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 5 | |||||
| formatElapsed | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| formatTimeAgo | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| formatTimeUntil | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 2.15 | |||||
| formatDuration | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | |||||
| formatYesNo | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Console; | |
| 6 | ||
| 7 | use Stewart\Contracts\Time\Duration; | |
| 8 | use Stewart\Contracts\Time\Instant; | |
| 9 | ||
| 10 | final readonly class StatusFormatter | |
| 11 | { | |
| 12 | public function formatBytes(?int $bytes): string | |
| 13 | { | |
| 14 | if ($bytes === null) { | |
| 15 | return '-'; | |
| 16 | } | |
| 17 | ||
| 18 | if ($bytes >= 1_073_741_824) { | |
| 19 | return \sprintf('%.1f GiB', $bytes / 1_073_741_824); | |
| 20 | } | |
| 21 | ||
| 22 | if ($bytes >= 1_048_576) { | |
| 23 | return \sprintf('%.1f MiB', $bytes / 1_048_576); | |
| 24 | } | |
| 25 | ||
| 26 | if ($bytes >= 1024) { | |
| 27 | return \sprintf('%.0f KiB', $bytes / 1024); | |
| 28 | } | |
| 29 | ||
| 30 | return $bytes . ' B'; | |
| 31 | } | |
| 32 | ||
| 33 | public function formatElapsed(?Duration $duration): string | |
| 34 | { | |
| 35 | if ($duration === null) { | |
| 36 | return '-'; | |
| 37 | } | |
| 38 | ||
| 39 | $milliseconds = $duration->toMicroseconds() / 1_000; | |
| 40 | ||
| 41 | return $milliseconds >= 1000 ? \sprintf('%.1f s', $milliseconds / 1000) : \sprintf('%.1f ms', $milliseconds); | |
| 42 | } | |
| 43 | ||
| 44 | public function formatTimeAgo(?Instant $at, Instant $now): string | |
| 45 | { | |
| 46 | if ($at === null) { | |
| 47 | return 'never'; | |
| 48 | } | |
| 49 | ||
| 50 | return $this->formatDuration($now->elapsedSince($at)) . ' ago'; | |
| 51 | } | |
| 52 | ||
| 53 | public function formatTimeUntil(?Instant $at, Instant $now): string | |
| 54 | { | |
| 55 | if ($at === null) { | |
| 56 | return '-'; | |
| 57 | } | |
| 58 | ||
| 59 | return 'in ' . $this->formatDuration($at->elapsedSince($now)); | |
| 60 | } | |
| 61 | ||
| 62 | public function formatDuration(Duration $duration): string | |
| 63 | { | |
| 64 | $seconds = (int) $duration->toSeconds(); | |
| 65 | ||
| 66 | if ($seconds < 60) { | |
| 67 | return \sprintf('%ds', $seconds); | |
| 68 | } | |
| 69 | ||
| 70 | if ($seconds < 3600) { | |
| 71 | return \sprintf('%dm %02ds', intdiv($seconds, 60), $seconds % 60); | |
| 72 | } | |
| 73 | ||
| 74 | if ($seconds < 86400) { | |
| 75 | return \sprintf('%dh %02dm', intdiv($seconds, 3600), intdiv($seconds % 3600, 60)); | |
| 76 | } | |
| 77 | ||
| 78 | return \sprintf('%dd %02dh', intdiv($seconds, 86400), intdiv($seconds % 86400, 3600)); | |
| 79 | } | |
| 80 | ||
| 81 | public function formatYesNo(bool $value): string | |
| 82 | { | |
| 83 | return $value ? 'yes' : 'no'; | |
| 84 | } | |
| 85 | } |