import v1.1.0_beta1 | 2009-08-21

This commit is contained in:
2019-07-17 22:16:19 +02:00
parent 2c1152f0d3
commit 8dee6b1a10
2306 changed files with 251360 additions and 23428 deletions

View File

@ -14,7 +14,7 @@
* @package Zend_ProgressBar
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Console.php 12233 2008-11-01 00:11:01Z dasprid $
* @version $Id: Console.php 14557 2009-03-31 12:06:30Z dasprid $
*/
/**
@ -22,6 +22,11 @@
*/
require_once 'Zend/ProgressBar/Adapter.php';
/**
* @see Zend_Text_MultiByte
*/
require_once 'Zend/Text/MultiByte.php';
/**
* Zend_ProgressBar_Adapter_Console offers a text-based progressbar for console
* applications
@ -38,7 +43,7 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
* Percentage value of the progress
*/
const ELEMENT_PERCENT = 'ELEMENT_PERCENT';
/**
* Visual value of the progress
*/
@ -48,34 +53,34 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
* ETA of the progress
*/
const ELEMENT_ETA = 'ELEMENT_ETA';
/**
* Text part of the progress
*/
const ELEMENT_TEXT = 'ELEMENT_TEXT';
/**
* Finish action: End of Line
*/
const FINISH_ACTION_EOL = 'FINISH_ACTION_EOL';
/**
* Finish action: Clear Line
*/
const FINISH_ACTION_CLEAR_LINE = 'FINISH_ACTION_CLEAR_LINE';
/**
* Finish action: None
*/
const FINISH_ACTION_NONE = 'FINISH_ACTION_NONE';
/**
* Width of the progressbar
*
* @var integer
*/
protected $_width = null;
/**
* Elements to display
*
@ -84,21 +89,21 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
protected $_elements = array(self::ELEMENT_PERCENT,
self::ELEMENT_BAR,
self::ELEMENT_ETA);
/**
* Which action to do at finish call
*
* @var string
*/
protected $_finishAction = self::FINISH_ACTION_EOL;
/**
* Width of the bar element
*
* @var integer
*/
protected $_barWidth;
/**
* Left character(s) within the bar
*
@ -112,28 +117,28 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
* @var string
*/
protected $_barIndicatorChar = '';
/**
* Right character(s) within the bar
*
* @var string
*/
protected $_barRightChar = '-';
/**
* Stdout stream, when STDOUT is not defined (e.g. in CGI)
*
* Output-stream, when STDOUT is not defined (e.g. in CGI) or set manually
*
* @var resource
*/
protected $_stdout = null;
protected $_outputStream = null;
/**
* Width of the text element
*
* @var string
*/
protected $_textWidth = 20;
/**
* Wether the output started yet or not
*
@ -141,36 +146,78 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
*/
protected $_outputStarted = false;
/**
* Charset of text element
*
* @var string
*/
protected $_charset = 'utf-8';
/**
* Defined by Zend_ProgressBar_Adapter
*
* @param null|array|Zend_Config $options
*/
public function __construct($options = null)
{
// If STDOUT isn't defined, open a local resource
if (!defined('STDOUT')) {
$this->_stdout = fopen('php://stdout', 'w');
}
{
// Call parent constructor with options
parent::__construct($options);
// Check if a width was set, else use auto width
if ($this->_width === null) {
$this->setWidth();
}
}
/**
* Close local stdout, when open
*/
public function __destruct()
{
if ($this->_stdout !== null) {
fclose($this->_stdout);
if ($this->_outputStream !== null) {
fclose($this->_outputStream);
}
}
/**
* Set a different output-stream
*
* @param string $resource
* @return Zend_ProgressBar_Adapter_Console
*/
public function setOutputStream($resource)
{
$stream = @fopen($resource, 'w');
if ($stream === false) {
require_once 'Zend/ProgressBar/Adapter/Exception.php';
throw new Zend_ProgressBar_Adapter_Exception('Unable to open stream');
}
if ($this->_outputStream !== null) {
fclose($this->_outputStream);
}
$this->_outputStream = $stream;
}
/**
* Get the current output stream
*
* @return resource
*/
public function getOutputStream()
{
if ($this->_outputStream === null) {
if (!defined('STDOUT')) {
$this->_outputStream = fopen('php://stdout', 'w');
} else {
return STDOUT;
}
}
return $this->_outputStream;
}
/**
* Set the width of the progressbar
@ -186,27 +233,27 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
// terminal always has a fixed width of 80 characters and the
// cursor is counted to the line, else windows would line break
// after every update.
$this->_width = 79;
$this->_width = 79;
} else {
// Set the default width of 80
$this->_width = 80;
// Try to determine the width through stty
if (preg_match('#\d+ (\d+)#', @shell_exec('stty size'), $match) === 1) {
$this->_width = (int) $match[1];
} else if (preg_match('#columns = (\d+);#', @shell_exec('stty'), $match) === 1) {
} else if (preg_match('#columns = (\d+);#', @shell_exec('stty'), $match) === 1) {
$this->_width = (int) $match[1];
}
}
} else {
$this->_width = (int) $width;
}
$this->_calculateBarWidth();
return $this;
}
/**
* Set the elements to display with the progressbar
*
@ -220,19 +267,19 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
self::ELEMENT_BAR,
self::ELEMENT_ETA,
self::ELEMENT_TEXT);
if (count(array_diff($elements, $allowedElements)) > 0) {
require_once 'Zend/ProgressBar/Adapter/Exception.php';
throw new Zend_ProgressBar_Adapter_Exception('Invalid element found in $elements array');
throw new Zend_ProgressBar_Adapter_Exception('Invalid element found in $elements array');
}
$this->_elements = $elements;
$this->_calculateBarWidth();
return $this;
}
/**
* Set the left-hand character for the bar
*
@ -244,14 +291,14 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
{
if (empty($char)) {
require_once 'Zend/ProgressBar/Adapter/Exception.php';
throw new Zend_ProgressBar_Adapter_Exception('Character may not be empty');
throw new Zend_ProgressBar_Adapter_Exception('Character may not be empty');
}
$this->_barLeftChar = (string) $char;
return $this;
}
/**
* Set the right-hand character for the bar
*
@ -263,14 +310,14 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
{
if (empty($char)) {
require_once 'Zend/ProgressBar/Adapter/Exception.php';
throw new Zend_ProgressBar_Adapter_Exception('Character may not be empty');
throw new Zend_ProgressBar_Adapter_Exception('Character may not be empty');
}
$this->_barRightChar = (string) $char;
return $this;
}
/**
* Set the indicator character for the bar
*
@ -280,7 +327,7 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
public function setBarIndicatorChar($char)
{
$this->_barIndicatorChar = (string) $char;
return $this;
}
@ -293,11 +340,21 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
public function setTextWidth($width)
{
$this->_textWidth = (int) $width;
$this->_calculateBarWidth();
return $this;
}
/**
* Set the charset of the text element
*
* @param string $charset
*/
public function setCharset($charset)
{
$this->_charset = $charset;
}
/**
* Set the finish action
@ -311,23 +368,23 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
$allowedActions = array(self::FINISH_ACTION_CLEAR_LINE,
self::FINISH_ACTION_EOL,
self::FINISH_ACTION_NONE);
if (!in_array($action, $allowedActions)) {
require_once 'Zend/ProgressBar/Adapter/Exception.php';
throw new Zend_ProgressBar_Adapter_Exception('Invalid finish action specified');
throw new Zend_ProgressBar_Adapter_Exception('Invalid finish action specified');
}
$this->_finishAction = $action;
return $this;
}
/**
* Defined by Zend_ProgressBar_Adapter_Interface
*
* @param float $current Current progress value
* @param float $max Max progress value
* @param flaot $percent Current percent value
* @param float $percent Current percent value
* @param integer $timeTaken Taken time in seconds
* @param integer $timeRemaining Remaining time in seconds
* @param string $text Status text
@ -342,39 +399,39 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
$data = '';
$this->_outputStarted = true;
}
// Build all elements
$renderedElements = array();
foreach ($this->_elements as $element) {
switch ($element) {
case self::ELEMENT_BAR:
$visualWidth = $this->_barWidth - 2;
$visualWidth = $this->_barWidth - 2;
$bar = '[';
$indicatorWidth = strlen($this->_barIndicatorChar);
$doneWidth = min($visualWidth - $indicatorWidth, round($visualWidth * $percent));
if ($doneWidth > 0) {
$bar .= substr(str_repeat($this->_barLeftChar, ceil($doneWidth / strlen($this->_barLeftChar))), 0, $doneWidth);
}
$bar .= $this->_barIndicatorChar;
$leftWidth = $visualWidth - $doneWidth - $indicatorWidth;
if ($leftWidth > 0) {
$bar .= substr(str_repeat($this->_barRightChar, ceil($leftWidth / strlen($this->_barRightChar))), 0, $leftWidth);
$bar .= substr(str_repeat($this->_barRightChar, ceil($leftWidth / strlen($this->_barRightChar))), 0, $leftWidth);
}
$bar .= ']';
$renderedElements[] = $bar;
break;
case self::ELEMENT_PERCENT:
$renderedElements[] = str_pad(round($percent * 100), 3, ' ', STR_PAD_LEFT) . '%';
break;
case self::ELEMENT_ETA:
// In the first 5 seconds we don't get accurate results,
// this skipping technique is found in many progressbar
@ -383,32 +440,32 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
$renderedElements[] = str_repeat(' ', 12);
break;
}
if ($timeRemaining === null || $timeRemaining > 86400) {
$etaFormatted = '??:??:??';
} else {
$hours = floor($timeRemaining / 3600);
$minutes = floor(($timeRemaining % 3600) / 60);
$seconds = ($timeRemaining % 3600 % 60);
$etaFormatted = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
$renderedElements[] = 'ETA ' . $etaFormatted;
break;
break;
case self::ELEMENT_TEXT:
$renderedElements[] = str_pad(substr($text, 0, $this->_textWidth), $this->_textWidth, ' ');
$renderedElements[] = Zend_Text_MultiByte::strPad(substr($text, 0, $this->_textWidth), $this->_textWidth, ' ', STR_PAD_RIGHT, $this->_charset);
break;
}
}
$data .= implode(' ', $renderedElements);
// Output line data
$this->_outputData($data);
}
/**
* Defined by Zend_ProgressBar_Adapter_Interface
*
@ -420,22 +477,22 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
case self::FINISH_ACTION_EOL:
$this->_outputData(PHP_EOL);
break;
case self::FINISH_ACTION_CLEAR_LINE:
if ($this->_outputStarted) {
$data = str_repeat("\x08", $this->_width)
. str_repeat(' ', $this->_width)
. str_repeat("\x08", $this->_width);
$this->_outputData($data);
}
break;
case self::FINISH_ACTION_NONE:
break;
}
}
/**
* Calculate the bar width when other elements changed
*
@ -445,26 +502,26 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
{
if (in_array(self::ELEMENT_BAR, $this->_elements)) {
$barWidth = $this->_width;
if (in_array(self::ELEMENT_PERCENT, $this->_elements)) {
$barWidth -= 4;
}
if (in_array(self::ELEMENT_ETA, $this->_elements)) {
$barWidth -= 12;
}
if (in_array(self::ELEMENT_TEXT, $this->_elements)) {
$barWidth -= $this->_textWidth;
}
$this->_barWidth = $barWidth - (count($this->_elements) - 1);
}
}
/**
* Outputs given data to STDOUT.
*
*
* This split-off is required for unit-testing.
*
* @param string $data
@ -472,10 +529,6 @@ class Zend_ProgressBar_Adapter_Console extends Zend_ProgressBar_Adapter
*/
protected function _outputData($data)
{
if ($this->_stdout !== null) {
fwrite($this->_stdout, $data);
} else {
fwrite(STDOUT, $data);
}
fwrite($this->getOutputStream(), $data);
}
}