Clear PHP CLI output

前端 未结 10 1894
一整个雨季
一整个雨季 2020-11-29 22:15

I\'m trying to get a \"live\" progress indicator working on my php CLI app. Rather than outputting as

1Done
2Done
3Done

I would rather it c

相关标签:
10条回答
  • 2020-11-29 22:41

    i know the question isn't strictly about how to clear a SINGLE LINE in PHP, but this is the top google result for "clear line cli php", so here is how to clear a single line:

    function clearLine()
    {
        echo "\033[2K\r";
    }
    
    0 讨论(0)
  • 2020-11-29 22:43

    Console functions are platform dependent and as such PHP has no built-in functions to deal with this. system and other similar functions won't work in this case because PHP captures the output of these programs and prints/returns them. What PHP prints goes to standard output and not directly to the console, so "printing" the output of cls won't work.

    0 讨论(0)
  • 2020-11-29 22:44
    <?php
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    
    function bufferout($newline, $buffer=null){
        $count = strlen(rtrim($buffer));
        $buffer = $newline;
        if(($whilespace = $count-strlen($buffer))>=1){
            $buffer .= str_repeat(" ", $whilespace);
        }
        return $buffer."\r"; 
    };
    
    $start = "abcdefghijklmnopqrstuvwxyz0123456789";
    $i = strlen($start);
    
    while ($i >= 0){
        $new = substr($start, 0, $i);
        if($old){
            echo $old = bufferout($new, $old);
        }else{
            echo $old = bufferout($new);
        }
        sleep(1);
        $i--;
    }
    ?>
    

    A simple implementation of @dkamins answer. It works well. It's a bit- hack-ish. But does the job. Wont work across multiple lines.

    0 讨论(0)
  • 2020-11-29 22:52

    Tried some of solutions from answers:

    <?php
    ...
        $messages = [
            '11111',
            '2222',
            '333',
            '44',
            '5',
        ];
    
        $endlines = [
            "\r",
            "\033[2K\r",
            "\r\033[K\033[1A\r\033[K\r",
            chr(27).chr(91).'H'.chr(27).chr(91).'J',
        ];
        foreach ($endlines as $i=>$end) {
            foreach ($messages as $msg) {
                output()->write("$i. ");
                output()->write($msg);
                sleep(1);
                output()->write($end);
            }
        }
    

    And \033[2K\r seems like works correct.

    0 讨论(0)
  • 2020-11-29 22:53

    something like this :

    for ($i = 0; $i <= 100; $i++) {
        echo "Loading... {$i}%\r";
        usleep(10000);
    }
    
    0 讨论(0)
  • 2020-11-29 22:56

    I came across this while searching for a multi line solution to this problem. This is what I eventually came up with. You can use Ansi Escape commands. http://www.inwap.com/pdp10/ansicode.txt

    <?php
    function replaceOut($str)
    {
        $numNewLines = substr_count($str, "\n");
        echo chr(27) . "[0G"; // Set cursor to first column
        echo $str;
        echo chr(27) . "[" . $numNewLines ."A"; // Set cursor up x lines
    }
    
    while (true) {
        replaceOut("First Ln\nTime: " . time() . "\nThird Ln");
        sleep(1);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题