From dc08715bc7543b10de006488f5b3c5b97e8d90e2 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 21 Jun 2020 10:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/ProgressBar.php | 115 ++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/extend/catcher/library/ProgressBar.php b/extend/catcher/library/ProgressBar.php index 821f97c..9539b73 100644 --- a/extend/catcher/library/ProgressBar.php +++ b/extend/catcher/library/ProgressBar.php @@ -7,4 +7,117 @@ * @author JaguarJack * @copyright By CatchAdmin * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt - */ \ No newline at end of file + */ + namespace catcher\library; + + use think\console\Output; + + class ProgressBar + { + protected $output; + + protected $total; + + protected $current = 0; + + protected $header = '[x] '; + + protected $length= 100; + + protected $average; + + public function __construct(Output $output, int $total) + { + $this->output = $output; + + $this->total = $total; + + $this->average = $this->length/$total; + } + + /** + * 开始 + * + * @time 2020年06月20日 + * @return void + */ + public function start() + { + $this->write(); + } + + /** + * 前进 + * + * @time 2020年06月20日 + * @param int $step + * @return void + */ + public function advance($step = 1) + { + $this->current += $step; + + $this->write(); + } + + /** + * 结束 + * + * @time 2020年06月20日 + * @return void + */ + public function finished() + { + $this->write(true); + + $this->current = 1; + } + + /** + * 输出 + * + * @time 2020年06月20日 + * @param bool $end + * @return void + */ + protected function write($end = false) + { + + $bar = $this->bar() . ($end ? '' : "\r"); + + $this->output->write(sprintf('%s', $bar), false); + } + + /** + * 进度条 + * + * @time 2020年06月20日 + * @return string + */ + protected function bar() + { + $left = $this->total - $this->current; + + $empty = str_repeat(' ', $left * $this->average); + + $bar = str_repeat('>', $this->current * $this->average); + + $percent = ((int)(sprintf('%.2f', $this->current/$this->total) * 100)) . '%'; + + return $this->header . $bar . $empty . ' ' . $percent; + } + + /** + * 设置头信息 + * + * @time 2020年06月20日 + * @param $header + * @return $this + */ + public function setHeader($header) + { + $this->header = $header; + + return $this; + } + } \ No newline at end of file