Path

ez components / documentation / api reference / 2008.2.1 / consoletools


eZ Components 2008.2.1

ConsoleTools: ezcConsoleOutput

[ Tutorial ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Class: ezcConsoleOutput

Class for handling console output. [source]
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.
 1.  // Create the output handler
 2.   $out new ezcConsoleOutput();
 3.  
 4.  // Set the verbosity to level 10
 5.   $out->options->verbosityLevel 10;
 6.  // Enable auto wrapping of lines after 40 characters
 7.   $out->options->autobreak    40;
 8.  
 9.  // Set the color of the default output format to green
10.   $out->formats->default->color   'green';
11.  
12.  // Set the color of the output format named 'success' to white
13.   $out->formats->success->color   'white';
14.  // Set the style of the output format named 'success' to bold
15.   $out->formats->success->style   array'bold' );
16.  
17.  // Set the color of the output format named 'failure' to red
18.   $out->formats->failure->color   'red';
19.  // Set the style of the output format named 'failure' to bold
20.   $out->formats->failure->style   array'bold' );
21.  // Set the background color of the output format named 'failure' to blue
22.   $out->formats->failure->bgcolor 'blue';
23.  
24.  // Output text with default format
25.   $out->outputText'This is default text ' );
26.  // Output text with format 'success'
27.   $out->outputText'including success message''success' );
28.  // Some more output with default output.
29.   $out->outputText"and a manual linebreak.\n" );
30.  
31.  // Manipulate the later output
32.   $out->formats->success->color 'green';
33.  $out->formats->default->color 'blue';
34.  
35.  // This is visible, since we set verbosityLevel to 10, and printed in default format (now blue)
36.   $out->outputText"Some verbose output.\n"null10 );
37.  // This is not visible, since we set verbosityLevel to 10
38.   $out->outputText"Some more verbose output.\n"null20 );
39.  // This is visible, since we set verbosityLevel to 10, and printed in format 'failure'
40.   $out->outputText"And some not so verbose, failure output.\n"'failure');
For a list of valid colors, style attributes and background colors, please refer to 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.

Constants

TARGET_OUTPUT = "php://output" Target to print to standard out, with output buffering possibility.
TARGET_STDERR = "php://stderr" Target to print to standard error.
TARGET_STDOUT = "php://stdout" Target to print to standard out.

Properties

ezcConsoleOutputFormats read/write  $formats
Contains the output formats.
ezcConsoleOutputOptions read/write  $options
Contains the options for this class.

Member Variables

protected array(string=>int) $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 bgcolor names to their escape sequence values.
protected array(string=>int) $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 color names to their escape sequence values.
protected bool $positionStored = false
Whether a position has been stored before, using the storePos() method.
protected array(string=>mixed) $properties
Container to hold the properties
protected array(string=>int) $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,
)

Stores the mapping of styles names to their escape sequence values.

Method Summary

public ezcConsoleOutput __construct( [$formats = null], [$options = array()] )
Create a new console output handler.
protected string buildSequence( [$format = 'default'] )
Returns the escape sequence for a specific format.
public string formatText( $text, [$format = 'default'] )
Returns a formated version of the text.
protected int getFormatCode( $type, $key )
Returns the code for a given formating option of a given type.
public ezcConsoleOutputOptions getOptions( )
Returns the current options.
public static bool isValidFormatCode( $type, $key )
Returns if a format code is valid for the specific formating option.
public void outputLine( [$text = ''], [$format = 'default'], [$verbosityLevel = 1] )
Print text to the console and automatically append a line break.
public void outputText( $text, [$format = 'default'], [$verbosityLevel = 1] )
Print text to the console.
public void restorePos( )
Restores a cursor position.
public void setOptions( $options )
Set new options.
public void storePos( )
Stores the current cursor position.
public void toPos( [$column = 1] )
Move the cursor to a specific column of the current line.

Methods

__construct

ezcConsoleOutput __construct( [ezcConsoleOutputFormats $formats = null], [ $options = array()] )
Create a new console output handler.

Parameters

Name Type Description
$formats ezcConsoleOutputFormats Formats to be used for output.
$options array(string=>string) Options to set.

See also:

ezcConsoleOutputFormats, ezcConsoleOutput::$formats, ezcConsoleOutputOptions, ezcConsoleOutput::$options.


buildSequence

string buildSequence( [string $format = 'default'] )
Returns the escape sequence for a specific format.
Returns the default format escape sequence, if the requested format does not exist.

Parameters

Name Type Description
$format string Name of the format.

formatText

string formatText( string $text, [string $format = 'default'] )
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 ezcConsoleOutput::$formats.

Parameters

Name Type Description
$text string Text to apply style to.
$format string Format chosen to be applied.

getFormatCode

int getFormatCode( string $type, string $key )
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.

Parameters

Name Type Description
$type string Formatting type.
$key string Format option name.

See also:

ezcConsoleOutput::isValidFormatCode().


getOptions

ezcConsoleOutputOptions getOptions( )
Returns the current options.
Returns the options currently set for this output handler.

isValidFormatCode

bool isValidFormatCode( string $type, string $key )
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').

Parameters

Name Type Description
$type string Formating type.
$key string Format option name.

See also:

ezcConsoleOutput::getFormatCode();.


outputLine

void outputLine( [string $text = ''], [string $format = 'default'], [int $verbosityLevel = 1] )
Print text to the console and automatically append a line break.
This method acts similar to 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 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).

Parameters

Name Type Description
$text string The text to print.
$format string Format chosen for printing.
$verbosityLevel int On which verbose level to output this message.

outputText

void outputText( string $text, [string $format = 'default'], [int $verbosityLevel = 1] )
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 ezcConsoleOutput::$options, a style name 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 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).

Parameters

Name Type Description
$text string The text to print.
$format string Format chosen for printing.
$verbosityLevel int On which verbose level to output this message.

Throws

ClassDescription
ezcConsoleInvalidOutputTargetException If the given target (ezcConsoleOutputFormat) could not be opened for writing or writing failed.

restorePos

void restorePos( )
Restores a cursor position.
Restores the cursor position last saved using ezcConsoleOutput::storePos().

Throws

ClassDescription
ezcConsoleNoPositionStoredException If no position is saved.

setOptions

void setOptions( ezcConsoleOutputOptions $options )
Set new options.
This method allows you to change the options of an output handler.

Parameters

Name Type Description
$options ezcConsoleOutputOptions The options to set.

Throws

ClassDescription
ezcBaseSettingNotFoundException If you tried to set a non-existent option value.
ezcBaseSettingValueException If the value is not valid for the desired option.
ezcBaseValueException If you submit neither an array nor an instance of ezcConsoleOutputOptions.

storePos

void storePos( )
Stores the current cursor position.
Saves the current cursor position to return to it using ezcConsoleOutput::restorePos(). Multiple calls to this method will override each other. Only the last position is saved.

toPos

void toPos( [int $column = 1] )
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).

Parameters

Name Type Description
$column int Column to jump to.

Last updated: Mon, 09 Feb 2009