1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stefaminator\Cli; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Progress { |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
public static function showStatus(int $done, int $total, int $size = 30): void { |
11
|
|
|
|
12
|
1 |
|
static $start_time = NULL; |
13
|
|
|
|
14
|
|
|
// if we go over our bound, just ignore it |
15
|
1 |
|
if ($done > $total) { |
16
|
1 |
|
return; |
17
|
|
|
} |
18
|
|
|
|
19
|
1 |
|
if (empty($start_time)) { |
20
|
1 |
|
$start_time = time(); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
$now = time(); |
24
|
|
|
|
25
|
1 |
|
$factor = self::getFactor($total, $done); |
26
|
|
|
|
27
|
1 |
|
$status_bar_pb = self::getStatusbar($factor, $size); |
28
|
|
|
|
29
|
1 |
|
$status_bar = "\r"; |
30
|
1 |
|
$status_bar .= Color::getColoredString($status_bar_pb, Color::FOREGROUND_COLOR_GREEN, Color::BACKGROUND_COLOR_LIGHT_GRAY); |
31
|
|
|
|
32
|
1 |
|
$disp = number_format($factor * 100); |
33
|
|
|
|
34
|
1 |
|
$status_bar_percent = str_pad("$disp%", 5, ' ', STR_PAD_LEFT); |
35
|
1 |
|
$status_bar .= Color::getColoredString($status_bar_percent, Color::FOREGROUND_COLOR_GREEN); |
36
|
|
|
|
37
|
1 |
|
$status_bar_done = " $done/$total"; |
38
|
|
|
|
39
|
1 |
|
$status_bar .= Color::getColoredString($status_bar_done, Color::FOREGROUND_COLOR_YELLOW); |
40
|
|
|
|
41
|
1 |
|
$rate = self::getRate($now - $start_time, $done); |
42
|
1 |
|
$left = $total - $done; |
43
|
1 |
|
$eta = round($rate * $left, 2); |
44
|
|
|
|
45
|
1 |
|
$elapsed = $now - $start_time; |
46
|
|
|
|
47
|
1 |
|
$status_bar .= ' remaining: ' . number_format($eta) . ' sec. elapsed: ' . number_format($elapsed) . ' sec.'; |
48
|
|
|
|
49
|
1 |
|
echo "$status_bar "; |
50
|
|
|
|
51
|
1 |
|
flush(); |
52
|
|
|
|
53
|
|
|
// when done, send a newline |
54
|
1 |
|
if ($done === $total) { |
55
|
1 |
|
echo "\n"; |
56
|
1 |
|
$start_time = NULL; |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
private static function getStatusbar(float $factor, int $size): string { |
61
|
|
|
|
62
|
1 |
|
$bar = (int)round($factor * $size); |
63
|
|
|
|
64
|
1 |
|
$status_bar_pb = str_repeat('█', $bar); |
65
|
1 |
|
if ($bar < $size) { |
66
|
1 |
|
$status_bar_pb .= str_repeat(' ', $size - $bar); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $status_bar_pb; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
private static function getFactor(int $total, int $done): float { |
73
|
|
|
|
74
|
1 |
|
return (float)(($total === 0) ? 1 : ($done / $total)); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
private static function getRate(int $seconds, int $done): float { |
78
|
|
|
|
79
|
1 |
|
return (float)(($done === 0) ? 0 : ($seconds / $done)); |
80
|
|
|
} |
81
|
|
|
} |