Skip to content

Commit 196733e

Browse files
committed
update
1 parent 0be7828 commit 196733e

File tree

3 files changed

+37
-59
lines changed

3 files changed

+37
-59
lines changed

src/Utils/Interact.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -263,41 +263,6 @@ public static function limitedAsk($question, $default = null, \Closure $validato
263263
return $answer;
264264
}
265265

266-
public static $startTime = 0;
267-
public static $endTime = 0;
268-
public static $stepWidth = 2;
269-
public static $step = 0;
270-
public static $max = 0;
271-
private static $options = [
272-
'format' => '[{bar}] {percent:3s}%({current}/{max})',
273-
];
274-
275-
/**
276-
* @param array $options
277-
*/
278-
public static function progressBarOptions(array $options)
279-
{
280-
self::$options = array_merge(self::$options, $options);
281-
}
282-
283-
public static function progressBarStart($max)
284-
{
285-
self::$startTime = time();
286-
self::$max = time();
287-
}
288-
289-
public static function progressBarUp($step = 1)
290-
{
291-
292-
}
293-
294-
public static function progressBarEnd()
295-
{
296-
self::$endTime = time();
297-
298-
self::$max = 0;
299-
}
300-
301266
/**
302267
* 读取输入信息
303268
* @param mixed $message 若不为空,则先输出文本

src/Utils/ProgressBar.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function setFormat(string $format)
509509
private static function loadDefaultParsers()
510510
{
511511
return [
512-
'bar' => function (ProgressBar $bar) {
512+
'bar' => function (self $bar) {
513513
$completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth());
514514
$display = str_repeat($bar->getCompleteChar(), $completeBars);
515515

@@ -520,10 +520,10 @@ private static function loadDefaultParsers()
520520

521521
return $display;
522522
},
523-
'elapsed' => function (ProgressBar $bar) {
523+
'elapsed' => function (self $bar) {
524524
return Helper::formatTime(time() - $bar->getStartTime());
525525
},
526-
'remaining' => function (ProgressBar $bar) {
526+
'remaining' => function (self $bar) {
527527
if (!$bar->getMaxSteps()) {
528528
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
529529
}
@@ -536,7 +536,7 @@ private static function loadDefaultParsers()
536536

537537
return Helper::formatTime($remaining);
538538
},
539-
'estimated' => function (ProgressBar $bar) {
539+
'estimated' => function (self $bar) {
540540
if (!$bar->getMaxSteps()) {
541541
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
542542
}
@@ -552,13 +552,13 @@ private static function loadDefaultParsers()
552552
'memory' => function () {
553553
return Helper::formatMemory(memory_get_usage(true));
554554
},
555-
'current' => function (ProgressBar $bar) {
555+
'current' => function (self $bar) {
556556
return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
557557
},
558-
'max' => function (ProgressBar $bar) {
558+
'max' => function (self $bar) {
559559
return $bar->getMaxSteps();
560560
},
561-
'percent' => function (ProgressBar $bar) {
561+
'percent' => function (self $bar) {
562562
return floor($bar->getPercent() * 100);
563563
},
564564
];

src/Utils/Show.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
/**
1414
* Class Show
1515
* show formatted message text
16-
*
1716
* @package Inhere\Console\Utils
18-
*
1917
* @method static int info($messages, $quit = false)
2018
* @method static int note($messages, $quit = false)
2119
* @method static int notice($messages, $quit = false)
@@ -24,7 +22,6 @@
2422
* @method static int warning($messages, $quit = false)
2523
* @method static int danger($messages, $quit = false)
2624
* @method static int error($messages, $quit = false)
27-
*
2825
* @method static int liteInfo($messages, $quit = false)
2926
* @method static int liteNote($messages, $quit = false)
3027
* @method static int liteNotice($messages, $quit = false)
@@ -277,7 +274,6 @@ public static function section($title, $body, array $opts = [])
277274

278275
/**
279276
* Show a list
280-
*
281277
* ```
282278
* $title = 'list title';
283279
* $data = [
@@ -316,7 +312,6 @@ public static function aList($data, $title = null, array $opts = [])
316312

317313
/**
318314
* Show multi list
319-
*
320315
* ```
321316
* [
322317
* 'list1 title' => [
@@ -330,7 +325,6 @@ public static function aList($data, $title = null, array $opts = [])
330325
* ... ...
331326
* ]
332327
* ```
333-
*
334328
* @param array $data
335329
* @param array $opts
336330
*/
@@ -353,15 +347,11 @@ public static function mList(array $data, array $opts = [])
353347

354348
/**
355349
* Show console help message
356-
*
357350
* @param array $config The config data
358-
*
359351
* There are config structure. you can setting some or ignore some. will only render it when value is not empty.
360-
*
361352
* [
362353
* description string The description text. e.g 'Composer version 1.3.2'
363354
* usage string The usage message text. e.g 'command [options] [arguments]'
364-
*
365355
* commands array|string The command list. e.g:
366356
* [
367357
* // command => description
@@ -382,7 +372,6 @@ public static function mList(array $data, array $opts = [])
382372
* '-h, --help' => 'Display this help message'
383373
* ... ...
384374
* ]
385-
*
386375
* examples array|string The command usage example. e.g 'php server.php {start|reload|restart|stop} [-d]'
387376
* ]
388377
* @param bool $showAfterQuit Show help after quit
@@ -548,6 +537,7 @@ public static function panel($data, $title = 'Information Panel', array $opts =
548537
}
549538

550539
unset($panelData);
540+
551541
return 0;
552542
}
553543

@@ -557,7 +547,6 @@ public static function panel($data, $title = 'Information Panel', array $opts =
557547
* @param string $title
558548
* @param array $opts
559549
* @example
560-
*
561550
* ```
562551
* // like from database query's data.
563552
* $data = [
@@ -566,14 +555,12 @@ public static function panel($data, $title = 'Information Panel', array $opts =
566555
* ... ...
567556
* ];
568557
* Show::table($data, 'a table');
569-
*
570558
* // use custom head
571559
* $data = [
572560
* [ value1, value2, value3, ... ], // first row
573561
* [ value4, value5, value6, ... ], // second row
574562
* ... ...
575563
* ];
576-
*
577564
* $opts = [
578565
* 'showBorder' => true,
579566
* 'tHead' => [col1, col2, col3, ...]
@@ -751,7 +738,6 @@ public static function progressTxt($total, $msg, $doneMsg = '')
751738

752739
/**
753740
* a simple progress bar by 'yield'
754-
*
755741
* ```php
756742
* $i = 0;
757743
* $total = 120;
@@ -760,14 +746,12 @@ public static function progressTxt($total, $msg, $doneMsg = '')
760746
* 'doneChar' => '#'
761747
* ]);
762748
* echo "progress:\n";
763-
*
764749
* while ($i <= $total) {
765750
* $bar->send($i);
766751
* usleep(50000);
767752
* $i++;
768753
* }
769754
* ```
770-
*
771755
* @param int $total
772756
* @param array $opts
773757
* @internal int $current
@@ -814,6 +798,35 @@ public static function progressBar($total, array $opts = [])
814798
yield false;
815799
}
816800

801+
/**
802+
* create ProgressBar
803+
*
804+
* ```php
805+
* $max = 200;
806+
* $bar = Show::createProgressBar($max);
807+
* while ($i <= $total) {
808+
* $bar->advance();
809+
* usleep(50000);
810+
* $i++;
811+
* }
812+
* $bar->finish();
813+
* ```
814+
*
815+
* @param int $max
816+
* @param bool $start
817+
* @return ProgressBar
818+
*/
819+
public static function createProgressBar($max = 0, $start = true)
820+
{
821+
$bar = new ProgressBar(null, $max);
822+
823+
if ($start) {
824+
$bar->start();
825+
}
826+
827+
return $bar;
828+
}
829+
817830
/////////////////////////////////////////////////////////////////
818831
/// Helper Method
819832
/////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)