/usr/share/php/ezc/ConsoleTools/output.php is in php-zeta-console-tools 1.7-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | <?php
/**
* File containing the ezcConsoleOutput 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
* under the License.
*
* @package ConsoleTools
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @filesource
*/
/**
* Class for handling console output.
*
* The ezcConsoleOutput class provides an interface to output text to the console. It deals with formating
* text in different ways and offers some comfortable options to deal
* with console text output.
*
* <code>
* // Create the output handler
* $out = new ezcConsoleOutput();
*
* // Set the verbosity to level 10
* $out->options->verbosityLevel = 10;
* // Enable auto wrapping of lines after 40 characters
* $out->options->autobreak = 40;
*
* // Set the color of the default output format to green
* $out->formats->default->color = 'green';
*
* // Set the color of the output format named 'success' to white
* $out->formats->success->color = 'white';
* // Set the style of the output format named 'success' to bold
* $out->formats->success->style = array( 'bold' );
*
* // Set the color of the output format named 'failure' to red
* $out->formats->failure->color = 'red';
* // Set the style of the output format named 'failure' to bold
* $out->formats->failure->style = array( 'bold' );
* // Set the background color of the output format named 'failure' to blue
* $out->formats->failure->bgcolor = 'blue';
*
* // Output text with default format
* $out->outputText( 'This is default text ' );
* // Output text with format 'success'
* $out->outputText( 'including success message', 'success' );
* // Some more output with default output.
* $out->outputText( "and a manual linebreak.\n" );
*
* // Manipulate the later output
* $out->formats->success->color = 'green';
* $out->formats->default->color = 'blue';
*
* // This is visible, since we set verbosityLevel to 10, and printed in default format (now blue)
* $out->outputText( "Some verbose output.\n", null, 10 );
* // This is not visible, since we set verbosityLevel to 10
* $out->outputText( "Some more verbose output.\n", null, 20 );
* // This is visible, since we set verbosityLevel to 10, and printed in format 'failure'
* $out->outputText( "And some not so verbose, failure output.\n", 'failure', 5 );
* </code>
*
* For a list of valid colors, style attributes and background colors, please
* refer to {@link ezcConsoleOutputFormat}.
*
* ATTENTION: Windows operating systems do not support styling of text on the
* console. Therefore no styling sequences are generated on any version of
* this operating system.
*
* @property ezcConsoleOutputOptions $options
* Contains the options for this class.
* @property ezcConsoleOutputFormats $formats
* Contains the output formats.
*
* @package ConsoleTools
* @version //autogen//
* @mainclass
*/
class ezcConsoleOutput
{
/**
* Target to print to standard out, with output buffering possibility.
*/
const TARGET_OUTPUT = "php://output";
/**
* Target to print to standard out.
*/
const TARGET_STDOUT = "php://stdout";
/**
* Target to print to standard error.
*/
const TARGET_STDERR = "php://stderr";
/**
* Container to hold the properties
*
* @var array(string=>mixed)
*/
protected $properties;
/**
* Whether a position has been stored before, using the storePos() method.
*
* @see ezcConsoleOutput::storePos()
* @var bool
*/
protected $positionStored = false;
/**
* Stores the mapping of color names to their escape
* sequence values.
*
* @var array(string=>int)
*/
protected static $color = array(
'gray' => 30,
'black' => 30, // Alias black to gray (Bug #8478)
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37,
'default' => 39
);
/**
* Stores the mapping of bgcolor names to their escape
* sequence values.
*
* @var array(string=>int)
*/
protected static $bgcolor = array(
'gray' => 40, // Alias gray to black (Bug #8478)
'black' => 40,
'red' => 41,
'green' => 42,
'yellow' => 43,
'blue' => 44,
'magenta' => 45,
'cyan' => 46,
'white' => 47,
'default' => 49,
);
/**
* Stores the mapping of styles names to their escape
* sequence values.
*
* @var array(string=>int)
*/
protected static $style = array(
'default' => '0',
'bold' => 1,
'faint' => 2,
'normal' => 22,
'italic' => 3,
'notitalic' => 23,
'underlined' => 4,
'doubleunderlined' => 21,
'notunderlined' => 24,
'blink' => 5,
'blinkfast' => 6,
'noblink' => 25,
'negative' => 7,
'positive' => 27,
);
/**
* Basic escape sequence string. Use sprintf() to insert escape codes.
*
* @var string
*/
private $escapeSequence = "\033[%sm";
/**
* Collection of targets to print to.
*
* @var array
*/
private $targets = array();
/**
* Create a new console output handler.
*
* @see ezcConsoleOutput::$options
* @see ezcConsoleOutputOptions
* @see ezcConsoleOutput::$formats
* @see ezcConsoleOutputFormats
*
* @param ezcConsoleOutputFormats $formats Formats to be used for output.
* @param array(string=>string) $options Options to set.
*/
public function __construct( ezcConsoleOutputFormats $formats = null, array $options = array() )
{
$options = isset( $options ) ? $options : new ezcConsoleOutputOptions();
$formats = isset( $formats ) ? $formats : new ezcConsoleOutputFormats();
$this->properties['options'] = new ezcConsoleOutputOptions( $options );
$this->properties['formats'] = $formats;
}
/**
* Set new options.
* This method allows you to change the options of an output handler.
*
* @param ezcConsoleOutputOptions $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
* ezcConsoleOutputOptions.
*/
public function setOptions( $options )
{
if ( is_array( $options ) )
{
$this->properties['options']->merge( $options );
}
else if ( $options instanceof ezcConsoleOutputOptions )
{
$this->properties['options'] = $options;
}
else
{
throw new ezcBaseValueException( "options", $options, "instance of ezcConsoleOutputOptions" );
}
}
/**
* Returns the current options.
* Returns the options currently set for this output handler.
*
* @return ezcConsoleOutputOptions The current options.
*/
public function getOptions()
{
return $this->properties['options'];
}
/**
* Property read access.
*
* @throws ezcBasePropertyNotFoundException
* If the the desired property is not found.
*
* @param string $propertyName Name of the property.
* @return mixed Value of the property or null.
* @ignore
*/
public function __get( $propertyName )
{
switch ( $propertyName )
{
case 'options':
case 'formats':
return $this->properties[$propertyName];
default:
break;
}
throw new ezcBasePropertyNotFoundException( $propertyName );
}
/**
* Property write access.
*
* @param string $propertyName Name of the property.
* @param mixed $val The value for the property.
*
* @throws ezcBaseValueException
* If a the value for the property options is not an instance of
* ezcConsoleOutputOptions.
* @throws ezcBaseValueException
* If a the value for the property formats is not an instance of
* ezcConsoleOutputFormats.
* @ignore
*/
public function __set( $propertyName, $val )
{
switch ( $propertyName )
{
case 'options':
if ( !( $val instanceof ezcConsoleOutputOptions ) )
{
throw new ezcBaseValueException( $propertyName, $val, 'ezcConsoleOutputOptions' );
}
$this->properties['options'] = $val;
return;
case 'formats':
if ( !( $val instanceof ezcConsoleOutputFormats ) )
{
throw new ezcBaseValueException( $propertyName, $val, 'ezcConsoleOutputFormats' );
}
$this->properties['formats'] = $val;
return;
default:
break;
}
throw new ezcBasePropertyNotFoundException( $propertyName );
}
/**
* Property isset access.
*
* @param string $propertyName Name of the property.
* @return bool True is the property is set, otherwise false.
* @ignore
*/
public function __isset( $propertyName )
{
switch ( $propertyName )
{
case 'options':
case 'formats':
return true;
}
return false;
}
/**
* Print text to the console.
*
* Output a string to the console. If $format parameter is omitted, the
* default style is chosen. Style can either be a special style {@link
* ezcConsoleOutput::$options}, a style name {@link
* ezcConsoleOutput::$formats} or 'default' to print with the default
* styling.
*
* The $format parameter defines the name of a format. Formats are defined
* through the $formats proprty, which contains format definitions in form
* of {@link ezcConsoleOutputFormat} objects. The format influences the
* outer appearance of a message (e.g. color) as well as the target the
* message is printed to (e.g. STDERR).
*
* @throws ezcConsoleInvalidOutputTargetException
* If the given target ({@link ezcConsoleOutputFormat}) could not
* be opened for writing or writing failed.
*
* @param string $text The text to print.
* @param string $format Format chosen for printing.
* @param int $verbosityLevel On which verbose level to output this message.
* @return void
*/
public function outputText( $text, $format = 'default', $verbosityLevel = 1 )
{
if ( $this->properties['options']->verbosityLevel >= $verbosityLevel )
{
if ( is_int( $this->properties['options']->autobreak ) && $this->properties['options']->autobreak > 0 )
{
$textLines = preg_split( "(\r\n|\n|\r)", $text );
foreach ( $textLines as $id => $textLine )
{
$textLines[$id] = wordwrap( $textLine, $this->properties['options']->autobreak, PHP_EOL, true );
}
$text = implode( PHP_EOL, $textLines );
}
// Initialize target, if not happened before
if ( !isset( $this->targets[$this->formats->$format->target] ) )
{
// @ to suppress the warning. We handle error cases with an
// exception here.
if ( ( $this->targets[$this->formats->$format->target] = @fopen( $this->formats->$format->target, "w" ) ) === false )
{
throw new ezcConsoleInvalidOutputTargetException( $this->formats->$format->target );
}
}
// Print using formats or without. Note: Since the target is a part
// of the format, it will also be ignored, if you switch formats off!
if ( $this->properties['options']->useFormats === true )
{
if ( fwrite( $this->targets[$this->formats->$format->target], $this->formatText( $text, $format ) ) === false )
{
throw new ezcConsoleInvalidOutputTargetException( $this->formats->$format->target );
}
}
else
{
echo $text;
}
}
}
/**
* Print text to the console and automatically append a line break.
*
* This method acts similar to {@link ezcConsoleOutput::outputText()}, in
* fact it even uses it. The difference is, that outputLine()
* automatically appends a manual line break to the printed text. Besides
* that, you can leave out the $text parameter of outputLine() to only
* print a line break.
*
* The $format parameter defines the name of a format. Formats are defined
* through the $formats proprty, which contains format definitions in form
* of {@link ezcConsoleOutputFormat} objects. The format influences the
* outer appearance of a message (e.g. color) as well as the target the
* message is printed to (e.g. STDERR).
*
* @param string $text The text to print.
* @param string $format Format chosen for printing.
* @param int $verbosityLevel On which verbose level to output this message.
* @return void
*/
public function outputLine( $text = '', $format = 'default', $verbosityLevel = 1 )
{
$this->outputText( $text . PHP_EOL, $format, $verbosityLevel );
}
/**
* Returns a formated version of the text.
*
* If $format parameter is omitted, the default style is chosen. The format
* must be a valid registered format definition. For information on the
* formats, see {@link ezcConsoleOutput::$formats}.
*
* @param string $text Text to apply style to.
* @param string $format Format chosen to be applied.
* @return string
*/
public function formatText( $text, $format = 'default' )
{
switch ( ezcBaseFeatures::os() )
{
case "Windows":
return $text;
default:
return $this->buildSequence( $format ) . $text . $this->buildSequence( 'default' );
}
}
/**
* Stores the current cursor position.
*
* Saves the current cursor position to return to it using
* {@link ezcConsoleOutput::restorePos()}. Multiple calls
* to this method will override each other. Only the last
* position is saved.
*
* @return void
*/
public function storePos()
{
if ( ezcBaseFeatures::os() !== "Windows" )
{
echo "\0337";
$this->positionStored = true;
}
}
/**
* Restores a cursor position.
*
* Restores the cursor position last saved using {@link
* ezcConsoleOutput::storePos()}.
*
* @throws ezcConsoleNoPositionStoredException
* If no position is saved.
* @return void
*/
public function restorePos()
{
if ( ezcBaseFeatures::os() !== "Windows" )
{
if ( $this->positionStored === false )
{
throw new ezcConsoleNoPositionStoredException();
}
echo "\0338";
}
}
/**
* Move the cursor to a specific column of the current line.
*
* Moves the cursor to a specific column index of the current line (default
* is 1).
*
* @param int $column Column to jump to.
* @return void
*/
public function toPos( $column = 1 )
{
if ( ezcBaseFeatures::os() !== "Windows" )
{
echo "\033[{$column}G";
}
}
/**
* Returns if a format code is valid for the specific formating option.
*
* This method determines if a given code is valid for a specific
* formatting option ('color', 'bgcolor' or 'style').
*
* @see ezcConsoleOutput::getFormatCode();
*
* @param string $type Formating type.
* @param string $key Format option name.
* @return bool True if the code is valid.
*/
public static function isValidFormatCode( $type, $key )
{
return isset( self::${$type}[$key] );
}
/**
* Returns the escape sequence for a specific format.
*
* Returns the default format escape sequence, if the requested format does
* not exist.
*
* @param string $format Name of the format.
* @return string The escape sequence.
*/
protected function buildSequence( $format = 'default' )
{
if ( $format === 'default' )
{
return sprintf( $this->escapeSequence, 0 );
}
$modifiers = array();
$formats = array( 'color', 'style', 'bgcolor' );
foreach ( $formats as $formatType )
{
// Get modifiers
if ( is_array( $this->formats->$format->$formatType ) )
{
if ( !in_array( 'default', $this->formats->$format->$formatType ) )
{
foreach ( $this->formats->$format->$formatType as $singleVal )
{
$modifiers[] = $this->getFormatCode( $formatType, $singleVal );
}
}
}
else
{
if ( $this->formats->$format->$formatType !== 'default' )
{
$modifiers[] = $this->getFormatCode( $formatType, $this->formats->$format->$formatType );
}
}
}
// Merge modifiers
return sprintf( $this->escapeSequence, implode( ';', $modifiers ) );
}
/**
* Returns the code for a given formating option of a given type.
*
* $type is the type of formating ('color', 'bgcolor' or 'style'), $key the
* name of the format to lookup. Returns the numeric code for the requested
* format or 0 if format or type do not exist.
*
* @see ezcConsoleOutput::isValidFormatCode()
*
* @param string $type Formatting type.
* @param string $key Format option name.
* @return int The code representation.
*/
protected function getFormatCode( $type, $key )
{
if ( !ezcConsoleOutput::isValidFormatCode( $type, $key ) )
{
return 0;
}
return ezcConsoleOutput::${$type}[$key];
}
}
?>
|