1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package MissingBits/ConsoleOutput |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-the-missing-bits |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MissingBits\ConsoleOutput; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* echo a character for each item, along with '[x / y]' to show overall |
48
|
|
|
* progress |
49
|
|
|
*/ |
50
|
|
|
class ShowProgress |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* echo a character for each item, along with '[x / y]' to show overall |
54
|
|
|
* progress |
55
|
|
|
* |
56
|
|
|
* @param string $progressChar |
57
|
|
|
* what should we output? |
58
|
|
|
* this should be a single character; ASCII codes are fine |
59
|
|
|
* as long as they only move the cursor 1 character to the |
60
|
|
|
* right after they have completed |
61
|
|
|
* @param int $currentProgress |
62
|
|
|
* how much progress has been made? |
63
|
|
|
* @param int $maxProgress |
64
|
|
|
* what is the maximum value of $currentProgress? |
65
|
|
|
* @param int $consoleWidth |
66
|
|
|
* how wide is the terminal display? |
67
|
|
|
* default assumes UNIX standard width of 80 chars |
68
|
|
|
* @param callback $printerFunc |
69
|
|
|
* what should we use to write to the console? |
70
|
|
|
* this needs to accept printf() arguments |
71
|
|
|
* @return string |
72
|
|
|
* anything returned by your $printerFunc |
73
|
|
|
*/ |
74
|
|
|
public static function with($progressChar, $currentProgress, $maxProgress, $consoleWidth = 80, $printerFunc = 'printf') |
75
|
|
|
{ |
76
|
|
|
// build the current progress |
77
|
|
|
// |
78
|
|
|
// we do this to ensure that we call `$printerFunc` a maximum |
79
|
|
|
// of once |
80
|
|
|
// |
81
|
|
|
// hopefully this is better for performance |
82
|
|
|
$format = "%s"; |
83
|
|
|
$formatParams = [ $progressChar ]; |
84
|
|
|
|
85
|
|
|
// do we need to output anything else? |
86
|
|
|
$maxString = (string)$maxProgress; |
87
|
|
|
$maxPerRow = $consoleWidth - 10 - (strlen($maxString) * 2); |
88
|
|
|
$remainderPerRow = $currentProgress % $maxPerRow; |
89
|
|
|
|
90
|
|
|
// have we reached 100% progress? |
91
|
|
|
if ($currentProgress == $maxProgress) { |
92
|
|
|
$format .= "%s"; |
93
|
|
|
$formatParams[] = str_repeat(' ', $remainderPerRow); |
94
|
|
|
|
95
|
|
|
list($format, $formatParams) = self::addProgressCount($currentProgress, $maxProgress, $format, $formatParams); |
96
|
|
|
} |
97
|
|
|
else if ($remainderPerRow === 0) { |
98
|
|
|
// we have reached the end of the console line |
99
|
|
|
list($format, $formatParams) = self::addProgressCount($currentProgress, $maxProgress, $format, $formatParams); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// all done |
103
|
|
|
return call_user_func_array($printerFunc, array_append_values([$format], $formatParams)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* add a summary of progress to date to the existing format string |
108
|
|
|
* and associated parameters |
109
|
|
|
* |
110
|
|
|
* @param int $currentProgress |
111
|
|
|
* @param int $maxProgress |
112
|
|
|
* @param string $format |
113
|
|
|
* @param array $formatParams |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
private static function addProgressCount($currentProgress, $maxProgress, $format, $formatParams) |
117
|
|
|
{ |
118
|
|
|
$maxString = (string)$maxProgress; |
119
|
|
|
$format .= ' [%' . strlen($maxString) . 's / %s]' . PHP_EOL; |
120
|
|
|
$formatParams[] = (string)$currentProgress; |
121
|
|
|
$formatParams[] = $maxString; |
122
|
|
|
|
123
|
|
|
return [ $format, $formatParams ]; |
124
|
|
|
} |
125
|
|
|
} |