public static function progress(string $msg, float $p)
{
// Bound the percentage to [0, 1]
$p = min(max($p, 0.0), 1.0);
if (!Config::get()->progress_bar || Config::get()->dump_ast) {
return;
}
// Don't update every time when we're moving
// super fast
if ($p < 1.0 && rand(0, 1000) > 1000 * Config::get()->progress_bar_sample_rate) {
return;
}
// If we're on windows, just print a dot to show we're
// working
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
fwrite(STDERR, '.');
return;
}
$memory = memory_get_usage() / 1024 / 1024;
$peak = memory_get_peak_usage() / 1024 / 1024;
$padded_message = str_pad($msg, 10, ' ', STR_PAD_LEFT);
fwrite(STDERR, "{$padded_message} ");
$current = (int) ($p * 60);
$rest = max(60 - $current, 0);
fwrite(STDERR, str_repeat("█", $current));
fwrite(STDERR, str_repeat("░", $rest));
fwrite(STDERR, " " . sprintf("% 3d", (int) (100 * $p)) . "%");
fwrite(STDERR, sprintf(' %0.2dMB/%0.2dMB', $memory, $peak) . "\r");
}