Skip to content

Commit c1975fc

Browse files
committed
update
1 parent 0882c02 commit c1975fc

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

src/utils/Interact.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,39 @@ public static function loopAsk($question, $default = null, \Closure $validator =
255255
return $answer;
256256
}
257257

258+
public static $startTime = 0;
259+
public static $endTime = 0;
260+
public static $stepWidth = 2;
261+
public static $step = 0;
262+
public static $max = 0;
263+
private static $options = [
264+
'format' => '[{bar}] {percent:3s}%({current}/{max})',
265+
];
258266

259-
public static function progressBarSetting()
267+
/**
268+
* @param array $options
269+
*/
270+
public static function progressBarOptions(array $options)
260271
{
261-
272+
self::$options = array_merge(self::$options, $options);
262273
}
263274

264-
public static function progressBarStart()
275+
public static function progressBarStart($max)
265276
{
266-
277+
self::$startTime = time();
278+
self::$max = time();
267279
}
268280

269-
public static function progressBarUp()
281+
public static function progressBarUp($step = 1)
270282
{
271283

272284
}
273285

274286
public static function progressBarEnd()
275287
{
288+
self::$endTime = time();
276289

290+
self::$max = 0;
277291
}
278292

279293
/**

src/utils/ProgressBar.php

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,17 @@ class ProgressBar
9292

9393
/**
9494
* @param OutputInterface $output
95-
* @param array $config
95+
* @param int $maxSteps
9696
* @return ProgressBar
9797
*/
9898
public static function create(OutputInterface $output = null, int $maxSteps = 0)
9999
{
100-
return new self($output, $config);
100+
return new self($output, $maxSteps);
101101
}
102102

103103
/**
104104
* @param OutputInterface $output
105-
* @param array $config
106-
* @return ProgressBar
105+
* @param int $maxSteps
107106
*/
108107
public function __construct(OutputInterface $output = null, int $maxSteps = 0)
109108
{
@@ -121,7 +120,7 @@ public function __construct(OutputInterface $output = null, int $maxSteps = 0)
121120
public function start($maxSteps = null)
122121
{
123122
if ($this->started) {
124-
throw new LogicException('Progress bar already started.');
123+
throw new \LogicException('Progress bar already started.');
125124
}
126125

127126
$this->startTime = time();
@@ -143,7 +142,7 @@ public function start($maxSteps = null)
143142
public function advance(int $step = 1)
144143
{
145144
if (!$this->started) {
146-
throw new LogicException('Progress indicator has not yet been started.');
145+
throw new \LogicException('Progress indicator has not yet been started.');
147146
}
148147

149148
$this->advanceTo($this->step + $step);
@@ -243,8 +242,12 @@ public function render(string $text)
243242
$this->output->write($text, false);
244243
}
245244

245+
/**
246+
* @return mixed
247+
*/
246248
protected function buildLine()
247249
{
250+
// $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
248251
return preg_replace_callback('/({[\w_]+})/i', function ($matches) {
249252
if ($formatter = $this->getFormatter($matches[1])) {
250253
$text = call_user_func($formatter, $this, $this->output);
@@ -295,6 +298,22 @@ public function getFormatter(string $section, bool $throwException = false)
295298
return null;
296299
}
297300

301+
/**
302+
* @return array
303+
*/
304+
public function getMessages(): array
305+
{
306+
return $this->messages;
307+
}
308+
309+
/**
310+
* @param array $messages
311+
*/
312+
public function setMessages(array $messages)
313+
{
314+
$this->messages = $messages;
315+
}
316+
298317
/**
299318
* set a named Message
300319
* @param string $message The text to associate with the placeholder
@@ -305,6 +324,10 @@ public function setMessage($message, string $name = 'message')
305324
$this->messages[$name] = $message;
306325
}
307326

327+
/**
328+
* @param string $name
329+
* @return string
330+
*/
308331
public function getMessage(string $name = 'message')
309332
{
310333
return $this->messages[$name];
@@ -349,6 +372,30 @@ private function setMaxSteps(int $maxSteps)
349372
$this->stepWidth = $this->maxSteps ? Helper::strLen($this->maxSteps) : 2;
350373
}
351374

375+
/**
376+
* @return int
377+
*/
378+
public function getMaxSteps(): int
379+
{
380+
return $this->maxSteps;
381+
}
382+
383+
/**
384+
* @return int
385+
*/
386+
public function getStepWidth(): int
387+
{
388+
return $this->stepWidth;
389+
}
390+
391+
/**
392+
* @param int $stepWidth
393+
*/
394+
public function setStepWidth(int $stepWidth)
395+
{
396+
$this->stepWidth = $stepWidth;
397+
}
398+
352399
/**
353400
* @return int
354401
*/
@@ -380,7 +427,7 @@ public function setCompleteChar(string $completeChar)
380427
public function getCompleteChar(): string
381428
{
382429
if (null === $this->completeChar) {
383-
return $this->max ? '=' : $this->completeChar;
430+
return $this->maxSteps ? '=' : $this->completeChar;
384431
}
385432

386433
return $this->completeChar;
@@ -418,16 +465,25 @@ public function setRemainingChar(string $remainingChar)
418465
$this->remainingChar = $remainingChar;
419466
}
420467

468+
/**
469+
* @return float
470+
*/
421471
public function getPercent()
422472
{
423473
return $this->percent;
424474
}
425475

476+
/**
477+
* @return mixed
478+
*/
426479
public function getStartTime()
427480
{
428481
return $this->startTime;
429482
}
430483

484+
/**
485+
* @return mixed
486+
*/
431487
public function getFinishTime()
432488
{
433489
return $this->finishTime;
@@ -449,12 +505,15 @@ public function setFormat(string $format)
449505
$this->format = $format;
450506
}
451507

508+
/**
509+
* @return array
510+
*/
452511
private static function loadDefaultFormatters()
453512
{
454513
return [
455-
'bar' => function (ProgressBar $bar, OutputInterface $output) {
514+
'bar' => function (ProgressBar $bar) {
456515
$completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth());
457-
$display = str_repeat($bar->getBarCharacter(), $completeBars);
516+
$display = str_repeat($bar->getCompleteChar(), $completeBars);
458517

459518
if ($completeBars < $bar->getBarWidth()) {
460519
$emptyBars = $bar->getBarWidth() - $completeBars;
@@ -468,7 +527,7 @@ private static function loadDefaultFormatters()
468527
},
469528
'remaining' => function (ProgressBar $bar) {
470529
if (!$bar->getMaxSteps()) {
471-
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
530+
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
472531
}
473532

474533
if (!$bar->getProgress()) {
@@ -481,7 +540,7 @@ private static function loadDefaultFormatters()
481540
},
482541
'estimated' => function (ProgressBar $bar) {
483542
if (!$bar->getMaxSteps()) {
484-
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
543+
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
485544
}
486545

487546
if (!$bar->getProgress()) {
@@ -492,7 +551,7 @@ private static function loadDefaultFormatters()
492551

493552
return Helper::formatTime($estimated);
494553
},
495-
'memory' => function (ProgressBar $bar) {
554+
'memory' => function () {
496555
return Helper::formatMemory(memory_get_usage(true));
497556
},
498557
'current' => function (ProgressBar $bar) {
@@ -507,6 +566,9 @@ private static function loadDefaultFormatters()
507566
];
508567
}
509568

569+
/**
570+
* @return array
571+
*/
510572
private static function defaultFormats()
511573
{
512574
return [

0 commit comments

Comments
 (0)