This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
/
ProgressBar.php
109 lines (86 loc) · 3.93 KB
/
ProgressBar.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
<?php
/*
The MIT License (MIT)
Copyright (c) 2013 Randi Miller <Randi@randimiller.me>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class ProgressBar {
private $bar_length;
private $total;
private $start_time = null;
private $use_shimmer;
private $shimmer_location = 0;
/*
Only value needed is the total number of cycles to be completed
Optional: bar_length is the length of the bar (total number of '=')
*/
public function __construct($total, $bar_length = 40, $use_shimmer = false) {
$this->bar_length = $bar_length;
$this->total = $total;
$this->use_shimmer = $use_shimmer;
}
/*
Updates the progress bar and meta data
The only param needed is the number that has been completed
thus far
*/
public function update($done) {
$output = "";
// Get the current time when running for the first time
if($this->start_time === null) {
$this->start_time = new DateTime();
}
// Get the amount of current run time
$interval = $this->start_time->diff(new DateTime());
$runtime = $interval->format("%H:%I:%S");
// Find out the percentage completed
$percent = $done / $this->total;
// Starting point of the progress bar
$output .= "\r |";
// Stop division by zero
if($percent > 0) {
// Calculate the number of blocks that should be shown
$blocks = round($percent * $this->bar_length );
// Display the progress
$shaft = str_repeat("=", $blocks) . ">";
//Add shimmer effect if enabled
if ($this->use_shimmer) {
if ( $done%10 == 0 ){
$this->shimmer_location = --$this->shimmer_location < 0 ? $this->bar_length : $this->shimmer_location;
}
if ( $this->shimmer_location!=strlen($shaft)-1) { //don't shimmer over the arrow
$shaft = substr_replace($shaft, ' ', $this->shimmer_location, 1);
}
}
$output .= $shaft;
$output .= str_repeat(" ", ($this->bar_length - $blocks));
$output .= "| ";
} else {
$output .= str_repeat(" ", $this->bar_length) . "| ";
}
// Show some meta data
$percent = number_format($percent * 100, 2);
$output .= "{$done}/{$this->total} -- Complete: {$percent}% -- Time: {$runtime}";
// Yippee! All finished!
if($percent == 100) {
$output .= "\n\nStatus: Complete\n";
$output .= "Runtime: {$runtime}\n";
$output .= "Total cycles: {$done} \n\n";
}
echo $output;
}
}