2016-02-25 74 views
0

我的Laravel應用程序的一部分使用cli來執行批處理。我正在設法建立一個進度條,以提供有用的信息,告訴您在這個過程中有多遠。我正在做批處理的一件事是地址。我想格式化它有點類似於此:具有可讀多行消息/信息的CLI進度條

Processing addresses... 

Local Shopping Mall 
123 Fake street 
Cityville, USA 
12345 
4/378 [>---------------------------] 1% 

第一個地址後,我想以後移動光標回權「處理的地址...」,我想覆蓋舊地址與新的地址。

現在,我得到這個:

Processing addresses... 
124 Fake street 
Cityville, USA 
12345 

125 Fake street 
Cityville, USA 
12345 

126 Fake street 
Cityville, USA 
12345 

127 Fake street 
Cityville, USA 
12345 

4/378 [>---------------------------] 1% 

這裏的我使用的是(略有修改)代碼:

public function handle() 
    { 
     $this->info('Processing addresses...'); 

     $addresses = \App\Address::all(); 
     $bar = $this->output->createProgressBar(count($addresses)); 
     foreach ($addresses as $address) { 
      $bar->clear(); 
      $this->info("\r" . $this->_getFormattedAddress($address)); 
      $bar->advance(); 
      sleep(1); 
     } 
    } 

    private function _getFormattedAddress(\App\Address $address){ 
     $out = ""; 
     $out .= $address->address1 . "\n"; 
     $out .= $address->address2 . "\n"; 
     $out .= $address->city . "\n"; 
     $out .= $address->region . "\n"; 
     $out .= $address->iso_code . "\n"; 
     $out .= $address->postal_code . "\n"; 
     return $out; 
    } 

回答

0

「\ r」 - 迴歸點開始線$ this-> info() - 將PHP_EOL添加到結束行。

如果您需要將光標返回到頂部輸出。我不知道Laravel是否可以做到。但是這個類可以幫助您:

class OutputCliHelper 
{ 
    protected static $lastLines = 0; 

    /** 
    * @param $data 
    * @return string 
    */ 
    public static function replaceSingleLine($data){ 
     return "\r".$data; 
    } 

    /** 
    * @return int 
    */ 
    public static function getLastLines() 
    { 
     return static::$lastLines; 
    } 

    /** 
    * @param  $data 
    * @param null $lastLines 
    * @return string 
    */ 
    public static function replaceMultiLine($data, $lastLines = null)   { 
     if(!is_null($lastLines)) { 
      static::$lastLines = $lastLines; 
     } 

     $term_width = exec('tput cols', $toss, $status); 
     if($status) { 
      $term_width = 64; // Arbitrary fall-back term width. 
     } 

     $lineCount = 0; 
     foreach(explode(PHP_EOL, $data) as $line) { 
      $lineCount += count(str_split($line, $term_width)); 
     } 

     $magic = ''; 
     // Erasure MAGIC: Clear as many lines as the last output had. 
     for($i = 0; $i < static::$lastLines; $i++) { 
      // Return to the beginning of the line 
      $magic .= "\r"; 
      // Erase to the end of the line 
      $magic .= "\033[K"; 
      // Move cursor Up a line 
      $magic .= "\033[1A"; 
      // Return to the beginning of the line 
      $magic .= "\r"; 
      // Erase to the end of the line 
      $magic .= "\033[K"; 
      // Return to the beginning of the line 
      $magic .= "\r"; 
      // Can be consolodated into 
      // $magic .= "\r\033[K\033[1A\r\033[K\r"; 
     } 
     static::$lastLines = $lineCount; 

     return $magic.$data."\n"; 
    } 
} 

現在,你需要得到輸出字符串(爆PHP_EOL),並調用:

$text = "{$id} Fake street\nCityville, USA\n12345"; 
$progressOutputString = ...; 
$yourOutputString = $text.PHP_EOL.$progressOutputString; 
echo OutputCliHelper::replaceMultiLine($yourOutputString); 

例如:

public function handle() 
{ 
    $output = new BufferedOutput(); //Symfony 
    $progress = new ProgressBar($output); //Symfony 
    $progress->start(10); 
    for ($i = 0; $i < 10; $i++) { 
     $progress->advance(); 
     $tmp = explode(PHP_EOL, $output->fetch()); 
     $string = array_pop($tmp); 
     echo static::replaceMultiLine($string.PHP_EOL.mt_rand()); 
    } 
}