cli 模式下,动态展示信息 [ 技术分享 ]
<?php
namespace console\controllers;
use yii\console\Controller;
class TController extends Controller
{
private function replaceable_echo($message, $force_clear_lines = NULL)
{
static $last_lines = 0;
if(!is_null($force_clear_lines)) {
$last_lines = $force_clear_lines;
}
$term_width = exec('tput cols', $toss, $status);
if($status) {
$term_width = 64; // Arbitrary fall-back term width.
}
$line_count = 0;
foreach(explode("\n", $message) as $line) {
$line_count += count(str_split($line, $term_width));
}
// Erasure MAGIC: Clear as many lines as the last output had.
for($i = 0; $i < $last_lines; $i++) {
// Return to the beginning of the line
echo "\r";
// Erase to the end of the line
echo "\033[K";
// Move cursor Up a line
echo "\033[1A";
// Return to the beginning of the line
echo "\r";
// Erase to the end of the line
echo "\033[K";
// Return to the beginning of the line
echo "\r";
// Can be consolodated into
// echo "\r\033[K\033[1A\r\033[K\r";
}
$last_lines = $line_count;
echo $message."\n";
}
public function actionIndex()
{
$line = '';
for ($i=0;$i<=100;$i++){
$message = '当前进度为:';
$line .='=';
$message .= $line.">$i%";
echo $this->replaceable_echo($message);
usleep(100000);
}
}
}
效果图:
共 0 条回复
没有找到数据。
小叮当的肚兜
注册时间:2016-10-31
最后登录:17小时前
在线时长:97小时45分
最后登录:17小时前
在线时长:97小时45分
- 粉丝13
- 金钱44010
- 威望270
- 积分47680