Skip to content

Commit 987c23f

Browse files
committed
Simplify progress example
1 parent de8b759 commit 987c23f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

examples/progress_advanced.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
}
1111

1212
// Display a progress bar: xxx% [=======> ]
13-
$percent = (int)floor( $downloaded * 100 / $download_size );
14-
$percentage = sprintf('%3d%%', $percent);
15-
$arrow_length = 40;
16-
$arrow_tail_length = max(1, floor(($percent / 100 * $arrow_length) - 3));
17-
$space_length = max(0, $arrow_length - $arrow_tail_length - 3);
18-
$arrow = '[' . str_repeat('=', $arrow_tail_length) . '>' . str_repeat(' ', $space_length) . ']';
19-
echo ' ' . $percentage . ' ' . $arrow . "\r";
13+
$progress_size = 40;
14+
$fraction_downloaded = $downloaded / $download_size;
15+
$dots = round($fraction_downloaded * $progress_size);
16+
printf('%3.0f%% [', $fraction_downloaded * 100 );
17+
$i = 0;
18+
for ( ; $i < $dots - 1; $i++) {
19+
echo '=';
20+
}
21+
echo '>';
22+
for ( ; $i < $progress_size - 1; $i++) {
23+
echo ' ';
24+
}
25+
echo ']' . "\r";
2026
});
2127
$curl->complete(function($instance) {
2228
echo "\n" . 'download complete' . "\n";

0 commit comments

Comments
 (0)