forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.php
More file actions
192 lines (164 loc) · 3.46 KB
/
Copy pathProgressBar.php
File metadata and controls
192 lines (164 loc) · 3.46 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 2017/3/7 0007
* Time: 22:36
*/
namespace inhere\console\utils;
use inhere\console\Helper;
use inhere\console\io\Output;
/**
* Class ProgressBar
* @package inhere\console\utils
*
*
* ```
* 1 [->--------------------------]
* 3 [■■■>------------------------]
* 25/50 [==============>-------------] 50%
* ```
*
*/
class ProgressBar
{
// options
private $barWidth = 30;
private $completeChar; // 完成的
private $progressChar = '>'; // 当前进度
private $remainingChar = '-'; // 剩下的
private $startTime;
private $percent;
private $step = 0;
private $max;
private $stepWidth;
private $format ='{bar} {percent}({complete}/{total})';
/**
* @param Output $output
* @param array $config
* @return ProgressBar
*/
public static function create(Output $output, array $config = [])
{
return new self($output, $config);
}
public function __construct(Output $output, array $config = [])
{
$this->config = $config;
}
/**
* 开始
* @param null $max
*/
public function start($max=null)
{
$this->startTime = time();
$this->step = 0;
$this->percent = 0.0;
if (null !== $max) {
$this->setMaxSteps($max);
}
$this->display();
}
/**
* 前进
* @param int $step
*/
public function advance($step = 1)
{
}
/**
* 完成
*/
public function finish()
{
}
protected function display()
{
return preg_replace_callback('/({[\w_]+})/', function($matched) {
}, $this->format);
}
/**
* Sets the progress bar maximal steps.
* @param int $max The progress bar max steps
*/
private function setMaxSteps($max)
{
$this->max = max(0, (int) $max);
$this->stepWidth = $this->max ? Helper::strLen($this->max) : 4;
}
/**
* @return int
*/
public function getBarWidth()
{
return $this->barWidth;
}
/**
* @param int $barWidth
*/
public function setBarWidth($barWidth)
{
$this->barWidth = $barWidth;
}
/**
* @param mixed $completeChar
*/
public function setCompleteChar($completeChar)
{
$this->completeChar = $completeChar;
}
/**
* Gets the complete bar character.
* @return string A character
*/
public function getCompleteChar()
{
if (null === $this->completeChar) {
return $this->max ? '=' : $this->completeChar;
}
return $this->completeChar;
}
/**
* @return string
*/
public function getProgressChar()
{
return $this->progressChar;
}
/**
* @param string $progressChar
*/
public function setProgressChar($progressChar)
{
$this->progressChar = $progressChar;
}
/**
* @return string
*/
public function getRemainingChar()
{
return $this->remainingChar;
}
/**
* @param string $remainingChar
*/
public function setRemainingChar($remainingChar)
{
$this->remainingChar = $remainingChar;
}
/**
* @return string
*/
public function getFormat()
{
return $this->format;
}
/**
* @param string $format
*/
public function setFormat($format)
{
$this->format = $format;
}
}