使用Laravel命令行输出
▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░ 58%
类型的进度条
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Progress extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'def:progress {num1 : First Number of the progress Or "year"、"month"} {num2=100}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'echo a progress bar by two number';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
$arg1 = $this->argument('num1');
if ($arg1 == 'year') {
$arg1 = date('z');
$arg2 = 365;
} else if ($arg1 == 'month') {
$arg1 = intval(date('d'));
$arg2 = date('t');
} else {
$arg2 = $this->argument('num2');
}
$percent = ceil($arg1 / $arg2 * 100);
$dark_num = $percent / 5;
$dark_num = $dark_num <= 20 ? $dark_num : 20;
$bright_num = 20 - $dark_num;
$bright_num = $bright_num >= 0 ? $bright_num : 0;
$this->line(str_repeat('▓', $dark_num) . str_repeat('░', $bright_num) . ' ' . $percent . '%');
}
}
在CMD调用: php artisan def:progress 1 5
result:
▓▓▓▓░░░░░░░░░░░░░░░░ 20%
在控制器代码中调用:
use Illuminate\Support\Facades\Artisan;
...
Artisan::call('def:progress', ['num1' => 'year']);
$progress = Artisan::output();
echo $progress;
result:
▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░ 59%
最后更新于 3年 前 by mouc