Path

ez components / documentation / api reference / 1.0.1 / consoletools


eZ Components 1.0.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 verboseLevel 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 verboseLevel to 10
38.   $out->outputText"Some more verbose output.\n"null20 );
39.  // This is visible, since we set verboseLevel 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.

Member Variables

protected array(string=>int) $bgcolor = array(
'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,
'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 ezcConsoleOutputFormats $formats
Formats
protected ezcConsoleOutputOptions $options
Options
protected bool $positionStored = false
Whether a position has been stored before, using the storePos() method.
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 static bool isValidFormatCode( $type, $key )
Returns if a format code is valid for ta 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 options for this object.
public void storePos( )
Stores the current cursor position.
public void toPos( [$col = 1] )
Move the cursor to a specific column of the current line.
public mixed __get( $propertyName )
Property read access.
public bool __isset( $propertyName )
Property isset access.
public void __set( $propertyName, $val )
Property write access.

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:

ezcConsoleOutputFormatsezcConsoleOutput::$formatsezcConsoleOutputOptionsezcConsoleOutput::$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 omited, 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().


isValidFormatCode

bool isValidFormatCode( string $type, string $key )
Returns if a format code is valid for ta 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.

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 ommited, the default style is chosen. Style can either be a special style eczConsoleOutput::$options, a style name ezcConsoleOutput$formats or 'none' to print without any styling.

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.

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( $options )
Set options for this object.
Set the options.

Parameters

Name Type Description
$options array  

See also:

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 $col = 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
$col int Column to jump to.

__get

mixed __get( string $propertyName )
Property read access.

Parameters

Name Type Description
$propertyName string Name of the property.

Throws

ClassDescription
ezcBasePropertyNotFoundException If the the desired property is not found.

__isset

bool __isset( string $propertyName )
Property isset access.

Parameters

Name Type Description
$propertyName string Name of the property.

__set

void __set( string $propertyName, mixed $val )
Property write access.

Parameters

Name Type Description
$propertyName string Name of the property.
$val mixed The value for the property.

Throws

ClassDescription
ezcBaseValueException If a the value for the property options is not an instance of ezcConsoleOutputOptions.
ezcBaseValueException If a the value for the property formats is not an instance of ezcConsoleOutputFormats.

Last updated: Fri, 02 Nov 2007