|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Hal\Component\Output; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CliOutput |
|
14
|
|
|
* @package Hal\Component\Output |
|
15
|
|
|
*/ |
|
16
|
|
|
/** |
|
17
|
|
|
* Class CliOutput |
|
18
|
|
|
* @package Hal\Component\Issue |
|
19
|
|
|
*/ |
|
20
|
|
|
/** |
|
21
|
|
|
* Class ProgressBar |
|
22
|
|
|
* @package Hal\Component\Output |
|
23
|
|
|
*/ |
|
24
|
|
|
class ProgressBar |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Output |
|
29
|
|
|
*/ |
|
30
|
|
|
private $output; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $max; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
private $current = 0; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* ProgressBar constructor. |
|
44
|
|
|
* @param Output $output |
|
45
|
|
|
* @param int $max |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Output $output, $max) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->output = $output; |
|
50
|
|
|
$this->max = $max; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Start progress bar |
|
55
|
|
|
*/ |
|
56
|
|
|
public function start() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->current = 0; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Advance progress bar |
|
63
|
|
|
*/ |
|
64
|
|
|
public function advance() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->current++; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->hasAnsi()) { |
|
69
|
|
|
$percent = round($this->current / $this->max * 100); |
|
70
|
|
|
$this->output->write("\x0D"); |
|
71
|
|
|
$this->output->write("\x1B[2K"); |
|
72
|
|
|
$this->output->write(sprintf('... %s%% ...', $percent)); |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->output->write('.'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Clear console |
|
80
|
|
|
*/ |
|
81
|
|
|
public function clear() |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->hasAnsi()) { |
|
84
|
|
|
$this->output->write("\x0D"); |
|
85
|
|
|
$this->output->write("\x1B[2K"); |
|
86
|
|
|
$this->output->clearln(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Detects ANSI support |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function hasAnsi() |
|
96
|
|
|
{ |
|
97
|
|
|
if (DIRECTORY_SEPARATOR === '\\') { |
|
98
|
|
|
return |
|
99
|
|
|
0 >= version_compare('10.0.10586', |
|
100
|
|
|
PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD) |
|
101
|
|
|
|| false !== getenv('ANSICON') |
|
102
|
|
|
|| 'ON' === getenv('ConEmuANSI') |
|
103
|
|
|
|| 'xterm' === getenv('TERM'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return function_exists('posix_isatty') && @posix_isatty($this->stream); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: