Apache Zeta Components Manual :: File Source for progressbar.php
Source for file progressbar.php
Documentation is available at progressbar.php
* File containing the ezcConsoleProgressbar class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* Creating and maintaining progress-bars to be printed to the console.
* $out = new ezcConsoleOutput();
* // Create progress bar itself
* $progress = new ezcConsoleProgressbar( $out, 100, array( 'step' => 5 ) );
* $progress->options->emptyChar = '-';
* $progress->options->progressChar = '#';
* $progress->options->formatString = "Uploading file </tmp/foobar.tar.bz2>: %act%/%max% kb [%bar%]";
* // Do whatever you want to indicate progress for
* usleep( mt_rand( 20000, 2000000 ) );
* // Advance the progressbar by one step ( uploading 5k per run )
* // Finish progress bar and jump to next line.
* $out->outputText( "Successfully uploaded </tmp/foobar.tar.bz2>.\n", 'success' );
* @property ezcConsoleProgressbarOptions $options
* Contains the options for this class.
* The maximum progress value to reach.
* Container to hold the properties
* @var array(string=>mixed)
* Storage for actual values to be replaced in the format string.
* Actual values are stored here and will be inserted into the bar
* @var array(string=>string)
* Stores the bar utilization.
* This array saves how much space a specific part of the bar utilizes to not
* recalculate those on every step.
* @var array(string=>int)
* The current step the progress bar should show.
* The maximum number of steps to go.
* Calculated once from the settings.
* The ezcConsoleOutput object to use.
* Indicates if the starting point for the bar has been stored.
* Per default this is false to indicate that no start position has been
* Tool object to perform multi-byte encoding safe string operations.
* @var ezcConsoleStringTool
* Creates a new progress bar.
* @param ezcConsoleOutput $outHandler Handler to utilize for output
* @param int $max Maximum value, where progressbar
* @param array(string=>string) $options Options
* @see ezcConsoleProgressbar::$options
public function __construct( ezcConsoleOutput $outHandler, $max, array $options =
array() )
$this->stringTool =
new ezcConsoleStringTool();
$this->__set( 'max', $max );
* This method allows you to change the options of progressbar.
* @param ezcConsoleProgresbarOptions $options The options to set.
* @throws ezcBaseSettingNotFoundException
* If you tried to set a non-existent option value.
* @throws ezcBaseSettingValueException
* If the value is not valid for the desired option.
* @throws ezcBaseValueException
* If you submit neither an array nor an instance of
* ezcConsoleProgresbarOptions.
* Returns the current options.
* Returns the options currently set for this progressbar.
* @return ezcConsoleProgressbarOptions The current options.
* @param string $key Name of the property.
* @return mixed Value of the property or null.
* @throws ezcBasePropertyNotFoundException
* If the the desired property is not found.
public function __get( $key )
* @param string $key Name of the property.
* @param mixed $val The value for the property.
* @throws ezcBasePropertyNotFoundException
* If a desired property could not be found.
* @throws ezcBaseValueException
* If a desired property value is out of range.
public function __set( $key, $val )
// Step is now an option.
// Changes settings or options, need for recalculating measures
* @param string $key Name of the property.
* @return bool True is the property is set, otherwise false.
public function __isset( $key )
* Starts the progress bar and sticks it to the current line.
* No output will be done yet. Call {@link ezcConsoleProgressbar::output()}
* Prints the progress-bar to the screen. If start() has not been called
* yet, the current line is used for {@link ezcConsolProgressbar::start()}.
if ( $this->options->minVerbosity >
$this->output->options->verbosityLevel
||
( $this->options->maxVerbosity !==
false
&&
$this->options->maxVerbosity <
$this->output->options->verbosityLevel
// Do not print progress bar if verbosity level is lower than it's
* Advance the progress bar.
* Advances the progress bar by $step steps. Redraws the bar by default,
* using the {@link ezcConsoleProgressbar::output()} method.
* @param bool $redraw Whether to redraw the bar immediately.
* @param int $step How many steps to advance.
public function advance( $redraw =
true, $step =
1 )
* Finish the progress bar.
* Finishes the bar (jump to 100% if not happened yet,...) and jumps
* to the next line to allow new output. Also resets the values of the
* output handler used, if changed.
* Generate all values to be replaced in the format string.
// Sanitize value if it gets to large by rounding
$barFilledSpace =
$barFilledSpace >
$this->measures['barSpace'] ?
$this->measures['barSpace'] :
$barFilledSpace;
$bar =
$this->stringTool->strPad(
$this->stringTool->strPad(
( $fractionVal =
( $this->properties['options']->step *
$this->currentStep ) /
$this->max *
100 ) >
100 ?
100 :
$fractionVal
$this->valueMap['fraction'] =
$this->stringTool->strPad(
( $actVal =
$this->currentStep *
$this->properties['options']->step ) >
$this->max ?
$this->max :
$actVal
$this->valueMap['act'] =
$this->stringTool->strPad(
* Insert values into bar format string.
$bar =
$this->properties['options']->formatString;
foreach ( $this->valueMap as $name =>
$val )
* Calculate several measures necessary to generate a bar.
// Calc number of steps bar goes through
* Strip all escape sequences from a string to measure it's size correctly.