@@ -18,6 +18,8 @@ class ProgressBar
18
18
private $ _current = 0 ;
19
19
private $ _percent = 0 ;
20
20
private $ _counterMask = '{current}/{all} ({percent}%) ' ;
21
+ private $ _barWidth = 50 ;
22
+ private $ _currentBarWidth = 0 ;
21
23
22
24
public function initialize (Writer $ output , $ size )
23
25
{
@@ -29,10 +31,12 @@ public function increment()
29
31
{
30
32
$ this ->_current ++;
31
33
$ this ->_calculatePercent ();
34
+ $ this ->_calculateBarWidth ();
35
+ $ fill = $ this ->_fillMask () . ' ' . $ this ->_generateBar ();
32
36
if ($ this ->_current == $ this ->_size ) {
33
- $ this ->_output ->writeMessage ($ this -> _fillMask () );
37
+ $ this ->_output ->writeMessage ($ fill );
34
38
} else {
35
- $ this ->_output ->writeMessage ($ this -> _fillMask () , 1 , Writer::CR );
39
+ $ this ->_output ->writeMessage ($ fill , 1 , Writer::CR );
36
40
}
37
41
}
38
42
@@ -41,6 +45,11 @@ private function _calculatePercent()
41
45
$ this ->_percent = round ($ this ->_current * 100 / $ this ->_size );
42
46
}
43
47
48
+ private function _calculateBarWidth ()
49
+ {
50
+ $ this ->_currentBarWidth = round ($ this ->_barWidth * $ this ->_percent / 100 );
51
+ }
52
+
44
53
private function _fillMask ()
45
54
{
46
55
$ matches = array (
@@ -50,4 +59,9 @@ private function _fillMask()
50
59
);
51
60
return str_replace (array_keys ($ matches ), array_values ($ matches ), $ this ->_counterMask );
52
61
}
62
+
63
+ private function _generateBar ()
64
+ {
65
+ return "[ " . str_pad (str_repeat ('= ' , $ this ->_currentBarWidth ), $ this ->_barWidth , '. ' ) . "] " ;
66
+ }
53
67
}
0 commit comments