小叮当的肚兜 2019-07-18 11:33:27 156次浏览 0条回复 0 1 0
<?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);
        }
    }

}

效果图: Peek 2019-07-18 11-32.gif

觉得很赞
亿速云
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册