/
AbstractProgressBar.php
155 lines (124 loc) · 4.56 KB
/
AbstractProgressBar.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace JBZoo\Cli\ProgressBars;
use JBZoo\Utils\Arr;
use JBZoo\Utils\Dates;
use JBZoo\Utils\FS;
use JBZoo\Utils\Stats;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Helper\Helper as SymfonyHelper;
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar;
/**
* Class AbstractProgressBar
* @package JBZoo\Cli\ProgressBars
*/
abstract class AbstractProgressBar
{
public const BREAK = '<yellow>Progress stopped</yellow>';
public const MAX_LINE_LENGTH = 80;
/**
* @var int[]
*/
protected $stepMemoryDiff = [];
/**
* @var float[]
*/
protected $stepTimers = [];
/**
* @return string
*/
abstract protected function buildTemplate(): string;
/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function configureProgressBar(): void
{
static $inited;
if ($inited) {
return;
}
$inited = true;
// Memory
self::setPlaceholder('jbzoo_memory_current', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_usage(false));
});
self::setPlaceholder('jbzoo_memory_peak', static function (): string {
return SymfonyHelper::formatMemory(\memory_get_peak_usage(false));
});
self::setPlaceholder('jbzoo_memory_limit', static function (): string {
return Sys::iniGet('memory_limit');
});
self::setPlaceholder('jbzoo_memory_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}
return FS::format((int)Stats::mean($this->stepMemoryDiff));
});
self::setPlaceholder('jbzoo_memory_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepMemoryDiff) === 0) {
return 'n/a';
}
return FS::format(Arr::last($this->stepMemoryDiff));
});
// Timers
self::setPlaceholder('jbzoo_time_elapsed', static function (SymfonyProgressBar $bar): string {
return Dates::formatTime(\time() - $bar->getStartTime());
});
self::setPlaceholder('jbzoo_time_remaining', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}
if (!$bar->getProgress()) {
$remaining = 0;
} else {
$remaining = \round(
(\time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())
);
}
return Dates::formatTime($remaining);
});
self::setPlaceholder('jbzoo_time_estimated', static function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps()) {
return 'n/a';
}
if (!$bar->getProgress()) {
$estimated = 0;
} else {
$estimated = \round((\time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
}
return Dates::formatTime($estimated);
});
self::setPlaceholder('jbzoo_time_step_avg', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}
return \str_replace('±', '<blue>±</blue>', Stats::renderAverage($this->stepTimers)) . ' sec';
});
self::setPlaceholder('jbzoo_time_step_last', function (SymfonyProgressBar $bar): string {
if (!$bar->getMaxSteps() || !$bar->getProgress() || \count($this->stepTimers) === 0) {
return 'n/a';
}
return Dates::formatTime(Arr::last($this->stepTimers));
});
}
/**
* @param string $name
* @param callable $callable
*/
public static function setPlaceholder(string $name, callable $callable): void
{
SymfonyProgressBar::setPlaceholderFormatterDefinition($name, $callable);
}
}